Skip to content Skip to sidebar Skip to footer

Rand() Query And Performance

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 sugge

Solution 1:

Instead of using the RAND() use a random number in the offset for the LIMIT in 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.

Solution 2:

Instead of the in subquery, try an exclusive left join? That way, MySQL knows it doesn't have to scan the video table twice:

SELECT  video.* 
,       video.wins / video.loses AS win_loss_ratio
FROM    video
LEFT JOIN    
        video_log as vidlog
ON      vid.videoid = vidlog.videoid
WHERE   vidlog.videoid is null
        AND video.round =0ORDERBY 
        RAND()
LIMIT   0 , 2

Post a Comment for "Rand() Query And Performance"