2

According to this link, ORDER BY RAND() is innefiencient as hell

With that in mind, how do I optimize a random row query onto mysql if my Id (Primary key) is not consecuitive (AKA, i cant just do rand(1, max())?

3
  • Perhaps you can select the "next greatest" and LIMIT 1? i.e. instead of =rand(1,$max_id), use >= rand(1,$max_id) LIMIT 1. Sep 3, 2012 at 20:13
  • Thats the current leader, anyone else have a good idea? (then again, it does require a query for max(id))
    – Jay
    Sep 3, 2012 at 20:15
  • Note that you'll also need an ORDER BY id ASC for this, otherwise it won't repect the rand() as much as you want it to. Sep 3, 2012 at 20:17

5 Answers 5

4

The best way to get your table randomly sorted is to add an extra field and populate it with a arbitrary md5 hashes, and create an index on that field.

These hashes can be the hash of anything you like, as long as they're all different. I'd suggest hashing the primary key ID field, plus an arbitrary salt string.

UPDATE myTable SET rand_hash = md5(concat(id,'anything here'))

With these in place, you will have a pretty much completely random sort order for your table. You can query the table at a random point by creating another arbitrary md5 hash, and querying the record nearest that value. MD5 hashes are randomly distributed, so every record would have the same chance of being picked. Something like this would do the trick:

SELECT * FROM myTable WHERE rand_hash >= md5(now()) LIMIT 1

The best bit is that this would be querying on an index, so would be lightning fast, no matter where in the table the record is.

Hope that helps.

2
  • I already have a id that acts as an index, wouldnt it take up less db space to just use SELECT * FROM myTable WHERE index >= random() LIMIT 1
    – Jay
    Sep 3, 2012 at 21:20
  • 1
    You'd need the random() to know the min and max IDs. Also, if you have any gaps in the ID sequence (eg from deleted records), then you won't get random distribution. And also, with the md5 solution, the next record in the sequence will also be random, so if you need more than one, you can just increase the limit rather than querying multiple times.
    – Spudley
    Sep 3, 2012 at 21:27
0

You can ORDER BY id ASC, then select the either the rand() row or the next higher one:

...
WHERE id >= rand(1, $max_id)
ORDER BY id ASC
LIMIT 1
3
  • Why bother with the ORDER BY?
    – eggyal
    Sep 3, 2012 at 22:49
  • Because MySQL makes no guarantees about the order in which it scans your table. Without the order by, you might get the same row several times in a row if you executed the query several times in a row. Sep 4, 2012 at 0:23
  • You might, but with the same probability as rand() being equal to that row's id each time no?
    – eggyal
    Sep 4, 2012 at 4:10
0

You can use mysql_fetch_array() random no of times

$query=sprintf("SELECT * FROM acb");
$check=mysql_query($query);
for($i=1,$i<=rand($min_id,$max_id,$i++)
$row=mysql_fetch_array($check, MYSQL_ASSOC);

Now $row contains a random row Note: This method seems very inefficient to me

0

You can use mysql_data_seek(resource $result, int $row_number) in php instead.

$result = mysql_query( 'SELECT * FROM `table`' );
mysql_data_seek( $result , mt_rand( 0 , mysql_num_rows( $result ) ) );

But mysql_data_seek and mysql_num_rows do not work with unbuffered queries.

0

I tried the following query. It is faster than ORDER BY RAND ():

SELECT * FROM table WHERE (id >= RAND() * (SELECT MAX(id) FROM table) LIMIT 1;

If the column "id" is auto_increment, I tested this query is much faster than before:

SELECT * FROM table WHERE (id >= RAND() * (SELECT id FROM table ORDER BY id DESC LIMIT 1)) LIMIT 1;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.