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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T06:18:20+00:00 2026-05-19T06:18:20+00:00

So in MySQL, I’ve read that for large tables with lots of rows, using

  • 0

So in MySQL, I’ve read that for large tables with lots of rows, using ORDER BY RAND() is a bad idea (even with ~500 row tables, supposedly). Slow and inefficient. Lots of row scanning.

How does this (below) seem for an alternative?

SELECT * FROM (…subquery that generally returns a set of fewer than 20 rows…) ORDER BY RAND() LIMIT 8

Instead of using RAND() on a large set of data, I’d select a small subset, and only then would I apply RAND() on those returned rows. In 99.9% of all cases, the subquery seen above should select fewer than 20 rows (and in fact, it’s generally fewer than 8).

Curious to hear what people think.

(Just for reference, I’m doing my MySQL stuff with PHP.)

Thanks!

  • 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-19T06:18:21+00:00Added an answer on May 19, 2026 at 6:18 am

    Actually…I ended up running a test and I might have answered my own question. I thought I’d post this information here in case it was useful for anyone else. (If I’ve done anything wrong here, please let me know!)

    This is kind of surprising…

    Contrary to everything that I’ve read, I created a table called TestData with 1 million rows and ran the following query:

    SELECT * FROM TestData WHERE number = 41 ORDER BY RAND() LIMIT 8

    …and it returned the rows in an average of 0.0070 seconds. I don’t really see why RAND() has such a bad reputation. It seems pretty usable to me, at least in this particular situation.

    I have three columns in my table:

    id [BIGINT(20)] | textfield [tinytext] | number [BIGINT(20)]

    Primary Key on id, index on number.

    I guess MySQL is smart enough to know that it should only be applying RAND() to the 20 rows that are returned by “WHERE number = 41” ? (I specifically added only 20 rows that had the value 41 for ‘number’.)

    The alternate subquery method returns results with an average time of around .0080 seconds, which is slower than the non-subquery method.

    Subquery method: SELECT * FROM (SELECT * FROM TestData WHERE number = 41) as t ORDER BY RAND() LIMIT 8

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

Sidebar

Related Questions

Mysql workbench (5.2.31 to 5.2.34) synchronization feature produces bad sql that fails to execute
mysql: get all tables in database that have a column called xyz
mysql_affected_rows is to get number of affected rows in previous MySQL operation, but I
MYSQL/PHP, I want to create a record of activities that people perform on the
MySQL has a RENAME TABLE statemnt that will allow you to change the name
Mysql begin generate big IO. One website have many rows in session,requests table (
MySQL, Trying to get a list of ip addresses, in order. this query select
MySQL profiler cuts queries more that 300 characters length when i get the list
MYSQL: SELECT * FROM book ORDER BY $sidx $sord LIMIT $start , $limit; How
Mysql PREPARE and EXECUTE statements can not be used in a stored procedure that

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.