Imagine a sql table with a very high number of rows (for example 300 million of rows).
Each row have an text field and I’d like to retrieve 10 rows randomly.
The problem is that using
select key from the_table where enabled=1 order by rand() limit 10
Calculate the rand value and order all the rows by that value is a very high load.
Is there some way for avoiding that?
If you can generate the keys outside of SQL (even if it takes using 15-20 to get 10 that are enabled) you can then skip the order() which is almost certainly what the problem is wrt to load
SELECT key WHERE key IN [x,y,z….]
where x,y,z are generated randomly and externally.
Even better, you can create a table of enabled keys, sorted randomly and just page through that in chunks of 10. You’d need to update it occasionally and there’s a penalty in space but that’s probably not THAT big a deal.
There’s more sophisticated solutions, but that would get you moving.