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

  • Home
  • SEARCH
  • 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 6753543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:10:26+00:00 2026-05-26T13:10:26+00:00

So I have this relatively simple query to pull a random row based on

  • 0

So I have this relatively simple query to pull a random row based on a few criteria from two different MyISAM tables:

SELECT 
    table1 . * , table2 . * 
FROM 
    Table1 AS table1
LEFT JOIN 
    Table2 AS table2
USING (
    col1
)
WHERE 
    table1.col1 !=  '5324372'
AND 
    table1.col2 LIKE  'S'
AND (
    table1.col3 LIKE  'I'
    OR table1.col3 LIKE  'V-G'
)
AND (
    table2.col1 =  'A'
    OR table2.col2 =  'B'
)
ORDER BY RAND( ) 
LIMIT 1

…which at the beginning worked fine, but as my Database began to grow, now takes several seconds to execute.

All columns used above are indexed, so it’s not an indexing problem.

From what I’ve researched, it’s due to the ORDER BY RAND() line which apparently runs very slow on large tables.

I’ve seen some potential solutions to this problem, but they are quite cryptic and I could not wrap my head around any of them in relation to my query above.

I’m actually surprised that MySQL does not have a built-in solution to this very slow ORDER BY RAND() problem as it would seem to me like it is a very basic need for many types of applications needing to select a random row.

So how can I optimize my query above so that I get the same/similar effect without the long execution times?

  • 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-26T13:10:27+00:00Added an answer on May 26, 2026 at 1:10 pm

    You are choosing only one row , I think you can choose one row randomly by LIMIT randomly. Example:

    If you have:

    SELECT * table  ORDER BY RAND( )  LIMIT 1;
    

    change it to

    SELECT * table    LIMIT $randomvalue  ,$randomvalue + 1;
    

    $randomvalue is a random value chosen from the application level.

    update: the following answer is more clear than the above one.

    //get the total number of rows
    $result= mysql_query(" SELECT  COUNT(*) AS total FROM `table` ");
    $row = mysql_fetch_array($result);
    $total=$row['total'];
    
    
    //create random value from 1 to the total of rows 
    $randomvalue =rand(1,$total);
    
    
    //get the random row
    
    $result= mysql_query(" SELECT  * FROM `table` limit $randomvalue,1");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hopefully this is simple: I have a relatively long list where each list item
I'm trying to join two relatively simple tables together, but my query is experiencing
I have a relatively simple pivot/transform query, which appears as follows: TRANSFORM Sum(T1.VALUE) As
I hope this is a relatively easy problem although I have spent hours websearching
I have a web service, defined(WSDL) and implemented in PHP. This one is relatively
I have a relatively complex .htaccess file to control page requests, this currently redirects
I can't get my head around why this isn't working.. I have a relatively
I have a relatively simple ASP.net MVC 2 app that is using SubSonic. Everything
I have a relatively complex query, with several self joins, which works on a
I have a query that looks something like this: select xmlelement(rootNode, (case when XH.ID

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.