MySQL vs PHP when retrieving a random item

Posted by andufo on Stack Overflow See other posts from Stack Overflow or by andufo
Published on 2010-03-31T15:33:34Z Indexed on 2010/03/31 15:53 UTC
Read the original article Hit count: 344

Filed under:
|

Hi, which is more efficient (when managing over 100K records):

A. Mysql

SELECT * FROM user ORDER BY RAND();

of course, after that i would already have all the fields from that record.

B. PHP

use memcached to have $cache_array hold all the data from "SELECT id_user FROM user ORDER BY id_user" for 1 hour or so... and then:

$id = array_rand($cache_array);

of course, after that i have to make a MYSQL call with:

SELECT * FROM user WHERE id_user = $id;

so... which is more efficient? A or B?

© Stack Overflow or respective owner

Related posts about php

Related posts about mysql