MySQL Forums
Forum List  »  Performance

Re: MySQL rand() is slow on large datasets
Posted by: Jay Paroline
Date: January 18, 2008 12:25AM

The way to make that fast:

SELECT * FROM Table T JOIN (SELECT FLOOR(MAX(ID)*RAND()) AS ID FROM Table) AS x ON T.ID >= x.ID LIMIT 1;

It's not pretty but it is fast. The way Thomas suggested causes the MAX()*RAND() to be executed for every row comparison.

Options: ReplyQuote


Subject
Views
Written By
Posted
19185
July 24, 2007 08:19AM
7989
September 02, 2007 10:30AM
Re: MySQL rand() is slow on large datasets
10343
January 18, 2008 12:25AM


Sorry, you can't reply to this topic. It has been closed.
This forum is currently read only. You can not log in or make any changes. This is a temporary situation.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.