NogDog wrote:Just a thought I had, no idea if it would perform any better:
Do a SELECT COUNT(*) to get the number of records
Do a <?php $offset = rand(0, $count - 1); ?> where $count comes from the above query.
Do a SELECT * FROM table LIMIT 1 OFFSET $offset
I have no empirical evidence how this would perform versus the ORDER BY RAND() method, but figured I'd throw it out there in case you want to try it.
Tried it before. It's halfway between order by rand() and my method in terms of performance. Problem is that the db still has to generate the whole data set before hitting the offset and stopping, so the higher the number, the longer it will take to return.
Note that select count(*) is NOT fast in most databases (Oracle, PostgreSQL, MySQL with innodb tables) when the tables are very large.
for instance, on the table listed above, hitting the last month's data, Here's what I go for varying offsets, in milliseconds
1: 1.3
100: 3.5
1000: 4.4
10,000: 23
100,000: 210
500,000: 1280
1,000,000: 3000
You can see where that's heading. At 40+ million rows, it's not gonna scale.
COUNT() pretty much has to traverse through the entire table, right?
laserlight, yes count() has to traverse the whole table in many databases, such as postgresql and oracle and mysql with innodb tables...