MySQL: what's the most efficient way to select multiple random rows

前端 未结 2 637
执念已碎
执念已碎 2021-01-06 18:56

I have a fairly large dataset and a query that requires two joins, so efficiency of the query is very important to me. I need to retrieve 3 random rows from the database tha

相关标签:
2条回答
  • 2021-01-06 19:26

    Adding your RAND() call into the ORDER BY clause should allow you to ignore the ID. Try this:

    SELECT * FROM table WHERE ... ORDER BY RAND() LIMIT 3;
    

    After having performance issues pointed out, your best bet may be something along these lines (utilizing PHP):

    $result = PDO:query('SELECT MAX(id) FROM table');
    $max    = $result->fetchColumn();
    $ids    = array();
    $rows   = 5;
    
    for ($i = 0; $i < $rows; $i++) {
        $ids[] = rand(1, $max);
    }
    
    $ids     = implode(', ', $ids);
    $query   = PDO::prepare('SELECT * FROM table WHERE id IN (:ids)');
    $results = $query->execute(array('ids' => $ids));
    

    At this point you should be able to select the first 3 results. The only issue with this approach is dealing with deleted rows and you might have to either bump the $rows var or add some logic to do another query in case you didn't receive at least 3 results back.

    0 讨论(0)
  • 2021-01-06 19:40

    Since you don't want many results, there are a couple of interesting options using LIMIT and OFFSET.

    I'm going to assume an id column which is unique and suitable for sorting.

    The first step is to execute a COUNT(id), and then select random 3 numbers from 0 to COUNT(id) - 1 in PHP. (How to do that is a separate question, and the best approach depends on the number of rows total and the number you want).

    The second step has two options. Suppose the random numbers you selected are 0, 15, 2234. Either have a loop in PHP

    // $offsets = array(0, 15, 2234);
    foreach ($offsets as $offset) {
        $rows[] = execute_sql('SELECT ... ORDER BY id LIMIT 1 OFFSET ?', $offset);
    }
    

    or build a UNION. Note: this requires sub-selects because we're using ORDER BY.

    // $offsets = array(0, 15, 2234);
    $query = '';
    foreach ($offsets as $index => $offset) {
        if ($query) $query .= ' UNION ';
        $query .= 'SELECT * FROM (SELECT ... ORDER BY id LIMIT 1 OFFSET ?) Sub'.$index;
    }
    $rows = execute_sql($query, $offsets);
    
    0 讨论(0)
提交回复
热议问题