开发者

Fastest way to get random row from mySQL

开发者 https://www.devze.com 2023-03-09 02:51 出处:网络
I have InnoDB table with millions of records. I need to find the way to get random row as fast as i can.

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

精彩评论

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