Order by Random Optimization
Problem
Functionrand
(MySQL) or random
(PostgreSQL) returns a random floating-point value in the range 0 to 1.0. It is useful to get a random sample of dataset using following query.
select * from orders order by rand() limit 1;
This works fine if customer
table is less than 10,000 rows. But when you have 1,000,000 rows, the overhead for sorting the rows becomes unacceptable. The reason is quiet clear: we sort all rows, but only keep one.