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:[EMAIL PROTECTED] On Behalf Of Joshua Beall
Sent: Monday, December 06, 2004 12:40 PM
To: [EMAIL PROTECTED]
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/[EMAIL PROTECTED]


--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:    http://lists.mysql.com/[EMAIL PROTECTED]

Reply via email to