MySQL RAND() Ordering Optimization
Slow queries featuring ORDER BY RAND() present a performance challenge.
The Problem
Queries with ORDER BY RAND() often result in suboptimal performance, particularly on large or frequently updated tables. MySQL's solution (MySQLPerformanceBlog) may be insufficient.
The Solution
To optimize RAND() ordering, consider the following approach:
SELECT * FROM ( SELECT @cnt := COUNT(*) + 1, @lim := 10 FROM t_random ) vars STRAIGHT_JOIN ( SELECT r.*, @lim := @lim - 1 FROM t_random r WHERE (@cnt := @cnt - 1) AND RAND(20090301) < @lim / @cnt ) i
This approach avoids sorting by maintaining running probability while iterating through the data, making it more efficient than ORDER BY RAND().
For Single-Row Selection
To select a single random row, try this query:
SELECT aco.* FROM ( SELECT minid + FLOOR((maxid - minid) * RAND()) AS randid FROM ( SELECT MAX(ac_id) AS maxid, MIN(ac_id) AS minid FROM accomodation ) q ) q2 JOIN accomodation aco ON aco.ac_id = COALESCE ( ( SELECT accomodation.ac_id FROM accomodation WHERE ac_id > randid AND ac_status != 'draft' AND ac_images != 'b:0;' AND NOT EXISTS ( SELECT NULL FROM accomodation_category WHERE acat_id = ac_category AND acat_slug = 'vendeglatohely' ) ORDER BY ac_id LIMIT 1 ), ( SELECT accomodation.ac_id FROM accomodation WHERE ac_status != 'draft' AND ac_images != 'b:0;' AND NOT EXISTS ( SELECT NULL FROM accomodation_category WHERE acat_id = ac_category AND acat_slug = 'vendeglatohely' ) ORDER BY ac_id LIMIT 1 ) )
This optimization assumes that ac_ids are distributed relatively evenly.
The above is the detailed content of How Can I Optimize MySQL Queries Using ORDER BY RAND()?. For more information, please follow other related articles on the PHP Chinese website!