[nycphp-talk] Need help randomly accessing records from a database
Dan Cech
dcech at phpwerx.net
Thu Sep 1 10:00:09 EDT 2005
Cliff Hirsch wrote:
> I need to randomly access records from a database and have come up with
> the following solution:
After I posted my last response I realised something I'd forgotten to
mention.
For result sets larger than a few thousand rows you will most likely get
better mileage out of something like:
$sql = 'SELECT COUNT(*) FROM `sometable`';
$cnt = $db->getOne($sql);
$rnd = mt_rand(0,$cnt - 1);
$sql = 'SELECT * FROM `sometable` LIMIT '. $rnd .',1';
$row = $db->getRow($sql);
My testing on a table with 50,000 rows indicated that this method was
twice as fast on the first run and a massive 10x as fast for subsequent
runs.
Dan
More information about the talk
mailing list