I’m trying to prepare a query for performance. I’m hoping to remove the RAND() from the query below and replace it with a better performing alternative. Does anybody have any suggestions?
SELECT video.*,
video.wins / video.loses AS win_loss_ratio
FROM video
WHERE video.videoid NOT IN (SELECT vidlog.videoid
FROM video AS vid,
video_log AS vidlog
WHERE vid.videoid = vidlog.videoid)
AND video.round = 0
ORDER BY RAND(), win_loss_ratio DESC
LIMIT 0, 2
Thanks!
Instead of using the RAND() use a random number in the offset for the
LIMITin the language that is calling this query. That could have two problems though a. you need to know how many items are in the database, 2. only yield something random for one of the items though. Other option is to drop the LIMIT and RAND() altogether and just select random items when the query is returned (I would imagine this to be slower though)Is there a real reason why you need to have this very high in performance? I’m pretty sure using RAND() will be sufficient enough for this kind of query.