mysql order by rand() performance issue and solution

前端 未结 4 855
旧时难觅i
旧时难觅i 2020-12-06 12:39

i was using order by rand() to generate random rows from database without any issue but i reaalised that as the database size increase this rand() causes heavy load on serve

4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-06 13:44

    A possible solution is to use limit:

    $id23=rand(1,$numberOfRows);
    
    SELECT items FROM tablea where status='0' LIMIT $id23 1
    

    This wont produce any missed rows (but as hek2mgl mentioned) requires knowing the number of rows in the select.

提交回复
热议问题