Peter Fitzgibbons
2005-Sep-12 20:30 UTC
Which is better : array#select or activerecord#find
Hello all, I have a table with 8000 rows. Certain selections ( @item_status #find(:all, :conditions...) ) return 550 rows. I have to iterate a separate list @items, matching @items[].name to @item_status[].name Hit rate on the match will be anywhere from 50%-100%. @items is an array of WIN32OLE items. So the question is this : Is it better to: A) Retrieve entire data result ( @item_status#find(:all, :conditions=>"foreign_key = ''blah''") ) and then use @item_status#select {|i| i.name =~ @item.name} B) Retrieve only (possibly nil) exact item ( @item_status#find(:all, :conditions=>"foreign_key = ''blah'' AND name = ''#{@item.name}''") C) Quit being nitpicky and don''t worry about an N^2 process across the LAN wire against 500 items ? D) Use the RUBY WAY and do this with some kind of tricky hash magic or something ? Thanks for the advice! Peter J. Fitzgibbons Applications Manager Lakewood Homes - "The American Dream Builder"(r) Peter.Fitzgibbons-STCS76aLmhk1y/cD6r8xzl6hYfS7NtTn@public.gmane.org (847) 884-8800 _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Michael Schuerig
2005-Sep-12 21:59 UTC
Re: Which is better : array#select or activerecord#find
On Monday 12 September 2005 22:30, Peter Fitzgibbons wrote:> Hello all, > > I have a table with 8000 rows. Certain selections ( @item_status > #find(:all, :conditions...) ) return 550 rows. > I have to iterate a separate list @items, matching @items[].name to > @item_status[].name > Hit rate on the match will be anywhere from 50%-100%. @items is an > array of WIN32OLE items. > > So the question is this : > Is it better to:The best you can do is measure the different approaches in your specific context.> > A) Retrieve entire data result ( @item_status#find(:all, > > :conditions=>"foreign_key = ''blah''") ) and then use > : @item_status#select > {|i| i.name =~ @item.name}No, see below.> B) Retrieve only (possibly nil) exact item ( @item_status#find(:all, > :conditions=>"foreign_key = ''blah'' AND name = ''#{@item.name}''")Going to the DB for each item individually? Sounds pretty bad.> C) Quit being nitpicky and don''t worry about an N^2 process across > the LAN wire against 500 items ?Keep both lists sorted or sort them on the spot. Then matching itself is linear. Michael -- Michael Schuerig Failures to use one''s frontal lobes mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org can result in the loss of them. http://www.schuerig.de/michael/ --William H. Calvin