quick selection of a random row from a large table in mysql

前端 未结 24 2026
旧时难觅i
旧时难觅i 2020-11-22 09:30

What is a fast way to select a random row from a large mysql table?

I\'m working in php, but I\'m interested in any solution even if it\'s in another language.

24条回答
  •  轮回少年
    2020-11-22 09:47

    if you don't delete row in this table, the most efficient way is:

    (if you know the mininum id just skip it)

    SELECT MIN(id) AS minId, MAX(id) AS maxId FROM table WHERE 1
    
    $randId=mt_rand((int)$row['minId'], (int)$row['maxId']);
    
    SELECT id,name,... FROM table WHERE id=$randId LIMIT 1
    

提交回复
热议问题