<p>Sorry I've never used Neo before, but I'll answer as if it were a standard Craft matrix field, as I'd expect/hope that it would work the same way(?!):</p>
<ol>
<li><p>First, correct your syntax: In <code>entry.modules.with...</code> you're already in the context of your field called <code>modules</code>, so you shouldn't repeat the <code>modules. ...</code> part at the start of each eager loadable thing.</p>
</li>
<li><p>Second, there is no value in eager loading things that are not relationships pointing to other elements, ie: assets, categories, entries, users, or tags. So don't attempt to eager load text fields and lightswitches - it's meaningless.</p>
</li>
<li><p>For assets, don't forget to also eager load any transforms that you might be using.</p>
</li>
</ol>
<p>Syntax might then look like this:</p>
<pre class="lang-twig prettyprint-override"><code>{% set eagerLoadedModules = entry.modules.with([
    ['headlineWithContent:backgroundImage', {withTransforms: arrayOfTransforms1}],
    ['headlineWithContent:backgroundImageMobile', {withTransforms: arrayOfTransforms2}]
]).all() %}
</code></pre>
<p>Depending on how you set your transforms (control panel vs templates vs something custom like in config/custom.php) your transform arrays might look something like this:</p>
<pre class="lang-twig prettyprint-override"><code>{% set arrayOfTransforms1 = [
    {width: 1980},
    {width: 1700},
    {width: 1200},
    {width: 800},
    {width: 1980, format: 'webp'},
    {width: 1700, format: 'webp'},
    {width: 1200, format: 'webp'},
    {width: 800, format: 'webp'},
] %}

{% set arrayOfTransforms2 = [
    {width: 900, height: 900, mode: 'crop'},
    {width: 500, height: 500, mode: 'crop'},
] %}
</code></pre>
<p>(Aside: to avoid manual duplication of the webp variants, and also to make my transforms available globally without bumping into awkward Twig parse order issues, I put mine in config/custom.php like this: <a href="https://github.com/cld-agency/craftwind-boilerplate/blob/craft-4/config/custom.php" rel="nofollow noreferrer">https://github.com/cld-agency/craftwind-boilerplate/blob/craft-4/config/custom.php</a>)</p>

CC BY-SA 4.0
2024-03-20T16:11:12.520Z
2024-03-20T16:11:12.520Z
James Smith
<p>sorry if the following question is too basic, but I've spent quite some time digging now so I decided to reach out for help.</p>
<h2>Is 250 DB queries too many?</h2>
<p>I have a Neo field &quot;modules&quot; that can contain different ~30 other fields for content, such as &quot;Headline&quot;, &quot;WYSISG Block&quot;, &quot;Buttons&quot;, etc. I think this is a common pattern for CraftCMS. The modules are then rendered as follows:</p>
<pre><code>{% for module in entry.modules %} 
    {% include alias('@organisms/o-' ~ module.type.handle|replace('/(?&lt;!^)[A-Z]/', '-$0')|lower) ignore missing with module %}
{% endfor %}
</code></pre>
<p>With 14 content blocks this results in <strong>~250 DB queries -- is this too many</strong>?</p>
<h2>Reducing the number of queries</h2>
<p>I tried to bring the number of queries down by <strong>eager-loading</strong>, adding the following:</p>
<pre><code>{% set eagerLoadedModules = entry.modules.with(
    [
        'modules.headlineWithContent:backgroundImage',
        'modules.headlineWithContent:backgroundImageMobile',
        'modules.headlineWithContent:backgroundPosition',
        'modules.headlineWithContent:fieldTitle',
        'modules.headlineWithContent:text',
        '...', 
    ]
).all() %} 


{% for module in eagerLoadedModules %} 
    {% include alias('@organisms/o-' ~ module.type.handle|replace('/(?&lt;!^)[A-Z]/', '-$0')|lower) ignore missing with module  %}
{% endfor %}
</code></pre>
<p>However, this did not reduce the number of queries compared to the &quot;dummy&quot; version. I thought the problem was that eager-loading is rather meant for &quot;N+1 queries&quot; situations which is not really my use-case. I hoped that CraftCMS would <strong>merge the eager-load into one big query</strong>, but it seems to not be the case.</p>
<p>Can you please provide me with code examples and pointers on how to bring the number of queries down in this case?</p>
<ul>
<li>Craft: 3.9.6</li>
<li>Neo: 2.13.19</li>
</ul>
<p>Thanks!</p>

<craft3><eager-loading><queries><plugin-neo>
Eager-loading content and merging queries
# Eager-loading content and merging queries

(Asked by: username_0 on 2024-03-19)

sorry if the following question is too basic, but I've spent quite some time digging now so I decided to reach out for help.
Is 250 DB queries too many?
I have a Neo field "modules" that can contain different ~30 other fields for content, such as "Headline", "WYSISG Block", "Buttons", etc. I think this is a common pattern for CraftCMS. The modules are then rendered as follows:
{% for module in entry.modules %} 
    {% include alias('@organisms/o-' ~ module.type.handle|replace('/(?
With 14 content blocks this results in ~250 DB queries -- is this too many?
Reducing the number of queries
I tried to bring the number of queries down by eager-loading, adding the following:
{% set eagerLoadedModules = entry.modules.with(
    [
        'modules.headlineWithContent:backgroundImage',
        'modules.headlineWithContent:backgroundImageMobile',
        'modules.headlineWithContent:backgroundPosition',
        'modules.headlineWithContent:fieldTitle',
        'modules.headlineWithContent:text',
        '...', 
    ]
).all() %} 


{% for module in eagerLoadedModules %} 
    {% include alias('@organisms/o-' ~ module.type.handle|replace('/(?
However, this did not reduce the number of queries compared to the "dummy" version. I thought the problem was that eager-loading is rather meant for "N+1 queries" situations which is not really my use-case. I hoped that CraftCMS would merge the eager-load into one big query, but it seems to not be the case.
Can you please provide me with code examples and pointers on how to bring the number of queries down in this case?

Craft: 3.9.6
Neo: 2.13.19

Thanks!

**Tags:** craft3, eager-loading, queries, plugin-neo

---

## 1 answers

### Answer by username_1 on 2024-03-20. Score: 2 | Accepted answer

Sorry I've never used Neo before, but I'll answer as if it were a standard Craft matrix field, as I'd expect/hope that it would work the same way(?!):

First, correct your syntax: In entry.modules.with... you're already in the context of your field called modules, so you shouldn't repeat the modules. ... part at the start of each eager loadable thing.

Second, there is no value in eager loading things that are not relationships pointing to other elements, ie: assets, categories, entries, users, or tags. So don't attempt to eager load text fields and lightswitches - it's meaningless.

For assets, don't forget to also eager load any transforms that you might be using.


Syntax might then look like this:
{% set eagerLoadedModules = entry.modules.with([
    ['headlineWithContent:backgroundImage', {withTransforms: arrayOfTransforms1}],
    ['headlineWithContent:backgroundImageMobile', {withTransforms: arrayOfTransforms2}]
]).all() %}

Depending on how you set your transforms (control panel vs templates vs something custom like in config/custom.php) your transform arrays might look something like this:
{% set arrayOfTransforms1 = [
    {width: 1980},
    {width: 1700},
    {width: 1200},
    {width: 800},
    {width: 1980, format: 'webp'},
    {width: 1700, format: 'webp'},
    {width: 1200, format: 'webp'},
    {width: 800, format: 'webp'},
] %}

{% set arrayOfTransforms2 = [
    {width: 900, height: 900, mode: 'crop'},
    {width: 500, height: 500, mode: 'crop'},
] %}

(Aside: to avoid manual duplication of the webp variants, and also to make my transforms available globally without bumping into awkward Twig parse order issues, I put mine in config/custom.php like this: https://github.com/cld-agency/craftwind-boilerplate/blob/craft-4/config/custom.php)


---

