How can I request a random row (or as close to truly random as is possible) in pure SQL?
Solutions like Jeremies:
SELECT * FROM table ORDER BY RAND() LIMIT 1
work, but they need a sequential scan of all the table (because the random value associated with each row needs to be calculated - so that the smallest one can be determined), which can be quite slow for even medium sized tables. My recommendation would be to use some kind of indexed numeric column (many tables have these as their primary keys), and then write something like:
SELECT * FROM table WHERE num_value >= RAND() *
( SELECT MAX (num_value ) FROM table )
ORDER BY num_value LIMIT 1
This works in logarithmic time, regardless of the table size, if num_value
is indexed. One caveat: this assumes that num_value
is equally distributed in the range 0..MAX(num_value)
. If your dataset strongly deviates from this assumption, you will get skewed results (some rows will appear more often than others).