List:General Discussion« Previous MessageNext Message »
From:Jay Blanchard Date:December 6 2004 9:06pm
Subject:RE: Selecting a random row
View as plain text  
[snip]
> The way to
> enhance this is by selecting an indexed value, such as the following
> where `foo` is indexed
> 
> SELECT `foo` FROM fortunes ORDER BY RAND() LIMIT 1

Why does this help?  From the MySQL book I have, the reason ORDER BY
RAND() is slow is because "for each record in the table a random number
must be generated.  Then all random numbers are sorted so that the first
n records can be returned.

Is this correct?  If so, how does selecting an indexed column help this,
won't it still need to perform all those operations (generate rand-nums,
then sort) regardless of the index?
[/snip]

Using indexes for selection criteria does have an impact, even with
RAND. On a table such as yours it shouldn't matter due to the small
size. 
Thread
Selecting a random rowJoshua Beall6 Dec
RE: Selecting a random rowJay Blanchard6 Dec
  • RE: Selecting a random rowJoshua Beall6 Dec
RE: Selecting a random rowJay Blanchard6 Dec
RE: Selecting a random rowDathan Pattishall6 Dec