So eager loading via :include is far from perfect: as soon as there  
are more than one has_many then you're getting a big cartesian join  
and end up processing 10000 rows of information in order to get out  
200 rows of extra stuff (assuming two 100 object has_manys)

I've knocked together a patch (http://dev.rubyonrails.org/ticket/ 
9640) which takes a different approach:
Author.find :all, :conditions => blah, :preload => [:posts, :comments]

will first load authors, then one query for posts and one for comments

You can nest things like you can with eager loading i.e.

Author.find :all, :conditions => blah, :preload => [:comments,  
{:posts => :categories}]
The number of queries will be n+1 (except if you have a  
has_many :through, which count double since we first load  
the :through association)

The one thing you definitely can't do is have conditions on the  
preloaded tables, so

Author.find :all, :conditions => "posts.body like '%foo%', :preload  
=> [:comments, {:posts => :categories}]

is right out.

I've put together some unit tests (and by 'put together' I mean  
'nicked the tests for eager loading and replaced :include  
with :preload') that at least cover basic use. I think most of what  
is achievable via :include should also also be doable in this way,  
but it's definitely the case that right now I've only handled the  
simple case for a lot of stuff. I'm sure there are various  
combinations of options that aren't handled or covered by the tests  
although I don't see why most of it can't be fixed up.

I'm not sure where all this fits in but thought it was interesting  
enough that others might want to have a look. In the specific case I  
had in mind (large has_many loads) it's way faster than :include, and  
seems to be as fast or faster in other cases too.

Fred


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