Created
October 10, 2011 13:45
-
-
Save mfr/1275370 to your computer and use it in GitHub Desktop.
MySQL selecting random rows fast
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT * | |
FROM ( | |
SELECT @cnt := COUNT(*) + 1, | |
@lim := 10 | |
FROM t_random | |
) vars | |
STRAIGHT_JOIN | |
( | |
SELECT r.*, | |
@lim := @lim - 1 | |
FROM t_random r | |
WHERE (@cnt := @cnt - 1) | |
AND RAND() < @lim / @cnt | |
) i |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment