Better HN
top
new
best
ask
show
jobs
Log in
Sign up
MySQL select 10 random rows from 600K rows fast | Better HN
MySQL select 10 random rows from 600K rows fast
(stackoverflow.com)
1 points · by
morphics
· 13y ago · 4 comments
Comments
[–]
broshnat
13y ago
SELECT * FROM table ORDER BY RAND() LIMIT 10 ?
[–]
eurleif
13y ago
Try running EXPLAIN on that query. It has to create a temporary table, add a random column for every row, and then sort on that column. Not very fast.
[–]
broshnat
13y ago
Ok, how about get the max ID and run a loop of 10 queries matching a random ID between 1 and maxID?
[–]
eurleif
13y ago
That's essentially what the top-voted answer does, except it accounts for gaps in the IDs.