need a alternative to ORDER BY RAND()
Posted: Mon Mar 07, 2011 5:49 pm
i came across a link talking about how bad
One of the main lines of code at my site is the above, so i decided to do a little research to see why this was bad. After a little reading i found out that its ridiculous on the server when you have a large database almost taking 132s to select a random results from 5000 rows.
My site has a feature that allows users to submit links to the database and as it gets popular im sure several thousands of links will be stored resulting in a MAJOR issue. So before it even gets to this point i would like to nip it in the butt and just use another method if possible.
- Code: Select all
ORDER BY RAND()
- Code: Select all
$result = mysql_query("SELECT * FROM slinks where approval='1' ORDER BY RAND() LIMIT 1") or
die(mysql_error());
One of the main lines of code at my site is the above, so i decided to do a little research to see why this was bad. After a little reading i found out that its ridiculous on the server when you have a large database almost taking 132s to select a random results from 5000 rows.
My site has a feature that allows users to submit links to the database and as it gets popular im sure several thousands of links will be stored resulting in a MAJOR issue. So before it even gets to this point i would like to nip it in the butt and just use another method if possible.