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

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

    An easy but slow way would be (good for smallish tables)

    SELECT * from TABLE order by RAND() LIMIT 1
    
    0 讨论(0)
  • 2020-11-22 09:54

    I ran into the problem where my IDs were not sequential. What I came up with this.

    SELECT * FROM products WHERE RAND()<=(5/(SELECT COUNT(*) FROM products)) LIMIT 1
    

    The rows returned are approximately 5, but I limit it to 1.

    If you want to add another WHERE clause it becomes a bit more interesting. Say you want to search for products on discount.

    SELECT * FROM products WHERE RAND()<=(100/(SELECT COUNT(*) FROM pt_products)) AND discount<.2 LIMIT 1
    

    What you have to do is make sure you are returning enough result which is why I have it set to 100. Having a WHERE discount<.2 clause in the subquery was 10x slower, so it's better to return more results and limit.

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

    Use the below query to get the random row

    SELECT user_firstname ,
    COUNT(DISTINCT usr_fk_id) cnt
    FROM userdetails 
    GROUP BY usr_fk_id 
    ORDER BY cnt ASC  
    LIMIT 1
    
    0 讨论(0)
  • 2020-11-22 09:55

    Create a Function to do this most likely the best answer and most fastest answer here!

    Pros - Works even with Gaps and extremely fast.

    <?
    
    $sqlConnect = mysqli_connect('localhost','username','password','database');
    
    function rando($data,$find,$max = '0'){
       global $sqlConnect; // Set as mysqli connection variable, fetches variable outside of function set as GLOBAL
       if($data == 's1'){
         $query = mysqli_query($sqlConnect, "SELECT * FROM `yourtable` ORDER BY `id` DESC LIMIT {$find},1");
    
         $fetched_data = mysqli_fetch_assoc($query);
          if(mysqli_num_rows($fetched_data>0){
           return $fetch_$data;
          }else{
           rando('','',$max); // Start Over the results returned nothing
          }
       }else{
         if($max != '0'){
            $irand = rand(0,$max); 
            rando('s1',$irand,$max); // Start rando with new random ID to fetch
         }else{
    
            $query = mysqli_query($sqlConnect, "SELECT `id` FROM `yourtable` ORDER BY `id` DESC LIMIT 0,1");
            $fetched_data = mysqli_fetch_assoc($query);
            $max = $fetched_data['id'];
            $irand = rand(1,$max);
            rando('s1',$irand,$max); // Runs rando against the random ID we have selected if data exist will return
         }
       }
     }
    
     $your_data = rando(); // Returns listing data for a random entry as a ASSOC ARRAY
    ?>
    

    Please keep in mind this code as not been tested but is a working concept to return random entries even with gaps.. As long as the gaps are not huge enough to cause a load time issue.

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-11-22 09:59

    Take a look at this link by Jan Kneschke or this SO answer as they both discuss the same question. The SO answer goes over various options also and has some good suggestions depending on your needs. Jan goes over all the various options and the performance characteristics of each. He ends up with the following for the most optimized method by which to do this within a MySQL select:

    SELECT name
      FROM random AS r1 JOIN
           (SELECT (RAND() *
                         (SELECT MAX(id)
                            FROM random)) AS id)
            AS r2
     WHERE r1.id >= r2.id
     ORDER BY r1.id ASC
     LIMIT 1;
    

    HTH,

    -Dipin

    0 讨论(0)
提交回复
热议问题