<p>It's hard to see what you're actually trying to achieve here, but yes it's likely to do with the amount of queries you're doing on the page.</p>
<p>I would either re-consider the page layout (without knowing what you're trying to achieve by having two loops it's hard to judge) OR pull some, or all of the results asynchronously using javascript so the page rendering isn't blocked whilst the query is being processed.</p>
<p>The newly released <a href="https://plugins.craftcms.com/sprig" rel="nofollow noreferrer">sprig plugin</a> may be of interest.</p>

CC BY-SA 4.0
2020-06-22T14:01:18.910Z
2020-06-22T14:39:08.807Z
2020-06-22T14:39:08.807Z
dmatthams
<p>I was wondering if there is a better way to do a query like this, it takes a long time to execute and I know setting the parent variable is where there's a problem but am not sure of an alternative way to do it. Appreciate any and all help!</p>

<pre><code>{% set searchResults = craft.entries.section(['post']).search(searchQuery).with(['catalogue', 'postImage']).where({'field_show': 1}).limit(20) %}
{% paginate searchResults as pageInfo, pageEntries %}

&lt;div class="list"&gt;
  {% for item in pageEntries %}
        {% set image = item.postImage[0] ?? null %}
    {% set catalogue = item.catalogue[0] ?? null %}
    &lt;a href="{{ item.url }}" class="item row {{ loop.first ? 'active' }}"&gt;
            &lt;div class="col-8 col-sm"&gt;
              {{ item.title }}
            &lt;/div&gt;
        &lt;/a&gt;
    {% endfor %}
    &lt;div class="pagination"&gt;
        &lt;div class="row"&gt;
            &lt;div class="col"&gt;
                {% if pageInfo.prevUrl %}&lt;a href="{{ pageInfo.prevUrl }}"&gt;Previous&lt;/a&gt;{% endif %}
            &lt;/div&gt;
            &lt;div class="col text-center"&gt;
                Showing page {{ pageInfo.currentPage }} of {{ pageInfo.totalPages }} pages
            &lt;/div&gt;
            &lt;div class="col text-right"&gt;
                {% if pageInfo.nextUrl %}&lt;a href="{{ pageInfo.nextUrl }}"&gt;Next&lt;/a&gt;{% endif %}
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;
&lt;div class="preview"&gt;
    {% for item in pageEntries %}
      {% set image = item.postImage[0] ?? null %}
      {% set catalogue = item.catalogue[0] ?? null %}
      {% set parent = craft.entries.section('parents').relatedTo({targetElement: catalogue, field: 'catalogue'}).one() %}
        &lt;h2&gt;{{ item.title }}&lt;/h2&gt;
        &lt;p&gt;&lt;a href="{{ parent.url }}" title="{{ parent.title }}"&gt;{{ parent.title }}&lt;/a&gt;&lt;/p&gt;
        &lt;p&gt;{{ parent.date|date('l jS F Y') }} {{ parent.time and parent.archived != '1'  ? parent.time|time('g:iA') }}&lt;/p&gt;
    {% endfor %}
&lt;/div&gt;
</code></pre>

CC BY-SA 4.0
2020-05-11T21:36:23.490Z
I'd appreciate any input on this, can anyone see any improvements I could make?
Moucky
CC BY-SA 4.0
2020-06-22T15:28:13.373Z
It would to know how all of this is setup. How are the ``parents`` and ``post`` sections related?
RitterKnight
CC BY-SA 4.0
2020-06-23T10:59:22.007Z
As you say, setting the parent variable on the inside of your loop is the source of the slowness as you're doing an extra set of DB queries per iteration. It's hard to give you a proper answer without knowing more details about how your sections and fields are set up, but if it were me I'd try to solve this using a hash map/array of all parents indexed by a common key which you can then match up inside the main loop without incurring the N+1 problem.
James Smith
<search><query><eager-loading>
Optimising search query
# Optimising search query

(Asked by: username_0 on 2020-03-16)

I was wondering if there is a better way to do a query like this, it takes a long time to execute and I know setting the parent variable is where there's a problem but am not sure of an alternative way to do it. Appreciate any and all help!
{% set searchResults = craft.entries.section(['post']).search(searchQuery).with(['catalogue', 'postImage']).where({'field_show': 1}).limit(20) %}
{% paginate searchResults as pageInfo, pageEntries %}


  {% for item in pageEntries %}
        {% set image = item.postImage[0] ?? null %}
    {% set catalogue = item.catalogue[0] ?? null %}
    
            
              {{ item.title }}
            
        
    {% endfor %}
    
        
            
                {% if pageInfo.prevUrl %}Previous{% endif %}
            
            
                Showing page {{ pageInfo.currentPage }} of {{ pageInfo.totalPages }} pages
            
            
                {% if pageInfo.nextUrl %}Next{% endif %}
            
        
    


    {% for item in pageEntries %}
      {% set image = item.postImage[0] ?? null %}
      {% set catalogue = item.catalogue[0] ?? null %}
      {% set parent = craft.entries.section('parents').relatedTo({targetElement: catalogue, field: 'catalogue'}).one() %}
        {{ item.title }}
        {{ parent.title }}
        {{ parent.date|date('l jS F Y') }} {{ parent.time and parent.archived != '1'  ? parent.time|time('g:iA') }}
    {% endfor %}

**Tags:** search, query, eager-loading
> **Comments:**
> **username_0**: I'd appreciate any input on this, can anyone see any improvements I could make?  
> **username_1**: It would to know how all of this is setup. How are the ``parents`` and ``post`` sections related?  
> **username_2**: As you say, setting the parent variable on the inside of your loop is the source of the slowness as you're doing an extra set of DB queries per iteration. It's hard to give you a proper answer without knowing more details about how your sections and fields are set up, but if it were me I'd try to solve this using a hash map/array of all parents indexed by a common key which you can then match up inside the main loop without incurring the N+1 problem.  

---

## 1 answers

### Answer by username_3 on 2020-06-22. Score: 1

It's hard to see what you're actually trying to achieve here, but yes it's likely to do with the amount of queries you're doing on the page.

I would either re-consider the page layout (without knowing what you're trying to achieve by having two loops it's hard to judge) OR pull some, or all of the results asynchronously using javascript so the page rendering isn't blocked whilst the query is being processed.

The newly released [sprig plugin](https://plugins.craftcms.com/sprig) may be of interest.


---

