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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:44:40+00:00 2026-06-17T09:44:40+00:00

Possible Duplicate: How do I select random rows in MySQL? Okay, so I have

  • 0

Possible Duplicate:
How do I select random rows in MySQL?

Okay, so I have a table storing game data, and it’s reaching 7 million rows.

I need an efficient way to select a random row.

Here’s what I’ve tried so far:

SELECT `id` FROM `information` ORDER BY RAND() LIMIT 1;

Fairly logical query. Takes 8 seconds to run. Completely unacceptable.

Second attempt:

SELECT FLOOR(RAND()*COUNT(`id`)) INTO @target FROM `information`;
SELECT `id` FROM `information` LIMIT @target,1;

Fails on syntax error with the variable in the second query.

Third attempt:

SELECT `id` FROM (SELECT `id` FROM `information`) `a` ORDER BY RAND() LIMIT 1

The idea here is to strip out all the other fields, since they’re unnecessary bulk. It half-works, bringing the time down to 6 seconds. Still far too long, though.

And… I’m out. I can’t think of any other ways (but it is 1 AM…), so any suggestions would be much appreciated. 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-06-17T09:44:41+00:00Added an answer on June 17, 2026 at 9:44 am

    If you are using INNODB engine, an article I read before suggested that it is better to count the number of records first using COUNT(*) because the engine does not cache the count of the table unlike MyISAM,

    DECLARE @count INT;
    DECLARE @offset INT;
    
    SET @count = (SELECT COUNT(*) FROM `information`);
    SET @offset = CONVERT(FLOOR(RAND() * @count), SIGNED);
    
    PREPARE mystatement FROM "SELECT `id` FROM `information` ORDER BY RAND() LIMIT ?, 1";
    EXECUTE mystatement USING @offset;
    DEALLOCATE PREPARE mystatement;
    
    • SQLFiddle Demo

    DISCLAIMER: the query i used performed exactly as needed on 3m records. I don’t know how it behaves on 7m records though.

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

Sidebar

Related Questions

Possible Duplicate: MySQL select 10 random rows from 600K rows fast I need to
Possible Duplicate: Select n random rows from SQL Server table I have database and
Possible Duplicate: MySQL select 10 random rows from 600K rows fast There are many
Possible Duplicate: MySQL select join where AND where Well, I have a table -
Possible Duplicate: MySQL: Select Random Entry, but Weight Towards Certain Entries I have a
Possible Duplicate: Mysql Select Enum Values I have set up a coloumn in Mysql
Possible Duplicate: Select Records multiple times from table I want to have my query
Possible Duplicate: How to select rows where multiple joined table values meet selection criteria?
Possible Duplicate: quick selection of a random row from a large table in mysql
Possible Duplicate: Selecting Random Rows in MySQL I'm creating a simple web application using

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.