Answers.Body: <p>First of all just a few ideas about best practices</p>
  
  <h3>Don't</h3>
  
  <pre class="lang-twig prettyprint-override"><code>{{ entry.relatedBrand.one.url }}
  {{ entry.relatedBrand.one.title }}
  {{ entry.relatedBrand.one.size }}
  </code></pre>
  
  <h3>Do</h3>
  
  <pre class="lang-twig prettyprint-override"><code>{% set relatedImage = entry.relatedBrand.one() %}
  {{ relatedImage.url }}
  {{ relatedImage.title }}
  {{ relatedImage.size }}
  </code></pre>
  
  <p>Furthermore your code with eager loading</p>
  
  <pre class="lang-twig prettyprint-override"><code>{% set brandIds = craft
      .entries()
      .section('brands')
      .relatedTo({ targetElement: category })
      .ids() 
  %}
  {% set offers = craft
      .entries()
      .section('offers')
      .with(['relatedBrand.brandLogo'])
      .relatedTo(brandIds)
      .limit(6) 
      .all()
  %}
  
  {% for entry in offers %}
    &lt;div class="card category-card"&gt;
      {% set relatedBrand = entry.relatedBrand? entry.relatedBrand[0] : null %}
      &lt;a href="{{ relatedBrand.url?? '' }}#{{ entry.id }}" title="{{ entry.title }}"&gt;
      &lt;div class="row"&gt;
        &lt;div class="col-md-12 col-4"&gt;
            {% set logos = relatedBrand? relatedBrand.brandLogo : [] %}
            {% for asset in logos %}
              &lt;img class="card-img-top" src="{{ asset.geturl }}" alt="Card image cap"&gt;
            {% endfor %}
        &lt;/div&gt;
        &lt;div class="col-md-12 col-8"&gt;
          &lt;div class="card-body"&gt;
            &lt;h5 class="card-title"&gt;{{ entry.title }}&lt;/h5&gt;
            &lt;p&gt;{{ entry.body }}&lt;/p&gt;
            &lt;small class="text-muted"&gt;{{ relatedBrand.title?? '' }}&lt;/small&gt;
          &lt;/div&gt;
        &lt;/div&gt;
        &lt;/div&gt;
      &lt;/a&gt;
    &lt;/div&gt;
  {% endfor %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-10-29T14:28:02.453Z
Answers.Text: Robin! You're amazing! I literally tried 15 things on how to increase the speed of this!!! This has literally nearly halved the load on that query!
Answers.UserDisplayName: JMKelley
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-10-29T11:52:16.103Z
Answers.LastActivityDate: 2018-10-29T11:52:16.103Z
Answers.UserDisplayName: Robin Schambach
Body: <p>I have quite a substantial amount of queries heading out for this bit of code below. I've tried quite a few things to get it down, but I need to eager load some things just to try and reduce the overhead of this, but I still think it won't do enough. Is there anything I can look at to optimise this?</p>
  
  <pre><code>    {% set brandIds = craft.entries().section('brands').relatedTo({ targetElement: category }).ids() %}
      {% set offers = craft.entries().section('offers').relatedTo(brandIds).limit(6) %}
  
      {% for entry in offers %}
        &lt;div class="card category-card"&gt;
          &lt;a href="{{ entry.relatedBrand.one.url }}#{{ entry.id }}" title="{{ entry.title }}"&gt;
          &lt;div class="row"&gt;
            &lt;div class="col-md-12 col-4"&gt;
                {% for asset in entry.relatedBrand.one.brandLogo.all() %}
                  &lt;img class="card-img-top" src="{{ asset.geturl }}" alt="Card image cap"&gt;
                {% endfor %}
            &lt;/div&gt;
            &lt;div class="col-md-12 col-8"&gt;
              &lt;div class="card-body"&gt;
                &lt;h5 class="card-title"&gt;{{ entry.title }}&lt;/h5&gt;
                &lt;p&gt;{{ entry.body }}&lt;/p&gt;
                &lt;small class="text-muted"&gt;{{ entry.relatedBrand.one }}&lt;/small&gt;
              &lt;/div&gt;
            &lt;/div&gt;
            &lt;/div&gt;
          &lt;/a&gt;
        &lt;/div&gt;
    {% endfor %}
  </code></pre>
  
Tags: <templating><eager-loading>
Title: Eager Loading Relatedto & Target Elements
ThreadText: # Eager Loading Relatedto & Target Elements
  
  (Asked by: username_0 on 2018-10-28)
  
  I have quite a substantial amount of queries heading out for this bit of code below. I've tried quite a few things to get it down, but I need to eager load some things just to try and reduce the overhead of this, but I still think it won't do enough. Is there anything I can look at to optimise this?
  
  ```
      {% set brandIds = craft.entries().section('brands').relatedTo({ targetElement: category }).ids() %}
      {% set offers = craft.entries().section('offers').relatedTo(brandIds).limit(6) %}
  
      {% for entry in offers %}
        <div class="card category-card">
          <a href="{{ entry.relatedBrand.one.url }}#{{ entry.id }}" title="{{ entry.title }}">
          <div class="row">
            <div class="col-md-12 col-4">
                {% for asset in entry.relatedBrand.one.brandLogo.all() %}
                  <img class="card-img-top" src="{{ asset.geturl }}" alt="Card image cap">
                {% endfor %}
            </div>
            <div class="col-md-12 col-8">
              <div class="card-body">
                <h5 class="card-title">{{ entry.title }}</h5>
                <p>{{ entry.body }}</p>
                <small class="text-muted">{{ entry.relatedBrand.one }}</small>
              </div>
            </div>
            </div>
          </a>
        </div>
    {% endfor %}
  
  ```
  
  **Tags:** templating, eager-loading
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-10-29. Score: 2 | Accepted answer
  
  First of all just a few ideas about best practices
  
  ### Don't
  
  ```twig
  {{ entry.relatedBrand.one.url }}
  {{ entry.relatedBrand.one.title }}
  {{ entry.relatedBrand.one.size }}
  
  ```
  
  ### Do
  
  ```twig
  {% set relatedImage = entry.relatedBrand.one() %}
  {{ relatedImage.url }}
  {{ relatedImage.title }}
  {{ relatedImage.size }}
  
  ```
  
  Furthermore your code with eager loading
  
  ```twig
  {% set brandIds = craft
      .entries()
      .section('brands')
      .relatedTo({ targetElement: category })
      .ids() 
  %}
  {% set offers = craft
      .entries()
      .section('offers')
      .with(['relatedBrand.brandLogo'])
      .relatedTo(brandIds)
      .limit(6) 
      .all()
  %}
  
  {% for entry in offers %}
    <div class="card category-card">
      {% set relatedBrand = entry.relatedBrand? entry.relatedBrand[0] : null %}
      <a href="{{ relatedBrand.url?? '' }}#{{ entry.id }}" title="{{ entry.title }}">
      <div class="row">
        <div class="col-md-12 col-4">
            {% set logos = relatedBrand? relatedBrand.brandLogo : [] %}
            {% for asset in logos %}
              <img class="card-img-top" src="{{ asset.geturl }}" alt="Card image cap">
            {% endfor %}
        </div>
        <div class="col-md-12 col-8">
          <div class="card-body">
            <h5 class="card-title">{{ entry.title }}</h5>
            <p>{{ entry.body }}</p>
            <small class="text-muted">{{ relatedBrand.title?? '' }}</small>
          </div>
        </div>
        </div>
      </a>
    </div>
  {% endfor %}
  
  ```
  
  > **Comments:**
  > **username_0**: username_1! You're amazing! I literally tried 15 things on how to increase the speed of this!!! This has literally nearly halved the load on that query!  
  
  ---
  
  