Re: Random PostreSQL

From: Date: Tue, 01 Aug 2000 20:34:01 +0000
Subject: Re: Random PostreSQL
References: 1  Groups: php.db 
Request: Send a blank email to php-db+get-1684@lists.php.net to get a copy of this message
My first question is "why"? Most people are tearing hair out trying to get sets of records in particular order. Anyway, this ain't code, just an idea, and it relies on the randomness of PHP's random number generator. Figure out a way to use randomly generated values to match one of your keys. May be you have a sequence key which is an integer - in which case you're almost home free. In any case, make certain that the key you are selecting against is indexed, or this will be as slow as houses. Seed the generator srand() Find the maximum value you can retrieve For the number of records you want to retrieve select fields from table where numeric_key_field = rand (1, maximum_value) store result in an array end for Then do what you will with the array. Another way, and I'll admit I haven't tried this with any database I've used, would be to use a loop to store the set of random numbers in a variable, say ranset, and run this query select fields from table where numeric_key_field in ranset If that worked, then you would have the normal array returned from a query by php. Would it be faster? I don't know, it would depend on how PGSQL's query analyzer evaluates the "in" statement. A bunch of separate queries against a key field may run much faster than an in statement. If you were starting from scratch I guess you could set up a random_id field, and then select against it using the rand() function. Anyway, do a bunch of testing to ensure that you are getting random results. You will notice I've left out a bunch of PHP syntax, mostly because it's pretty new to me, and I wouldn't want to suggest syntax which might be completely unworkable. I've also left off any type conversions, as these would depend on the type of the key fields. Hope this helps - Miles Thompson joe@jWebMedia.com wrote: > Anyone know of a simple way to select a random group of records from > postgeSQL? Can't find anything in the manual that is of any help to me. > Thanks, > > Joe > > -- > PHP Database Mailing List (http://www.php.net/) > To unsubscribe, e-mail: php-db-unsubscribe@lists.php.net > For additional commands, e-mail: php-db-help@lists.php.net > To contact the list administrators, e-mail: php-list-admin@lists.php.net

« previous php.db (#1684) next »