MySQL: Alternatives to ORDER BY RAND()

前端 未结 7 1852
情歌与酒
情歌与酒 2020-11-22 02:46

I\'ve read about a few alternatives to MySQL\'s ORDER BY RAND() function, but most of the alternatives apply only to where on a single random result is needed.

7条回答
  •  春和景丽
    2020-11-22 03:20

    Order by rand() is very slow on large tables,

    I found the following workaround in a php script:

    Select min(id) as min, max(id) as max from table;
    

    Then do random in php

    $rand = rand($min, $max);
    

    Then

    'Select * from table where id>'.$rand.' limit 1';
    

    Seems to be quite fast....

提交回复
热议问题