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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.