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

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

    Grab all the id's, pick a random one from it, and retrieve the full row.

    If you know the id's are sequential without holes, you can just grab the max and calculate a random id.

    If there are holes here and there but mostly sequential values, and you don't care about a slightly skewed randomness, grab the max value, calculate an id, and select the first row with an id equal to or above the one you calculated. The reason for the skewing is that id's following such holes will have a higher chance of being picked than ones that follow another id.

    If you order by random, you're going to have a terrible table-scan on your hands, and the word quick doesn't apply to such a solution.

    Don't do that, nor should you order by a GUID, it has the same problem.

    0 讨论(0)
  • 2020-11-22 09:44

    MediaWiki uses an interesting trick (for Wikipedia's Special:Random feature): the table with the articles has an extra column with a random number (generated when the article is created). To get a random article, generate a random number and get the article with the next larger or smaller (don't recall which) value in the random number column. With an index, this can be very fast. (And MediaWiki is written in PHP and developed for MySQL.)

    This approach can cause a problem if the resulting numbers are badly distributed; IIRC, this has been fixed on MediaWiki, so if you decide to do it this way you should take a look at the code to see how it's currently done (probably they periodically regenerate the random number column).

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-22 09:45

    SELECT DISTINCT * FROM yourTable WHERE 4 = 4 LIMIT 1;

    0 讨论(0)
  • 2020-11-22 09:47

    Here's a solution that runs fairly quickly, and it gets a better random distribution without depending on id values being contiguous or starting at 1.

    SET @r := (SELECT ROUND(RAND() * (SELECT COUNT(*) FROM mytable)));
    SET @sql := CONCAT('SELECT * FROM mytable LIMIT ', @r, ', 1');
    PREPARE stmt1 FROM @sql;
    EXECUTE stmt1;
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题