note 26418 deleted from function.rand by felipe
| From: | felipe@php.net | Date: | Sun, 09 Dec 2007 15:38:18 +0000 |
| Subject: | note 26418 deleted from function.rand by felipe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-133022@lists.php.net to get a copy of this message | ||
Note Submitter: igge at netpond dot com
----
About selecting random rows from a MySQL table:
SELECT * FROM tablename ORDER BY RAND() LIMIT 1
works for small tables, but once the tables grow larger than 300,000 records or so this will be very
slow because MySQL will have to process ALL the entries from the table, order them randomly and then
return the first row of the ordered result, and this sorting takes long time. Instead you can do it
like this (atleast if you have an auto_increment PK):
SELECT MIN(id), MAX(id) FROM tablename;
Fetch the result into $a
$id=rand($a[0],$a[1]);
SELECT * FROM tablename WHERE id>='$d' LIMIT 1