MYSQL order by rand() optimize

http://www.jcodecraeer.com/a/chengxusheji/java/2017/0109/6960.html

No more “order by rand()” in code.

#0.0012s
SELECT * FROM `content` 
WHERE id >= (SELECT FLOOR(RAND() * (SELECT MAX(id) FROM `content`)))  
ORDER BY id ASC LIMIT 10;
 
#0.2s
SELECT * FROM `content`
ORDER BY RAND() LIMIT 10;

Leave a Reply

Your email address will not be published.