Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 325347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:12:30+00:00 2026-05-12T09:12:30+00:00

I’d like to optimize my queries so I look into mysql-slow.log . Most of

  • 0

I’d like to optimize my queries so I look into mysql-slow.log.

Most of my slow queries contains ORDER BY RAND(). I cannot find a real solution to resolve this problem. Theres is a possible solution at MySQLPerformanceBlog but I don’t think this is enough. On poorly optimized (or frequently updated, user managed) tables it doesn’t work or I need to run two or more queries before I can select my PHP-generated random row.

Is there any solution for this issue?

A dummy example:

SELECT  accomodation.ac_id,
        accomodation.ac_status,
        accomodation.ac_name,
        accomodation.ac_status,
        accomodation.ac_images
FROM    accomodation, accomodation_category
WHERE   accomodation.ac_status != 'draft'
        AND accomodation.ac_category = accomodation_category.acat_id
        AND accomodation_category.acat_slug != 'vendeglatohely'
        AND ac_images != 'b:0;'
ORDER BY
        RAND()
LIMIT 1
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T09:12:31+00:00Added an answer on May 12, 2026 at 9:12 am

    Try this:

    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 is especially efficient on MyISAM (since the COUNT(*) is instant), but even in InnoDB it’s 10 times more efficient than ORDER BY RAND().

    The main idea here is that we don’t sort, but instead keep two variables and calculate the running probability of a row to be selected on the current step.

    See this article in my blog for more detail:

    • Selecting random rows

    Update:

    If you need to select but a single random record, try this:

    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 assumes your ac_id‘s are distributed more or less evenly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Seemingly simple, but I cannot find anything relevant on the web. What is the
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
this is what i have right now Drawing an RSS feed into the php,
I would like to run a str_replace or preg_replace which looks for certain words
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.