Jump to content

ORDER BY RAND() alternative


alwoodman

Recommended Posts

I have this query, its a bit more complex than what's written below but it returns over 1000 rows of data and on using ORDER BY RAND() is hundreds of time slower than the other queries running in the system. I want to remove the ORDER BY RAND() function which will return 3 results. I then want to run another query with a specific ID from the returned results set. Will this speed up the query as its not using ORDER BY RAND(). I guess what im trying to say is how do i randomly select a row from a results set?

 

mysql_select_db($database_system_conn, $system_conn);
$query_getFeaturedBusiness = "SELECT ID FROM Business WHERE Status = 1 ORDER BY RAND()";
$getFeaturedBusiness = mysql_query($query_getFeaturedBusiness, $system_conn) or die(mysql_error());
$row_getFeaturedBusiness = mysql_fetch_assoc($getFeaturedBusiness);
$totalRows_getFeaturedBusiness = mysql_num_rows($getFeaturedBusiness);

 

thanks

 

Lee

Link to comment
https://forums.phpfreaks.com/topic/146509-order-by-rand-alternative/
Share on other sites

mysql_select_db($database_system_conn, $system_conn);
$query_getFeaturedBusiness = "SELECT ID FROM Business WHERE Status = 1";
$getFeaturedBusiness = mysql_query($query_getFeaturedBusiness, $system_conn) or die(mysql_error());
$totalRows_getFeaturedBusiness = mysql_num_rows($getFeaturedBusiness);
mysql_data_seek($getFeaturedBusiness,rand(0,$totalRows_getFeaturedBusiness));
$row_getFeaturedBusiness = mysql_fetch_assoc($getFeaturedBusiness);

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.