AppleII717 wrote in post #959561:
> I could do:
>   income = Item.joins(:account).where('account.type' => "Income")
>
> Or, I could do:
>
>   income = Item.where(:account_id => Account.where(:type =>
> "Income").map(&:id))
>
> While there are several others queries where this is used, the joins
> approach takes about 20ms in activerecord. The in approach (the inner
> query produces an array of ids) takes about 10ms.
>
> Numbers tell me that my array approach is better, but then I have not
> seen many use that approach.

I don't know how extensive (i.e. how many scenarios) your benchmarking 
involved, but if it was only one contrived test that may not be enough. 
For example what effect does an increasing number of values in the in() 
have on performance?

Searching a single table on an indexed simple integer is obviously going 
to be faster than a join. However, as the number of values in the "in" 
clause increases you need to know how that affects performance. For 
example in(1,2) might be significantly different than 
in(1,3,5,7,20,50,100,200).

My point is that performance of an in() likely doesn't increase linearly 
with the number of values.

-- 
Posted via http://www.ruby-forum.com/.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to