// you’re reading...

Featured

Get Random Row with MySQL Without ORDER BY RAND()

This is an update to a previous post of mine which uses the RAND() method. Using the following code, you can retrieve a random row much, much faster (MySQL 4.1.x/5.0.x), with thanks to Jan Kneschke:

SELECT <COLUMN> FROM <TABLE> AS r1
JOIN (SELECT ROUND(
  RAND( ) * (
    SELECT MAX( id ) FROM <TABLE>)
  ) AS id
) AS r2
WHERE r1.id >= r2.id
ORDER BY r1.id ASC
LIMIT 1;

Replace:

  • <COLUMN> with the name of the column(s) you wish to retrieve
  • <TABLE> with the name of the table you wish to retrieve the data from

I've tested this with a table with over 660,000 records, and got a response in 0.0200 seconds, whereas with ORDER BY RAND() i got a response in 2.1599 seconds.

Total Number of Rows:

Cardinality

Old Method:

Old Method

New Method:

New Method

Discussion

9 comments for “Get Random Row with MySQL Without ORDER BY RAND()”

  1. Are each of these queries from a cold start of MySQL?

    Posted by Techwolf | March 5, 2008, 2:08 pm
  2. Yes, the server was restarted before each of the queries were executed.

    Posted by Ed | March 5, 2008, 4:07 pm
  3. Hi Ed,

    This is a much better solution. :)

    It is worth noting, however, that it has a significant caveat: it assumes that you don't have any significant gaps in your IDs. If you have any gap (even of 1 record) the probability of returning the record following it is anywhere from a little, to substantially higher.

    In this model, the probability of returning any given record is:

    1 + [preceeding gap] / [max id]

    whereas, the optimal probability to return any given record is:

    1 / [record count]

    Regards,

    Jeremy

    Posted by Jeremy Cole | March 7, 2008, 9:55 pm
  4. ummm i thought this was supposed to NOT use the rand() function? lol i see it in there still.

    Posted by sunjester | March 13, 2008, 4:11 am
  5. Haha, you're right. I will change the title accordingly. It should say 'Without ORDER BY Rand()'.

    Thanks!

    Posted by Ed | March 13, 2008, 9:32 am
  6. Nice info, keep up the good work :)

    Posted by amol | April 16, 2008, 8:17 pm
  7. problem if your id column is missing some values

    Posted by amol | April 16, 2008, 11:16 pm
  8. Hi Amol,

    This example does assume that you're using the ID row without any missing values. An ID row is usually set to auto increment, and as such will never have missing values.

    Thanks!

    Posted by Ed | April 17, 2008, 9:00 am
  9. Hi Ed,

    Yes it true that an ID row is usually set to auto increment but we cannot guarantee that it won't have missing values due to the fact that some admins might need to delete some rows and auto increment doesn't go back and fill numbers into those missing rows.

    I have one more query, if you could help I would be very glad. My query is that how can we retrieve a random row different for each day, like in "quote of the day", "download of the day", etc.

    Thanks and Regards,
    Amol

    Posted by amol | April 17, 2008, 10:59 pm

Post a comment

Most Emailed