Your query is basically this:
SELECT m.*
from moviedb.movie m JOIN
(SELECT RAND()*MAX(ID) as maxid
FROM movie
) mm
ON m.ID >= mm.max
WHERE m.year > 2004 AND m.year < 2015 AND m.imdbRating > 6.9 AND m.imdbvotes > 9999.9
ORDER BY m.id
LIMIT 1;
I suspect the performance variation is caused by the order by
. There may be little you can do to fix this, unless you can convince MySQL to use an index for the order by
. Such an index would be id, year, imdbrating, imdbvotes
. This is a covering index for the where
and order by
clauses, and MySQL might use it to avoid a filesort.
Another approach would be to introduce flags for the rating and vote counts. Then you could phrase the query as:
where m.year > 2004 and m.year < 2015 and RatingGreatFlag = 1 and LotsaVotesFlag = 1
Then an index on RatingGreatFlag, LotsVotesFlag, id, year
would probably be a big help. However, maintaining these flags would probably require using triggers.
EDIT:
As I think about this problem, I wonder if removing the order by
would still produce what you want. This would given an indeterminate row after the random id. There are definitely ways to choose such an indeterminate row that would be bad (such as the one with the highest id), but in practice it might work very well.