List:General Discussion« Previous MessageNext Message »
From:Joshua Beall Date:December 6 2004 8:59pm
Subject:RE: Selecting a random row
View as plain text  
> How many rows do you anticipate that the table will have? Have you
> tested this on your server? I would have to bet that if you 
> have only a few K rows that speed/performance will not be an issue.

I doubt I will have more than 100.  Perhaps I shouldn't worry about it,
then.

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

Jfyi the book I am referring to is "MySQL" by Michael Kofler.  I'd give
the Amazon link but they appear to be down at the moment.  The ISBN is
1-893115-57-7 for any who want to look it up elsewhere/when amazon.com
comes back up.

  -jb

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