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 9126165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:58:30+00:00 2026-06-17T06:58:30+00:00

My question is based on the SO MySQL select 10 random rows from 600K

  • 0

My question is based on the SO MySQL select 10 random rows from 600K rows fast.
It is known

SELECT column FROM table
ORDER BY RAND()
LIMIT 10

is too slow for huge tables, another tricks and methods are used to extract some rows.

But what if I use WHERE:

SELECT column FROM table
WHERE colA=123
ORDER BY RAND()
LIMIT 10

What about the performance, if WHERE actually excludes at least 99.99% wrong rows among 600k?
By another words, what works first in this query – WHERE or ORDER BY RAND()?

If WHERE works first, does this mean ORDER BY RAND () sort only 60 rows (not 600k) and works fast?

  • 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-06-17T06:58:31+00:00Added an answer on June 17, 2026 at 6:58 am

    If this performs well (fast enough for you) and returns not many rows (say, less than a 1000):

    SELECT column FROM table
    WHERE colA=123 ;
    

    Then this will perform well, too, because it will sort only the (less than 1000) rows of the previous query:

    SELECT column FROM table
    WHERE colA=123
    ORDER BY RAND()
    LIMIT 10 ;
    

    If you want to be dead sure that it will perform pretty well, even if the first query returns many thousands or million rows, you can use this, which will limit the sorting to maximum 1000 (or a number of your choice) rows:

    SELECT column
    FROM
      ( SELECT column FROM table
        WHERE colA=123
        LIMIT 1000
      ) AS tmp
    ORDER BY RAND()
    LIMIT 10 ;
    

    The downside is that if there are indeed many rows, the 1000-cut will be arbitrary and indeterminate but not random. It will probably be done based on the indexes used for the query.

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

Sidebar

Related Questions

After dynamically creating a client-side table based on SELECT information retrieved from MySql database,
Consider the following table: mysql> select * from vCountryStatus; +-------------+------------+------+---------+--------+-----------------+ | CountryName | CountryISO
Based on this question here Selecting NOT NULL columns from a table One of
I am trying to select data from a table based on timestamps (stored as
This question is based on this thread . Problem: to access MySQL's manual when
Based on this question How to insert array into mysql using PDO and bindParam?
I have a three part question based on a dataframe (df is example rows)
I have this query in MySQL: SELECT pr.*, pr7.value AS `room_price_high` FROM `jos_hp_properties` pr
I have to delete about 10K rows from a table that has more than
Further to this question: MySQL - Conditional COUNT with GROUP BY Starting from the

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.