I've read about a few alternatives to MySQL's ORDER BY RAND()
function, but most of the alternatives apply only to where on a single random result is needed.
Does anyone have any idea how to optimize a query that returns multiple random results, such as this:
SELECT u.id,
p.photo
FROM users u, profiles p
WHERE p.memberid = u.id
AND p.photo != ''
AND (u.ownership=1 OR u.stamp=1)
ORDER BY RAND()
LIMIT 18
Order by rand()
is very slow on large tables,I found the following workaround in a php script:
Then do random in php
Then
Seems to be quite fast....