Lets say I have models
   Accounts(:id,:name,:type) has_many :items
     Items(:id,:account_id;name) belong_to :account

I want all the items where the account.type = "Cost"

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.

The Items table is expected to be large, the Accounts table will be
small.

Numbers tell me that my array approach is better, but then I have not
seen many use that approach.

Any other suggestions? Comments?

Steve



-- 
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