Let's say that I have a table with a bunch of records that I want to randomly present to users. I also want users to be able to paginate back and forth, so I need to keep some order at least for a while.
The application is basically just AJAX and uses the cache for pages that have already been visited, so even if I always performed random results when the user tried to return, he would get the previous page because it would load from the local cache.
The problem is that if I return only random results, there may be some duplicates. Each page contains 6 results, so to prevent this, I will need to do something like WHERE id NOT IN (1,2,3,4 ...)where I would put all previously loaded identifiers.
The huge drawback of this solution is that it will not be possible to cache anything on the server side, as each user requests different data.
An alternative solution would be to create another column to organize the records and shuffle it into each insertion time block. The problem here is that I will need to set a random number from the sequence for each record in the table, which will require as many queries as there are records.
I use Rails and MySQL, if relevant.
source
share