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

<p>I'll leave the original answer because they're certainly still viable options, this is now probably best solved by using the <a href="https://craftcms.com/docs/templating/eager-loading-elements" rel="nofollow">eager loading features</a> that Craft 2.6 added.</p>

<hr>

<p>A bit difficult to properly debug without seeing things like your content architecture, database profiling information, logs, etc.</p>

<p>But using the <a href="http://buildwithcraft.com/docs/templating/cache" rel="nofollow">{% cache %}</a> tag is certainly an option here.</p>

<p>If you're adverse to that, then you could also use a <a href="http://buildwithcraft.com/classreference/services/TemplatesService#hook-detail" rel="nofollow">template hook</a> that goes to a plugin method that would "eager load" the data you know you'll be needing for this particular page directly from PHP.</p>

<p>You can search Craft's <code>app</code> folder for <code>'{% hook'</code> to see how we're using that for the CP in a few places as well.</p>

CC BY-SA 3.0
2014-09-03T17:44:17.000Z
Thanks Brad. Caching does help, bringing the query count down to a tidy 18 queries, but the problem is these entries are being regularly edited which keeps clearing the cache. I'll look at the template hooks. I'll also look at debugging what's going on a little deeper tomorrow as the time the template's taking to load seems a little excessive - ~15s - even for the query count.
Jamie Pittock
CC BY-SA 3.0
2016-08-05T12:09:11.873Z
Eager loading is now available in Craft 2.6, so does that affect your answer now Brad?
Simon East
CC BY-SA 3.0
2016-08-05T16:56:49.957Z
@SimonEast Absolutely... I'll update my answer.
Brad Bell
CC BY-SA 3.0
2014-09-03T17:24:11.090Z
2016-08-05T16:58:10.747Z
2016-08-05T16:58:10.747Z
Brad Bell
<p>I've created an alphabetical index of entries in my template. The code I'm using is in the gist below:</p>

<p><a href="https://gist.github.com/jamiepittock/2635516e95317e5c6ed6" rel="noreferrer">https://gist.github.com/jamiepittock/2635516e95317e5c6ed6</a></p>

<p>It results in a layout like the one in the following image (screenshot only shows "B":</p>

<p><img src="https://i.sstatic.net/jfScA.png" alt="Screenshot of the resulting layout"></p>

<p>The problem is it takes 704 database queries. If I don't include the category name ("Business People" under the Jeff Bezos entry on the screenshot) that drops to 22.</p>

<p>Is there anything other than caching I can do here?</p>

<relations><performance><queries>
How can I reduce the number of queries when including a related item within a for loop
# How can I reduce the number of queries when including a related item within a for loop

(Asked by: username_0 on 2014-09-03)

I've created an alphabetical index of entries in my template. The code I'm using is in the gist below:

<https://gist.github.com/jamiepittock/2635516e95317e5c6ed6>

It results in a layout like the one in the following image (screenshot only shows "B":

![Screenshot of the resulting layout](https://i.sstatic.net/jfScA.png)

The problem is it takes 704 database queries. If I don't include the category name ("Business People" under the Jeff Bezos entry on the screenshot) that drops to 22.

Is there anything other than caching I can do here?

**Tags:** relations, performance, queries

---

## 1 answers

### Answer by username_1 on 2014-09-03. Score: 3

**Update**:

I'll leave the original answer because they're certainly still viable options, this is now probably best solved by using the [eager loading features](https://craftcms.com/docs/templating/eager-loading-elements) that Craft 2.6 added.

---

A bit difficult to properly debug without seeing things like your content architecture, database profiling information, logs, etc.

But using the [{% cache %}](http://buildwithcraft.com/docs/templating/cache) tag is certainly an option here.

If you're adverse to that, then you could also use a [template hook](http://buildwithcraft.com/classreference/services/TemplatesService#hook-detail) that goes to a plugin method that would "eager load" the data you know you'll be needing for this particular page directly from PHP.

You can search Craft's `app` folder for `'{% hook'` to see how we're using that for the CP in a few places as well.

> **Comments:**
> **username_0**: Thanks Brad. Caching does help, bringing the query count down to a tidy 18 queries, but the problem is these entries are being regularly edited which keeps clearing the cache. I'll look at the template hooks. I'll also look at debugging what's going on a little deeper tomorrow as the time the template's taking to load seems a little excessive - ~15s - even for the query count.  
> **username_2**: Eager loading is now available in Craft 2.6, so does that affect your answer now Brad?  
> **username_1**: @SimonEast Absolutely... I'll update my answer.  

---

