Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Random Result Pagination

Tags:

html

php

I currently have a listing of data that has a randomly generated order listing. Right now there is no pagination, so it is easy for me to generate a randomly ordered list. As my list of data grows, it gets slower because of all the data, so the obvious solution is pagination. The issue that I have with pagination is that I cannot randomly generate the order every time the page loads, so my manager and I have come to the conclusion that a list will have to be pre-generated ahead of time and will be re-generated every x amount of time. Now the issue is how do we store this generated list? There are four options that we've come up with:

  1. Session (takes up ram on the server)
  2. Cookies (more data is transmitted. Think about thousands of integer values transmitted to the user)
  3. Flat File (Implementation might take a bit of time. Not an extreme amount, but a little longer than the rest)
  4. database (a cron job will run ever x amount of time and do a mass update on all records. Our worry is that if there is too much data, it might slow down the system if people are hitting the server during an update.)

If there are any other solutions which seem better than pre-generated time-based lists, I'd love to hear about them.

Thanks.

UPDATE: An answer that I really liked, but was deleted for some reason, was that someone mentioned the use of a SEED, then I can store the seed instead of a list of ids, which would cut-down my data storage and simplify everything. I just tested the solution, and it works almost flawlessly. The only problem is that when I use LIMIT, everything will screw up. Does anyone have an suggestions to that? I don't want to have to generate all the data every time, I just want to use LIMIT * , . If I use this with a seed though, the numbers always reset, as it should.

Hopefully that made sense. It made more sense as I was thinking about it than how it turned out typed.

like image 660
JohnathanKong Avatar asked Jun 07 '10 17:06

JohnathanKong


People also ask

Does pagination help performance?

Thanks to pagination, we can split our large dataset into chunks ( or pages ) that we can gradually fetch and display to the user, thus reducing the load on the database. Pagination also solves a lot of performance issues both on the client and server-side!

What is skip and limit in pagination?

MongoDB has an extremely straightforward way to implement pagination: using skip and limit operations on a cursor. skip(n) skips n items in a query, while limit(m) returns only the next m items starting from the n-th one.

What is the use of pagination?

Pagination is the process of separating print or digital content into discrete pages. For print documents and some online content, pagination also refers to the automated process of adding consecutive numbers to identify the sequential order of pages.


1 Answers

Mysql RAND() accepts a seed as an optional argument. Using a seed, it will return the same randomized result set each time.

What you could do is generate a random seed in PHP on the first page request and pass it along to each page using a query string.

Edit: Sorry, I didn't realize the solution was posted already but was deleted.

like image 93
Kevin Avatar answered Oct 02 '22 16:10

Kevin