开发者

Fastest way to get random row from mySQL

I have InnoDB table with millions of records. I need to find the way to get random row as fast as i can.

My solution #1 was:

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

My solution #2 is:

$max = SELECT max(kid) FROM keywords
SELECT * FROM keywords T JOIN (SELECT FLOOR($max*RAND()) AS ID) AS x ON开发者_如何学C T.kid >= x.ID ORDER BY T.kid ASC LIMIT 1

So, solution #2 is way too faster, but it made of 2 queries. Is there a way to make it in one request ?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜