List:General Discussion« Previous MessageNext Message »
From:Philip Hallstrom Date:May 16 2005 5:12pm
Subject:Re: Efficiently finding a random record
View as plain text  
> 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 have gaps in the 
> IDs before them will be twice, three times, etc. (depending on the size of 
> the gap), as likely to be selected as records with no preceding gaps.

Replace MAX with COUNT and the WHERE clause with an OFFSET and the gap 
problem should go away...

-philip
Thread
Efficiently finding a random recordBrian Dunning13 May
  • Re: Efficiently finding a random recordFrank Bax13 May
    • Cumulative TotalsRussell Horn25 May
      • Re: Cumulative TotalsDan Bolser25 May
        • Re: Cumulative Totalsdoug27 May
          • Re: Cumulative TotalsFrederik Eaton10 Jul
            • Re: Cumulative TotalsGabriel PREDA10 Jul
      • Re: Cumulative TotalsBrent Baisley25 May
    • Re: Cumulative TotalsRhino25 May
      • Re: Cumulative Totalsmfatene25 May
        • Re: Cumulative Totalsmfatene25 May
  • Re: Efficiently finding a random recordPhilip Hallstrom13 May
    • Re: Efficiently finding a random recordEric Bergen13 May
      • Re: Efficiently finding a random recordDan Bolser14 May
        • Re: Efficiently finding a random recordEric Bergen14 May
          • Re: Efficiently finding a random recordMichael Stassen15 May
        • Re: Efficiently finding a random recordGary Huntress15 May
          • Re: Efficiently finding a random recordmfatene15 May
        • Re: Efficiently finding a random recordGary Huntress15 May
          • Re: Efficiently finding a random recordMichael Stassen15 May
            • Re: Efficiently finding a random recordKeith Ivey16 May
              • Re: Efficiently finding a random recordPhilip Hallstrom16 May