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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:23:35+00:00 2026-05-27T09:23:35+00:00

I’ve looked around and there doesnt seem to be any easy way to do

  • 0

I’ve looked around and there doesnt seem to be any easy way to do this. It almost looks like it’s easier just to grab a subset of records and do all the randomizing in code (perl). The methods I’ve seen online seem like theyre geared more to at most hundreds of thousands, but certainly not millions.

The table I’m working with has 6 million records (and growing), the IDs are auto incremented, but not always stored in the table (non-gapless).

I’ve tried to do the LIMIT 1 query that’s been recommended, but the query takes forever to run — is there a quick way to do this, given that there are gaps in the record? I can’t just take the max and randomize over the range.

Update:

One idea I had maybe was to grab the max, randomize a limit based on the max, and then grab a range of 10 records from random_limit_1 to random_limit_2 and then taking the first record found in that range.

Or if I know the max, is there a way i can just pick say the 5th record of the table, without having to know which ID it is. Then just grabbing the id of that record.

Update:

This query is somewhat faster-ish. Still not fast enough =/

SELECT t.id FROM table t JOIN (SELECT(FLOOR(max(id) * rand())) as maxid FROM table) as tt on t.id >= tt.maxid 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-27T09:23:35+00:00Added an answer on May 27, 2026 at 9:23 am

    Yeah, idea seems good:

    select min(ID), max(ID) from table into @min, @max;
    set @range = @max - @min;
    set @mr = @min + ((@range / 1000) * (rand() * 1000));
    select ID from table
      where ID >= @mr and ID <= @mr + 1000
      order by rand()
      limit 1
    --   into @result
    ;
    

    May change 1000 to 10000 or whatever as needed to scale…

    EDIT: you could also try this:

    select ID from table
      where (ID % 1000) = floor(rand() * 1000)
      order by rand()
      limit 1
    ;
    

    Splits it along different lines…

    EDIT 2:

    See: What is the best way to pick a random row from a table in MySQL?

    This is probably the fastest way:

    select @row := floor(count(*) * rand()) from some_tbl;
    select some_ID from some_tbl limit @row, 1;
    

    unfortunately, variables can’t be used in limit clause so you’d have to use a dynamic query, either writing the query string in code, or using PREPARE and EXECUTE. Also, limit n, 1 still requires scanning n items into the table, so it’s only about twice as fast as the second method listed above on average. (Though it is probably more uniform and guarantees a matching row will always be found)

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.