If you do something like Person.find(:all, :include => :notes, :limit
=> 10) AR requires two queries to get the result.
The first query just queries the people table and stores the 10 id
numbers that are to be loaded.
The second query queries the people and notes tables and adds a
condition that the id is included in the list of 10 stored ids.
Doing it all at once would not work because as well as a row for each
row in the people table, extra rows are included because of the left
outer join used on the notes table. For example, if the first person
had 10 notes associated with them, the limit would take those 10 rows
and you''d end up with a result that had 1 Person with their notes
preloaded. Not what you expect. You expect the :limit => 10 to give
you 10 Person objects.
To do it in one query you''d have to use a subselect in a condition of
the query. Some databases don''t support subselects.
select * from people left outer join notes on people.id notes.person_id where
people.id in (select id from people limit 10)
Clear as mud? :)
-Jonathan.
On 6/28/06, Mark <markgeorgegoddard@gmail.com>
wrote:> Hi
>
> Trying to cut down on queries with eager loading and noticed the
> following in development.log
>
> Load IDs For Limited Eager Loading - SELECT id ....etc
>
> Followed by
>
> Load Including Associations - SELECT table.id ... etc
>
> Is this common practice? What is limted eager loading?
>
> Cheers
>
> Mark
>
> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>