How can i optimize MySQL's ORDER BY RAND() function?

前端 未结 8 2418
天命终不由人
天命终不由人 2020-11-22 00:58

I\'d like to optimize my queries so I look into mysql-slow.log.

Most of my slow queries contains ORDER BY RAND(). I cannot find a real solu

8条回答
  •  暖寄归人
    2020-11-22 01:15

    (Yeah, I will get dinged for not having enough meat here, but can't you be a vegan for one day?)

    Case: Consecutive AUTO_INCREMENT without gaps, 1 row returned
    Case: Consecutive AUTO_INCREMENT without gaps, 10 rows
    Case: AUTO_INCREMENT with gaps, 1 row returned
    Case: Extra FLOAT column for randomizing
    Case: UUID or MD5 column

    Those 5 cases can be made very efficient for large tables. See my blog for the details.

提交回复
热议问题