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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T15:55:15+00:00 2026-06-03T15:55:15+00:00

I have a db table filled with about ~30k records. I want to randomly

  • 0

I have a db table filled with about ~30k records.

I want to randomly select a record one at a time (when demanded by users), delete the record from the table, and insert it in another table.

I’ve heard/found that doing ORDER BY RAND() can be quite slow. So I’m using this algorithm (pseudo code):

lowest = getLowestId(); //get lowest primary key id from table
highest = getHighestId(); //get highest primary key id from table

do
{
    id = rand(lowest, highest); //get random number between a range of lowest id and highest id
    idExists = checkIfRandomIdExists( id );
}
while (! idExists);

row = getRow (id);
process(row);
delete(id);

Right now, with 30k records, I seem to get random ids very quickly. However as the table size decreases to 15k, 10k, 5k, 100, etc, (can be months) I’m concerned that this might begin to get slower.

Can I do anything to make this method more effective, or is there a row count at which point I should start doing ORDER BY RAND() instead of this method? (e.g when 5k rows are left, start doing ORDER BY RAND() ?)

  • 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-03T15:55:17+00:00Added an answer on June 3, 2026 at 3:55 pm

    I might recommend a Fisher-Yates Shuffle instead in a separate table. To generate this, create a table like:

    CREATE TABLE Shuffle
    (
        SequentialId INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
        OtherTableId INT NOT NULL
    )
    

    Notably, don’t bother with the foreign key constraint. In SQL Server, for instance, I would say to add a foreign key constraint with ON DELETE CASCADE; if you have a storage engine for which that would be workable in MySQL, go for it.

    Now, in the language of your choice:

    1. Get an array of all the IDs in the other table (as @Truth suggested in comments).
    2. Shuffle these ids using Fisher-Yates (takes linear time).
    3. Insert them into the Shuffle table in order.

    Now, you have a random order, so you can just INNER JOIN to the Shuffle table, then ORDER BY Shuffle.SequentialId to find the first record. You can delete the record from Shuffle manually if you have no way to do ON DELETE CASCADE.

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

Sidebar

Related Questions

I have table with 3 columns A B C. I want to select *
I have a MySQL table that records classified listings. We don't force users to
I have a table where new data gets filled each time a user clicks
I have a MySQL table with about 1000 rows, filled with different types of
I have a data table filled with text in each table row. How do
I have an HTML table filled with a number of rows. How can I
I have a question on calculating times. I have a simple table filled with
I have a table with three filled columns named Name, City and Occupation. I
I have a table cell that isn't entirely filled with text so the bg
I have a NSMutableArray filled with NSMutableArray's. I would like to fill my table

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.