Selecting Random Rows from Large Table

Discussion in 'MySQL' started by gr8webseller, Jun 7, 2010.

  1. #1
    i want to select some random rows from table of more than 25000 rows,, i know that ORDER BY RAND() will take too much time,, please provide a easy method to get 20 random rows out of 25000 rows,,

    thank you
     
    gr8webseller, Jun 7, 2010 IP
  2. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    It would be easy if you use a SSL like php. You could just use php to generate a randome number between 0 and 25000 and the use that number plus twenty in your MySQL Query's LIMIT VALUE
     
    Imozeb, Jun 7, 2010 IP
  3. gr8webseller

    gr8webseller Peon

    Messages:
    1,097
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #3
    the index value won't be in order, there are lots of gaps like 1,2,4,5,7,8,14....... so here will be problem,,
     
    gr8webseller, Jun 7, 2010 IP
  4. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #4
    There's no way to do it outside of the database if the keys are not in order. You could select the keys and then run a random function in the application, but this could be slower. Probably worth a try.
     
    jestep, Jun 7, 2010 IP