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

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

    For selecting multiple random rows from a given table (say 'words'), our team came up with this beauty:

    SELECT * FROM
    `words` AS r1 JOIN 
    (SELECT  MAX(`WordID`) as wid_c FROM `words`) as tmp1
    WHERE r1.WordID >= (SELECT (RAND() * tmp1.wid_c) AS id) LIMIT n
    

提交回复
热议问题