Boofo
05-15-2010, 05:30 PM
I have been researching this on the net and ORDER BY RAND is supposed to be bad to use. I can across another way to get a random item from the db, but it uses a sub-query. From what I have read, this query is faster and less server intensive on bigger tables.
$quote=$db->query_first("
SELECT quote,name
FROM " . TABLE_PREFIX . "quote
WHERE quoteid = ROUND(".lcg_value()." * (SELECT COUNT(*) FROM " . TABLE_PREFIX . "quote))
LIMIT 1
");
This is the code that I was using before:
$quote=$db->query_first("
SELECT quote,name
FROM " . TABLE_PREFIX . "quote
ORDER BY RAND(NOW())
LIMIT 1
");
My quotes table is getting bigger all the time as I add quotes to it. Is the top query (with the sub-query) actually better than the old RAND query? Or is there an even better way to do it now?
$quote=$db->query_first("
SELECT quote,name
FROM " . TABLE_PREFIX . "quote
WHERE quoteid = ROUND(".lcg_value()." * (SELECT COUNT(*) FROM " . TABLE_PREFIX . "quote))
LIMIT 1
");
This is the code that I was using before:
$quote=$db->query_first("
SELECT quote,name
FROM " . TABLE_PREFIX . "quote
ORDER BY RAND(NOW())
LIMIT 1
");
My quotes table is getting bigger all the time as I add quotes to it. Is the top query (with the sub-query) actually better than the old RAND query? Or is there an even better way to do it now?