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

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -