sql - MySQL select 10 random rows from 600K rows fast -
how can best write query selects 10 rows randomly total of 600k?
a great post handling several cases, simple, gaps, non-uniform gaps.
http://jan.kneschke.de/projects/mysql/order-by-rand/
for general case, here how it:
select name random r1 join (select ceil(rand() * (select max(id) random)) id) r2 r1.id >= r2.id order r1.id asc limit 1
this supposes distribution of ids equal, , there can gaps in id list. see article more advanced examples
Comments
Post a Comment