我有InnoDB表,有数百万条记录。我需要找到尽可能快地获得随机行的方法。
我的解决方案#1是:
SELECT * FROM keywords T JOIN (SELECT FLOOR(MAX(kid)*RAND()) AS ID) AS x ON T.kid >= x.ID ORDER BY T.kid ASC LIMIT 1
我的解决方案#2是:
$max = SELECT max(kid) FROM keywords
SELECT * FROM keywords T JOIN (SELECT FLOOR($max*RAND()) AS ID) AS x ON T.kid >= x.ID ORDER BY T.kid ASC LIMIT 1
因此,解决方案#2速度太快,但它由2个查询组成。 有没有办法在一个请求中制作它?