<p>This is how relationships work in Craft at the moment. Each relation equals +1 query. If you don't access your relation on an entry the query isn't executed. So by only fetching title and/or id you don't run any extra relationship queries.</p>

<p>Caching is your best option at the moment to make the query count go down. Make use of caching to cache large chunks of you code.</p>

<p>Brandon has hinted on Slack that Craft3 might incorporate Yii2 lazy loading of relations using the ->with() command. This will solve a lot of the N+1 queries with relationships.</p>

<p><strong>Update</strong></p>

<p>The release of Craft 2.6 included the functions for Eager-Loading of elements. See this link for documentation on the subject: <a href="https://craftcms.com/docs/templating/eager-loading-elements" rel="nofollow">https://craftcms.com/docs/templating/eager-loading-elements</a></p>

CC BY-SA 3.0
2016-02-23T18:55:35.270Z
There's a rumor that eager loading of relationships might even make it into Craft 2.6. :)
Brad Bell
CC BY-SA 3.0
2016-02-23T20:10:45.047Z
If I could click the upvote button 100000 times I would right now.
naboovalley
CC BY-SA 3.0
2016-02-23T12:21:13.550Z
2016-03-11T10:29:09.690Z
2016-03-11T10:29:09.690Z
naboovalley
<p>I have a page with a lot of <code>entries</code> fields. They are awesome but that leads also to a LOT of db requests.</p>

<p>Aside from caching are there some best practices to lower db requests on <code>entries</code> fields?</p>

<p>For example my whole navigation is built with entries fields:</p>

<pre><code>{% for entry in globalsNavi.businessSorter %}
    &lt;a href="{{ entry.url }}"&gt;{{ entry.title }}&lt;/a&gt;
{% endfor %}
</code></pre>

<p>Would it make any difference if I grabbed only <code>url</code> and <code>title</code> in this example since there are a lot more fields available. And if it would make a difference how would I have to write this?</p>

<p>Something like:</p>

<pre><code>{% for entry in globalsNavi.businessSorter( entry.title, entry.url ) %}
</code></pre>

<relations><database><entries-field><best-practice>
Many db requests with relationship fields
# Many db requests with relationship fields

(Asked by: username_0 on 2016-02-23)

I have a page with a lot of entries fields. They are awesome but that leads also to a LOT of db requests.
Aside from caching are there some best practices to lower db requests on entries fields?
For example my whole navigation is built with entries fields:
{% for entry in globalsNavi.businessSorter %}
    {{ entry.title }}
{% endfor %}

Would it make any difference if I grabbed only url and title in this example since there are a lot more fields available. And if it would make a difference how would I have to write this?
Something like:
{% for entry in globalsNavi.businessSorter( entry.title, entry.url ) %}

**Tags:** relations, database, entries-field, best-practice


---

## 1 answers

### Answer by username_1 on 2016-02-23. Score: 6 | Accepted answer

This is how relationships work in Craft at the moment. Each relation equals +1 query. If you don't access your relation on an entry the query isn't executed. So by only fetching title and/or id you don't run any extra relationship queries.

Caching is your best option at the moment to make the query count go down. Make use of caching to cache large chunks of you code.

Brandon has hinted on Slack that Craft3 might incorporate Yii2 lazy loading of relations using the ->with() command. This will solve a lot of the N+1 queries with relationships.

**Update**

The release of Craft 2.6 included the functions for Eager-Loading of elements. See this link for documentation on the subject: <https://craftcms.com/docs/templating/eager-loading-elements>

> **Comments:**
> **username_2**: There's a rumor that eager loading of relationships might even make it into Craft 2.6. :)  
> **username_1**: If I could click the upvote button 100000 times I would right now.  

---

