I am wondering if anyone has ever come across this problem before. I have a table with 10,000 rows and growing and what I am trying to do is grab a random set of 50 of those rows from the DB. I have been thinking about how I would solve that problem for a while. I run into a problem when I have to take into consideration that some of the rows have been deleted so I can not just choose 50 random numbers between 1 and 10,000. Anyone have a clean solution to my problem? :-) Your input is greatly appreciated!
This might provide some guidance: http://wiki.rubyonrails.com/rails/show/HowtoSelectRandomRecords On 5/22/05, John Kopanas <john.kopanas-O1KSuMybMhqBUy7/sJONFg@public.gmane.org> wrote:> I am wondering if anyone has ever come across this problem before. I > have a table with 10,000 rows and growing and what I am trying to do > is grab a random set of 50 of those rows from the DB. I have been > thinking about how I would solve that problem for a while. > > I run into a problem when I have to take into consideration that some > of the rows have been deleted so I can not just choose 50 random > numbers between 1 and 10,000. > > Anyone have a clean solution to my problem? :-) > > Your input is greatly appreciated! > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
On Monday, May 23, 2005, 5:53:27 AM, John wrote:> I am wondering if anyone has ever come across this problem before. I > have a table with 10,000 rows and growing and what I am trying to do > is grab a random set of 50 of those rows from the DB. I have been > thinking about how I would solve that problem for a while.> I run into a problem when I have to take into consideration that some > of the rows have been deleted so I can not just choose 50 random > numbers between 1 and 10,000.If deleted rows are rare, you could try selecting random IDs, and ignore exceptions until you get your 50 records. Gavin
Thanks a lot everyone. This worked out perfectly. On 22-May-05, at 4:53 PM, Michael Campbell wrote:> This might provide some guidance: > http://wiki.rubyonrails.com/rails/show/HowtoSelectRandomRecords > > On 5/22/05, John Kopanas <john.kopanas-O1KSuMybMhqBUy7/sJONFg@public.gmane.org> wrote: > >> I am wondering if anyone has ever come across this problem before. I >> have a table with 10,000 rows and growing and what I am trying to do >> is grab a random set of 50 of those rows from the DB. I have been >> thinking about how I would solve that problem for a while. >> >> I run into a problem when I have to take into consideration that some >> of the rows have been deleted so I can not just choose 50 random >> numbers between 1 and 10,000. >> >> Anyone have a clean solution to my problem? :-) >> >> Your input is greatly appreciated! >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> >> > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
This is a common problem, and the following is probably not what you''re looking for, since it''s not very fast/clean: SELECT FROM ....... ORDER BY RAND() LIMIT 50 I have never heard of other, clean solutions. If anyone has, I''d be happy to know Robert> I am wondering if anyone has ever come across this problem before. I > have a table with 10,000 rows and growing and what I am trying to do > is grab a random set of 50 of those rows from the DB. I have been > thinking about how I would solve that problem for a while. > > I run into a problem when I have to take into consideration that some > of the rows have been deleted so I can not just choose 50 random > numbers between 1 and 10,000. > > Anyone have a clean solution to my problem? :-) > > Your input is greatly appreciated! > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > . >