NYCPHP Meetup

NYPHP.org

[nycphp-talk] Random record from MySQL

Jeff Siegel jsiegel1 at optonline.net
Thu Mar 11 13:07:57 EST 2004


I'll definitely use the LIMIT clause. After all, I only need one record. 
In addition, I'll have some parameters in a where clause so that if it 
comes back with a previously used record (I'm tracking that info)...I'll 
rerun/regenerate the query. Probably just do it in a loop and then exit 
out of the loop once I've found an unused record.

Jeff
-----

David Sklar wrote:

> Ophir Prusak wrote:
> 
>>
>> I've never seen this solution before. Very simple.
>> I wondering though about the efficiency of this solution.
>>
>> It seems to me that this assigns a random value to each row and then 
>> orders by that row.
>> Does the DB do a full table scan using this method?
>>
>> If yes, this would be a very inefficient method for very large tables.
> 
> 
> It is inefficient if you are retrieving the whole table ordered by 
> RAND(), but with the LIMIT clause, the table scan is short-circuited 
> after one row is found. So,
> 
> SELECT * FROM table ORDER BY RAND()
> 
> can be slow if there are a lot of rows in the table, but
> 
> SELECT * FROM table ORDER BY RAND() LIMIT 1
> 
> is OK.
> 
> David
> 
> 
> 
> _______________________________________________
> talk mailing list
> talk at lists.nyphp.org
> http://lists.nyphp.org/mailman/listinfo/talk
> 




More information about the talk mailing list