On 8/7/07, Trevor Squires <[EMAIL PROTECTED]> wrote:
>
> Hey,
>
> 2 comments from me:
>
> 1 - using :include one-level-deep when you are fetching *one*
> toplevel object *and* you are not issuing :conditions on
> the :included tables is *always* (well, I've never found an
> exception) slower.
>
> x = Foo.find(4678. :include => [:incoming_messages, :outgoing_messages])
>
> is slower than:
>
> x = Foo.find(4678)
> x.incoming_messages
> x.outgoing_messages
>
> If your reaction is to say "but it's always faster with eager
> loading" then I urge you to *measure* it and get back to me if you
> find that my measurements are wrong.

I agree.  Eager including is only really beneficial if you're doing
lots of queries looping through the messages:

# or use paginate in the lovely will_paginate plugin
@incoming_messages = @foo.incoming_messages.find(:all, :include => :author)

Doing your eager include here should be faster than doing a query on
each row for each message author.

Personally I've stopped using all eager includes in favor of the new
ActiveRecord connection caching and my own active_record_context
plugin (currently in use in Lighthouse):

http://activereload.net/2007/5/23/spend-less-time-in-the-database-and-more-time-outdoors

> 2 - I've got a plugin that improves your situation where you are
> fetching multiple toplevel objects *and* you don't have
> any :conditions that relate to the associations you're pulling in.
>
> foos = Foo.find(:all, :hydrate =>
> [:incoming_messages, :outgoing_messages])
>
> It will split that find() into 3 queries, wiring up the relation
> targets.   I've chosen the explicit :hydrate option to give the user
> more control over the strategy for pulling in associations and it
> works just fine with Rick O's scope plugin too.
>
> I've been sitting on the plugin since railsconf *last* year and never
> released it because I had so many doubts about whether the strategy
> was solid enough.  I've used it enough times that I'm confident it
> works.  I'll be releasing it for public consumption in the next
> couple of weeks.

Oh, I think coda hale had some similar plugin too.  You guys should
totally team up!

-- 
Rick Olson
http://lighthouseapp.com
http://weblog.techno-weenie.net
http://mephistoblog.com

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Core" 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-core?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to