List:General Discussion« Previous MessageNext Message »
From:Dathan Pattishall Date:December 6 2004 10:28pm
Subject:RE: Selecting a random row
View as plain text  
Pseudo code: 

$min = SELECT MIN(id) from fortunes;
$max = SELECT MAX(id) from fortunes;

While (!$row && $count < 3) {
  
   $id = rand $max + $min;
   if ($id > $max) {
      next;
   }
  
  $row = SELECT * from fortunes where id = $id;
  $count++
}
 
If ($count >= 3) {
   return 1st row;
}


-----Original Message-----
From: news [mailto:news@stripped] On Behalf Of Joshua Beall
Sent: Monday, December 06, 2004 12:40 PM
To: mysql@stripped
Subject: Selecting a random row

Hi All,

I understand that I can get a random row out of a table by doing
something like

SELECT * FROM fortunes ORDER BY RAND() LIMIT 1

But I have also been told that this is a very slow operation.

I am building a script that will display a random saying, user
testimonial, whatever, on a web page.  Since this is a public page
(i.e., not an admin backend), I have to be concerned about speed.  What
is the best way to get a random row out of a database for this sort of
application?  It's the sort of thing you see all the time, so I'm sure
others have thought about this before.  Any pointers?

Sincerely,
  -Josh 




--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:
http://lists.mysql.com/mysql?unsub=1

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