RE: Rand() Emulation

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



ORDER BY RAND() is not very performance wise.
Try to do the rand outside of the DB and get your random record 
with that number.

Sincerely

berber

Visit http://www.weberdev.com/ Today!!!
To see where PHP might take you tomorrow.


-----Original Message-----
From: Gerard Samuel [mailto:gsam@trini0.org] 
Sent: Tuesday, June 24, 2003 6:19 PM
To: Doug Thompson
Cc: Becoming Digital; PHP-DB
Subject: Re:  Rand() Emulation


Im probably not making myself clear.
Ultimately, my goal is to emulate mysql's ->
SELECT * FROM TABLE ORDER BY RAND() LIMIT (X)

for other databases that do not support RAND().  So using variations of 
php's rand(), wouldn't make sense,
as it only picks one value out of a range of values, that are numerical 
in nature.

But here is an idea, that Im thinking about, but haven't gotten to the 
code as yet.
1.  In the tables that I want random values from, create a "rand" 
column, that contains incremental numerical values for each row. 2.
select the the maximum number from the rand column. 3.  Assume that
there are no gaps between 0 and this max number. 4.  Create an array of
numbers with values between 0 and max number. 5.  Use array_rand() to
"randomly" choose (x) values from the array 
created in step 4.  (I may choose maybe
a 1 or 2 values more than whats required, just in case of gaps between 0

and max number in step 3).
6.  Use these randomly choosen values to select from the database as 
random rows.

So hopefully its a bit clearer what Im striving for.
To me the idea above would work, but it hinges on if that rand column, 
doesn't have gaps.
If you see room for improvement, or have another idea, or if Im talking 
gibberish then by all means.

Thanks.

Doug Thompson wrote:

>An incredible interpretation of
>
><quote>
>If called without the optional min, max arguments rand() returns a 
>pseudo-random value between 0 and RAND_MAX. If you want a random number
between 5 and 15 (inclusive), for example, use rand (5, 15).
></quote>
>
>Doug
>


-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux