Re: Efficiently finding a random record

2005-05-16 Thread Keith Ivey
Michael Stassen wrote: For example, if the selected random id is missing, we take the next id we find, like this: SELECT @rand_id:= CAST( 1 + MAX(id)*RAND() AS UNSIGNED) FROM history; SELECT * FROM history WHERE id = @rand_id LIMIT 1; That will have a possibly undesired effect. Records that

Re: Efficiently finding a random record

2005-05-16 Thread Philip Hallstrom
Michael Stassen wrote: For example, if the selected random id is missing, we take the next id we find, like this: SELECT @rand_id:= CAST( 1 + MAX(id)*RAND() AS UNSIGNED) FROM history; SELECT * FROM history WHERE id = @rand_id LIMIT 1; That will have a possibly undesired effect. Records that

Re: Efficiently finding a random record

2005-05-15 Thread mfatene
PROTECTED]; mysql@lists.mysql.com Sent: Friday, May 13, 2005 7:48 PM Subject: Re: Efficiently finding a random record in() can take millions of arguments. Up to max packet size. Try it :) Dan Bolser wrote: On Fri, 13 May 2005, Eric Bergen wrote: Even better is if you have an integer

Re: Efficiently finding a random record

2005-05-15 Thread Gary Huntress
: mysql@lists.mysql.com Sent: Sunday, May 15, 2005 6:42 AM Subject: Re: Efficiently finding a random record Hi, i did the test and agree with you. even with Select ID from history where id = 1+CAST( rand() * 19 as UNSIGNED) limit 1; when testing : select CAST( rand() * 19 as UNSIGNED

Re: Efficiently finding a random record

2005-05-15 Thread Michael Stassen
: [EMAIL PROTECTED] To: Gary Huntress [EMAIL PROTECTED] Cc: mysql@lists.mysql.com Sent: Sunday, May 15, 2005 6:42 AM Subject: Re: Efficiently finding a random record Hi, i did the test and agree with you. even with Select ID from history where id = 1+CAST( rand() * 19 as UNSIGNED) limit 1; when

Re: Efficiently finding a random record

2005-05-15 Thread Michael Stassen
Eric Bergen wrote: in() can take millions of arguments. Up to max packet size. Try it :) True, but several past threads have suggested that performance drops dramatically when the size of the IN list gets too large. As IN is equivalent to an equality check for each value in the list, separated

Re: Efficiently finding a random record

2005-05-14 Thread Gary Huntress
Bergen [EMAIL PROTECTED] To: Dan Bolser [EMAIL PROTECTED] Cc: Philip Hallstrom [EMAIL PROTECTED]; Brian Dunning [EMAIL PROTECTED]; mysql@lists.mysql.com Sent: Friday, May 13, 2005 7:48 PM Subject: Re: Efficiently finding a random record in() can take millions of arguments. Up to max packet size

Efficiently finding a random record

2005-05-13 Thread Brian Dunning
I have a db of about 300,000 records and when I try to find one random record like this: select * from table order by rand() limit 1; it can take several minutes. My Sherlock Holmes instincts tell me that what I'm doing is somehow inefficient. What is the primary culprit here? -- MySQL

Re: Efficiently finding a random record

2005-05-13 Thread Frank Bax
At 12:54 PM 5/13/05, Brian Dunning wrote: I have a db of about 300,000 records and when I try to find one random record like this: select * from table order by rand() limit 1; it can take several minutes. My Sherlock Holmes instincts tell me that what I'm doing is somehow inefficient. What is the

Re: Efficiently finding a random record

2005-05-13 Thread Philip Hallstrom
I have a db of about 300,000 records and when I try to find one random record like this: select * from table order by rand() limit 1; it can take several minutes. My Sherlock Holmes instincts tell me that what I'm doing is somehow inefficient. What is the primary culprit here? The culprit is

Re: Efficiently finding a random record

2005-05-13 Thread Eric Bergen
Even better is if you have an integer primary key (think auto_increment) and use in() So if you want 10 random rows in your app generate 20 or so random numbers and do something like select col1, from t where x in (1, 5, 3, 2...) limit 10 check num rows and if you don't get enough generate

Re: Efficiently finding a random record

2005-05-13 Thread Dan Bolser
On Fri, 13 May 2005, Eric Bergen wrote: Even better is if you have an integer primary key (think auto_increment) and use in() So if you want 10 random rows in your app generate 20 or so random numbers and do something like select col1, from t where x in (1, 5, 3, 2...) limit 10 check num rows

Re: Efficiently finding a random record

2005-05-13 Thread Eric Bergen
in() can take millions of arguments. Up to max packet size. Try it :) Dan Bolser wrote: On Fri, 13 May 2005, Eric Bergen wrote: Even better is if you have an integer primary key (think auto_increment) and use in() So if you want 10 random rows in your app generate 20 or so random numbers and