<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>

CC BY-SA 3.0
2017-07-25T05:41:58.690Z
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.
mrsmn
CC BY-SA 3.0
2017-07-25T20:09:19.260Z
Changing line 3 to `{% set businessesByTown = businesses|group(state.title) %}` results in "Impossible to access an attribute ("title") on a string variable ("N/A")."
Steven Thate
<performance><queries><eager-loading>
Best way to improve complex query?
# 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
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 %}
        
            {{town}}
            
                {% for business in businessesInTown %}
                
                    {% if business.ytVideoId|length %}
                        
                    {% endif %}
                    {{ business.link }}

                    {% set twn = business.bizTown.first %}
                    {{twn.title}}
                
                {% endfor %}
            
        
    {% 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 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

