2

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 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 ?

artyomboyko
  • 2,781
  • 5
  • 40
  • 54

0 Answers0