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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:09:55+00:00 2026-05-16T01:09:55+00:00

I would like to be able to pull back 15 or so records from

  • 0

I would like to be able to pull back 15 or so records from a database. I’ve seen that using WHERE id = rand() can cause performance issues as my database gets larger. All solutions I’ve seen are geared towards selecting a single random record. I would like to get multiples.

Does anyone know of an efficient way to do this for large databases?

edit:

Further Edit and Testing:

I made a fairly simple table, on a new database using MyISAM. I gave this 3 fields: autokey (unsigned auto number key) bigdata (a large blob) and somemore (a medium int). I then applied random data to the table and ran a series of queries using Navicat. Here are the results:

Query 1: select * from test order by rand() limit 15

 Query 2: select * 
          from 
      test 
          join 
      (select round(rand()*(select max(autokey) from test)) as val from test limit 15)                                           as rnd
      on 
          rnd.val=test.autokey;`

(I tried both select and select distinct and it made no discernible difference)

and:

Query 3 (I only ran this on the second test):
SELECT  *
    FROM    (
    SELECT  @cnt := COUNT(*) + 1,
            @lim := 10
    FROM    test
    ) vars
    STRAIGHT_JOIN
    (
    SELECT  r.*,
            @lim := @lim - 1
    FROM    test r
    WHERE   (@cnt := @cnt - 1)
            AND RAND(20090301) < @lim / @cnt
    ) i
ROWS:            QUERY 1:               QUERY 2:         QUERY 3:
2,060,922          2.977s                 0.002s            N/A

3,043,406          5.334s                 0.001s            1.260     

I would like to do more rows so I can see how query 3 scales, but at the moment, it seems as though the clear winner is query 2.

Before I wrap up this testing and declare an answer, and while I have all this data and the test environment set up, can anyone recommend any further testing?

  • 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-16T01:09:55+00:00Added an answer on May 16, 2026 at 1:09 am

    Try:

    select * from table order by rand() limit 15
    

    Another (and possibly more efficient way) would be to join against a set of random values. This should work, if there’s some contiguous integer key in the table. Here is how I would do it in postgres (My MySQL is a bit rusty)

    select * from table join 
       (select (random()*maxid)::integer as val from generate_series(1,15)) as rnd
       on rand.val=table.id;
    

    where maxid is the highest id in table. If id has an index, then this would mean only 15 index lookup, so its very fast.

    UPDATE:

    Looks like there no such thing as generate_series in MySQL. My fault. We don’t need it actually:

    select * 
    from 
     table 
    join 
     -- this just returns 15 random numbers. 
     -- I need `table` here only to produce rows for rand()
     (select round(rand()*(select max(id) from table)) as val from table limit 15) as rnd
    on 
     rnd.val=table.id;
    

    P.S. If I don’t want duplicates returned, I can use (select distinct […]) in the random generator expression.

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

Sidebar

Related Questions

I would like to be able to obtain all the parameter values from the
I would like to be able to loop through all of the defined parameters
We would like to be able to nightly make a copy/backup/snapshot of a production
I would like to be able to use the Tab key within a text
I would like to be able to display some dynamic text at the mouse
I would like to be able to define and use a custom type in
I would like to be able to predict what will be in the resulting
I would like to be able to do such things as var m1 =
I would like to be able to embed a command line interpreter inside a
I would like to be able to add a message to a unit test,

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.