# Load IDs For Limited Eager Loading

**URL:** <https://www.ruby-forum.com/t/load-ids-for-limited-eager-loading/96361>\
**Category:** Rails\
**Created:** [June 28, 2006, 12:04pm UTC](https://www.ruby-forum.com/t/load-ids-for-limited-eager-loading/96361 "2006-06-28T12:04:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mark](https://www.ruby-forum.com/u/mark)\
**Post date:** [June 28, 2006, 12:04pm UTC](https://www.ruby-forum.com/t/load-ids-for-limited-eager-loading/96361/1 "2006-06-28T12:04:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mark](https://www.ruby-forum.com/u/mark)\
**Post date:** [June 28, 2006, 12:20pm UTC](https://www.ruby-forum.com/t/load-ids-for-limited-eager-loading/96361/2 "2006-06-28T12:20:26Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mark](https://www.ruby-forum.com/u/mark)\
**Post date:** [June 28, 2006, 12:35pm UTC](https://www.ruby-forum.com/t/load-ids-for-limited-eager-loading/96361/3 "2006-06-28T12:35:39Z")

</div>

Thanks Jonathan

You were right - clear as mud 😉

As a general rule of thumb should I use :limit wherever possible even if  
I am creating a second query with eager loading or does it depend on my  
data?

Cheers

Mark

---

<div class="post-metadata">

**Author:** ![mark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mark](https://www.ruby-forum.com/u/mark)\
**Post date:** [June 28, 2006, 12:48pm UTC](https://www.ruby-forum.com/t/load-ids-for-limited-eager-loading/96361/4 "2006-06-28T12:48:53Z")

</div>

Two queries are only necessary if you use :limit and :include  
together. If you plan to use all the included associations then don’t  
worry about the second query, it’s still faster than not including  
them.

Using :limit is good because you are reducing the number of AR objects  
that have to be instantiated.

-Jonathan.

---

<div class="post-metadata">

**Author:** ![mark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mark](https://www.ruby-forum.com/u/mark)\
**Post date:** [June 28, 2006, 1:09pm UTC](https://www.ruby-forum.com/t/load-ids-for-limited-eager-loading/96361/5 "2006-06-28T13:09:04Z")

</div>

Thanks for your knowledge on this.

Enjoy your day.

Cheers

Mark
