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

前端 未结 24 2112
旧时难觅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:45

    The classic "SELECT id FROM table ORDER BY RAND() LIMIT 1" is actually OK.

    See the follow excerpt from the MySQL manual:

    If you use LIMIT row_count with ORDER BY, MySQL ends the sorting as soon as it has found the first row_count rows of the sorted result, rather than sorting the entire result.

提交回复
热议问题