MySQL's ORDER BY RAND()
is a simple way to get random database entries. However, retrieving multiple random rows using this method becomes extremely slow, especially with large datasets.
Consider this query aiming to select 18 random user profiles:
<code class="language-sql">SELECT u.id, p.photo FROM users u, profiles p WHERE p.memberid = u.id AND p.photo != '' AND (u.ownership=1 OR u.stamp=1) ORDER BY RAND() LIMIT 18</code>
The inefficiency stems from ORDER BY RAND()
's inherent need to sort the entire result set before applying the LIMIT
clause.
A More Efficient Approach
A superior strategy utilizes an indexed column to dramatically speed up random selection. Here's an optimized query:
<code class="language-sql">SELECT g.* FROM table g JOIN (SELECT id FROM table WHERE RAND() < (SELECT 4 / COUNT(*) ) * 10 LIMIT 4) AS subquery ON g.id = subquery.id;</code>
Explanation of Optimizations
This revised query incorporates several key improvements:
RAND()
against a percentage of the total rows. Indexing the id
column (or whichever column is used in the WHERE
clause) allows for rapid row identification.LIMIT
clause in both the subquery (4) and the overall query must be identical.WHERE
conditions as the main query, ensuring data integrity.This method significantly reduces query execution time, even with massive tables, providing a much more scalable solution for retrieving multiple random results.
Conclusion
These optimization techniques offer a substantial performance boost when dealing with random data selection in MySQL. By avoiding ORDER BY RAND()
, you can create more efficient and robust database applications.
The above is the detailed content of How Can I Efficiently Retrieve Multiple Random Results from a Database without Using `ORDER BY RAND()`?. For more information, please follow other related articles on the PHP Chinese website!