Jump to content

Removing RAND()


dreamwest

Recommended Posts

I had to remove rand from my queries because my database is over 3 million rows and it was serioulsly affecting the server

 

Is it possible to compress this alternate rand query into one line aka one mysql_query():

 


$offset_result = mysql_query( " SELECT FLOOR(RAND() * COUNT(*)) AS `offset` FROM `video` ");
$offset_row = mysql_fetch_object( $offset_result ); 
$offset = $offset_row->offset;
$result3 = mysql_query( " SELECT * FROM `video` WHERE approve='1' and `show`='1'  LIMIT $offset, 10 " );

Link to comment
https://forums.phpfreaks.com/topic/193016-removing-rand/
Share on other sites

I just understand now one thing u should substract from rows count the number of items u want to display like so:

$offset_result = mysql_query("SELECT id  FROM `video`");

$offset = floor((rand() / getrandmax()) * (mysql_num_rows($offset_result)-10)); //just to be sure that u will always be able to retrive 10 videos from offset

Link to comment
https://forums.phpfreaks.com/topic/193016-removing-rand/#findComment-1016524
Share on other sites

Thanks.

 

Problem is it doesnt reduce the amount of code for the query/ies and ill still have 3 queries. I was trying to reduce it to one combined query thus creating less code, easier to remember & less cluttered apps - it may be possible with subqueries, ill have to brush up on that

 

 

Link to comment
https://forums.phpfreaks.com/topic/193016-removing-rand/#findComment-1016561
Share on other sites

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.