> For the complete documentation index, see [llms.txt](https://quick.ortusbooks.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://quick.ortusbooks.com/1.3.0/relationships/eager-loading.md).

# Eager Loading

## The Problem

Let's imagine a scenario where you are displaying a list of posts. You fetch the posts:

```javascript
prc.posts = getInstance( "Post" ).limit( 25 ).get():
```

And start looping through them:

```markup
<cfoutput>
    <h1>Posts</h1>
    <ul>
        <cfloop array="#prc.posts.get()#" item="post">
            <li>#post.getTitle()# by #post.getAuthor().getUsername()#</li>
        </cfloop>
    </ul>
</cfoutput>
```

When you visit the page, though, you notice it takes a while to load. You take a look at your SQL console and you've executed 26 queries for this one page! What?!?

Turns out that each time you loop through a post to display its author's username you are executing a SQL query to retreive that author. With 25 posts this becomes 25 SQL queries plus one initial query to get the posts. This is where the [N+1 problem](https://stackoverflow.com/questions/97197/what-is-n1-select-query-issue) gets its name.

So what is the solution? **Eager Loading.**

Eager Loading means to load all the needed users for the posts in one query rather than separate queries and then stitch the relationships together. With Quick you can do this with one method call.

## The Solution

### with

You can eager load a relationship with the `with` method call.

```javascript
prc.posts = getInstance( "Post" )
    .with( "user" )
    .limit( 25 )
    .get();
```

`with` takes one parameter, the name of the relationship to load. Note that this is the name of the function, not the entity name. For example:

```javascript
// Post.cfc
component extends="quick.models.BaseEntity" {

    function author() {
        return belongsTo( "User" );    
    }

}
```

To eager load the User in the snippet above you would call pass `author` to the `with` method.

```javascript
getInstance( "Post" ).with( "author" );
```

For this operation, only two queries will be executed:

```
SELECT * FROM `posts` LIMIT 25

SELECT * FROM `users` WHERE `id` IN (1, 2, 3, 4, 5, 6, ...)
```

Quick will then stitch these relationships together so when you call `post.getAuthor()` it will use the fetched relationship value instead of going to the database.

You can eager load multiple relationships by passing an array of relation names to `with` or by calling `with` multiple times.

### load

Finally, you can postpone eager loading until needed by using the `load` method on `QuickCollection`. `load` has the same function signature as `with`. `QuickCollection` is the object returned for all Quick queries that return more than one record. Read more about it in [Collections](/1.3.0/collections.md).


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://quick.ortusbooks.com/1.3.0/relationships/eager-loading.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
