
SEL not carried through for associations
Reported by thomas | August 28th, 2010 @ 06:53 PM
Not sure if this is a bug or a new feature.
According to http://datamapper.org/articles/spotlight_on_laziness.html, SEL supposed to avoid the n+1 query problem. It references Zoo.find(:all, :include => [ :animals ]).each {}. find is no longer in the current 1.0.0 api, so one can only assume that DM is taking care of this under the hood.
I found a case (http://pastie.org/1119328) where
each iteration of
Device.all(:limit => 100, :offset => k).each {|i| r <<
i.x} triggers a separate join request for each association. This
loop took close to 3 seconds to perform. When the associations were
removed [with all data defined in the same Device table] the same
loop took only 68ms.
So, yes, n+1 is a performance killer in some cases. Per Dan Kubb's suggestion, I got around this by creating a view of the associations and define a model on top of this view. It would be nice to have DM handle this automatically.
Comments and changes to this ticket
-
Martin Gamsjaeger (snusnu) October 31st, 2010 @ 10:30 AM
- State changed from new to confirmed
Please Sign in or create a free account to add a new ticket.
With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »