Body: <p>This question is related to a previous question I asked: <a href="https://craftcms.stackexchange.com/questions/20860/sort-entries-in-a-category-via-entry-field">Sort entries in a category via entry field</a></p>
  
  <p>I have a page that is running the same query from the previous question:</p>
  
  <pre><code>{% set businesses = craft.entries.relatedTo(category) %}
  {% set state  = entry.bizTown.first.townCounty.first.countyRegion.first.regionState.first ?? 'N/A' %}
  {% set businessesByTown = businesses|group('bizTown.first.townCounty.first.countyRegion.first.regionState[0].title') %}
  
  {% if businesses|length %}
      {% for town, businessesInTown in businessesByTown %}
          &lt;ul class="three-col-list"&gt;
              &lt;h3&gt;{{town}}&lt;/h3&gt;
              &lt;ul&gt;
                  {% for business in businessesInTown %}
                  &lt;li class="business-cat__item"&gt;
                      {% if business.ytVideoId|length %}
                          &lt;i class="fa fa-video-camera" aria-hidden="true"&gt;&lt;/i&gt;
                      {% endif %}
                      &lt;strong&gt;{{ business.link }}&lt;/strong&gt;&lt;br&gt;
  
                      {% set twn = business.bizTown.first %}
                      &lt;a class="business-cat__item--town" href="{{twn.url}}"&gt;{{twn.title}}&lt;/a&gt;
                  &lt;/li&gt;
                  {% endfor %}
              &lt;/ul&gt;
          &lt;/ul&gt;
      {% endfor %}
  {% else %} ... {% endif %}
  </code></pre>
  
  <p>This is on a template which is displaying entries of a given sub-category. It separates the entires (the businesses) according to its state. It also displays what town each business belongs to.</p>
  
  <p><strong>Unforunately this takes the page ~20 seconds to render!</strong> I've tried looking into using plugins for custom SQL queries, and using eager loading.</p>
  
  <p>I have read through <a href="https://tighten.co/blog/craft-cms-building-complex-queries-by-extending-the-elementcriteriamodel" rel="nofollow noreferrer">this article</a> a lot and have poked around inside their plugin, but I still don't know where/how to start! Likewise with eager loading, with all the crap I have going on in this/these queries, I'm not sure where to start with that, either.</p>
  
  <p>At this point I feel like this is abstracting beyond my comfort level, and I'd love some expert input. Thanks!</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-07-25T05:41:58.690Z
Comments.Text: Is there a reason to do `entry.bizTown.first.townCounty.first.countyRegion.first.regionState.[0].title` again on the third line in code example instead of doing `state.title`? 
  
  Because now you're making craft look up the same object (accross multiple relations) twice.
Comments.UserDisplayName: mrsmn
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-07-25T20:09:19.260Z
Comments.Text: Changing line 3 to `{% set businessesByTown = businesses|group(state.title) %}` results in "Impossible to access an attribute ("title") on a string variable ("N/A")."
Comments.UserDisplayName: Steven Thate
Tags: <performance><queries><eager-loading>
Title: Best way to improve complex query?
ThreadText: # Best way to improve complex query?
  
  (Asked by: username_0 on 2017-07-24)
  
  This question is related to a previous question I asked: [Sort entries in a category via entry field](https://craftcms.stackexchange.com/questions/20860/sort-entries-in-a-category-via-entry-field)
  
  I have a page that is running the same query from the previous question:
  
  ```
  {% set businesses = craft.entries.relatedTo(category) %}
  {% set state  = entry.bizTown.first.townCounty.first.countyRegion.first.regionState.first ?? 'N/A' %}
  {% set businessesByTown = businesses|group('bizTown.first.townCounty.first.countyRegion.first.regionState[0].title') %}
  
  {% if businesses|length %}
      {% for town, businessesInTown in businessesByTown %}
          <ul class="three-col-list">
              <h3>{{town}}</h3>
              <ul>
                  {% for business in businessesInTown %}
                  <li class="business-cat__item">
                      {% if business.ytVideoId|length %}
                          <i class="fa fa-video-camera" aria-hidden="true"></i>
                      {% endif %}
                      <strong>{{ business.link }}</strong><br>
  
                      {% set twn = business.bizTown.first %}
                      <a class="business-cat__item--town" href="{{twn.url}}">{{twn.title}}</a>
                  </li>
                  {% endfor %}
              </ul>
          </ul>
      {% endfor %}
  {% else %} ... {% endif %}
  
  ```
  
  This is on a template which is displaying entries of a given sub-category. It separates the entires (the businesses) according to its state. It also displays what town each business belongs to.
  
  **Unforunately this takes the page ~20 seconds to render!** I've tried looking into using plugins for custom SQL queries, and using eager loading.
  
  I have read through [this article](https://tighten.co/blog/craft-cms-building-complex-queries-by-extending-the-elementcriteriamodel) a lot and have poked around inside their plugin, but I still don't know where/how to start! Likewise with eager loading, with all the crap I have going on in this/these queries, I'm not sure where to start with that, either.
  
  At this point I feel like this is abstracting beyond my comfort level, and I'd love some expert input. Thanks!
  
  **Tags:** performance, queries, eager-loading
  
  > **Comments:**
  > **username_1**: Is there a reason to do `entry.bizTown.first.townCounty.first.countyRegion.first.regionState.[0].title` again on the third line in code example instead of doing `state.title`?   Because now you're making craft look up the same object (accross multiple relations) twice.  
  > **username_0**: Changing line 3 to `{% set businessesByTown = businesses|group(state.title) %}` results in "Impossible to access an attribute ("title") on a string variable ("N/A")."  
  
  ---
  
  ## 0 answers
  
  