I've read that it isn't a good idea to run SQL queries with ORDER BY RAND() on large databases.
So here's my shot at breaking up the code. The code needs to select 10 random ids from the database, then do a second select to grab the random rows.
$sql = "SELECT id FROM table WHERE image != ''
ORDER BY id DESC LIMIT 50;";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
foreach($row as $key => $value)
{
$array[] = $value;
}
}
$rand_keys = array_rand($array, 10);
foreach($rand_keys as $value)
{
$rand_arr[] = $array[$value];
}
$rand_list = implode("," , $rand_arr);
$sql = "SELECT image FROM table
WHERE image != ''
AND id IN ($rand_list)";
$result = mysql_query($sql);
Any suggestions to speed up or simplify?