Home > Database > Mysql Tutorial > How Can I Optimize MySQL Queries Using ORDER BY RAND()?

How Can I Optimize MySQL Queries Using ORDER BY RAND()?

Linda Hamilton
Release: 2024-12-14 02:38:14
Original
602 people have browsed it

How Can I Optimize MySQL Queries Using ORDER BY RAND()?

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
Copy after login

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
        )
        )
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template