Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-06-22T14:01:18.910Z
Answers.LastActivityDate: 2020-06-22T14:39:08.807Z
Answers.LastEditDate: 2020-06-22T14:39:08.807Z
Answers.UserDisplayName: dmatthams
Body: <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>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-05-11T21:36:23.490Z
Comments.Text: I'd appreciate any input on this, can anyone see any improvements I could make?
Comments.UserDisplayName: Moucky
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-06-22T15:28:13.373Z
Comments.Text: It would to know how all of this is setup. How are the ``parents`` and ``post`` sections related?
Comments.UserDisplayName: RitterKnight
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-06-23T10:59:22.007Z
Comments.Text: 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.
Comments.UserDisplayName: James Smith
Tags: <search><query><eager-loading>
Title: Optimising search query
ThreadText: # 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 %}
  
  <div class="list">
    {% for item in pageEntries %}
          {% set image = item.postImage[0] ?? null %}
      {% set catalogue = item.catalogue[0] ?? null %}
      <a href="{{ item.url }}" class="item row {{ loop.first ? 'active' }}">
              <div class="col-8 col-sm">
                {{ item.title }}
              </div>
          </a>
      {% endfor %}
      <div class="pagination">
          <div class="row">
              <div class="col">
                  {% if pageInfo.prevUrl %}<a href="{{ pageInfo.prevUrl }}">Previous</a>{% endif %}
              </div>
              <div class="col text-center">
                  Showing page {{ pageInfo.currentPage }} of {{ pageInfo.totalPages }} pages
              </div>
              <div class="col text-right">
                  {% if pageInfo.nextUrl %}<a href="{{ pageInfo.nextUrl }}">Next</a>{% endif %}
              </div>
          </div>
      </div>
  </div>
  <div class="preview">
      {% 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() %}
          <h2>{{ item.title }}</h2>
          <p><a href="{{ parent.url }}" title="{{ parent.title }}">{{ parent.title }}</a></p>
          <p>{{ parent.date|date('l jS F Y') }} {{ parent.time and parent.archived != '1'  ? parent.time|time('g:iA') }}</p>
      {% endfor %}
  </div>
  
  ```
  
  **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.
  
  
  ---
  
  