Jump to content

need a alternative to ORDER BY RAND()


brentmoeller

Recommended Posts

i came across a link talking about how bad

ORDER BY RAND()

is.

 

Since

$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.

Link to comment
Share on other sites

I'm not sure by try running some tests on this..

 

SELECT * FROM slinks where approval='1' AND id IN (select t.id FROM slinks t ORDER BY RAND() LIMIT 5) 

 

My code assumes you have an indexed variable called "id" and that is your key.  Also I would avoid using "SELECT *".. ideally only select what you need instead of all columns.

Link to comment
Share on other sites

Without testing it, yeah it looks fine

 

Don't use * though...it's good practice to name your fields implicitly

 

And define your sql in another variable, it's cleaner and makes for easier testing

 

like this:

$sql = "Your sql";
$res = mysql_query($sql) or die(mysql_error());

Link to comment
Share on other sites

thanks dev

I have another related question. 

$result = mysql_query("SELECT * FROM slinks where approval='1' ORDER BY RAND() LIMIT 1") or
    die(mysql_error());

or

$result = mysql_query("SELECT * FROM slinks WHERE id >= FLOOR( RAND( ) * ( SELECT MAX( id ) FROM slinks ) )AND approval='1' ORDER BY id ASC LIMIT 1")or die(mysql_error());

 

The code dont reproduce that good of random results. Both lines of code  produce the same random results often. The code selects the same urls back to back often then it may select a different url and then go back to the same url it already randomly picked 2 times in a role. So i guess the question is how can i keep the query from selecting the same urls over and over and back to back?

 

Link to comment
Share on other sites

Ok i have figured out what code i want to run and its

$result = mysql_query("SELECT count(*) FROM slinks where approval='1'");  
$do = mysql_fetch_row($result);  
$random = mt_rand(0,$do[0] - 1);  
$result = mysql_query("SELECT * FROM slinks LIMIT $random, 1");  

 

now all i need to figure out with a little help from the great users at phpfreaks is how i can get the random site to only be ran one time per user.

 

example

click = randomsite1.com

click = randomsite2.com

click = randomsite3.com

click = randomsite4.com

click = randomsite5.com

click = randomsite5.com = <--SKIP done been called so lets move along

click = randomsite6.com

 

 

I guess somehow im going to have to store the urls that are loaded into a variable and compare the variable to the the random site to make sure we dont get a match.

then again i guess i would have to some how figure out how to store the data per unique user as well .

 

Any suggestions would be greatly appreciated

Link to comment
Share on other sites

I guess somehow im going to have to store the urls that are loaded into a variable and compare the variable to the the random site to make sure we dont get a match.

 

That's a reasonable start, and I've done it like that, but follow the link in the sticky. There's an article that talks about fixing holes and whatnot...might be some usefull info that you can use for your purpose.

 

then again i guess i would have to some how figure out how to store the data per unique user as well .

 

Not sure what you mean

 

Luck

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.