Answers.Body: <p>There’s a lot going on here. Let’s first work out the outline of your algorithm, and therefore where things could be improved.</p>
  
  <ul>
  <li>Query and iterate memberships
  
  <ul>
  <li>Query and iterate members
  
  <ul>
  <li>Query first asset
  
  <ul>
  <li>Get asset transform</li>
  </ul></li>
  </ul></li>
  </ul></li>
  </ul>
  
  <p><em>Note: Every time a transform is used, Craft first has to check whether the transform has been generated (<a href="https://craftcms.com/docs/templating/eager-loading-elements#eager-loading-image-transform-indexes" rel="nofollow">from the docs</a>)</em></p>
  
  <p>As you can see, there are multiple levels of nested relations going on. You’ve mentioned using eager-loading already, but it is definitely beneficial here. One of the cool things Craft can do is eager-load nested relations.</p>
  
  <p>Have you tried something like this?</p>
  
  <pre><code>{% set memberships = craft.entries({
      section: 'membership',
      order: 'cf_membershipShorthand',
      with: [
          '{relatedMembersField}.cf_ma_asset'
      ]
  }) %}
  
  {% for membership in memberships %}
      {% for member in membership.{relatedMembersField} %}
          {% set image = member.cf_ma_asset[0] ?? null %}
          {% set imageParams = { width: 30, height: 30 } %}
  
          {% if image %}
              {# Output image with transform #}
          {% endif %}
      {% endfor %}
  {% endfor %}
  </code></pre>
  
  <p><em>Where {relatedMembersField} is the handle of your entries field.</em></p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-05-23T16:29:01.397Z
Answers.Text: Hello Stuart. Thanks a lot for your answer but I think you misunderstood my problem. I don't have a {relatedMembersField} within the memberships section. The relations are made within the members section and I want to get the members with a REVERSE relation. Maybe I'll have to get the members and memberships entries independently and somehow merge them... but I don't know how you'd do this...
Answers.UserDisplayName: outline4
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-05-19T16:37:36.567Z
Answers.LastActivityDate: 2016-05-19T16:37:36.567Z
Answers.UserDisplayName: Stuart Whitehead
Body: <p>I have the following situation: </p>
  
  <p>A section called "membership" and a section called "member".<br>
  Within the "member" section I can choose one or more memberships.<br>
  Now I'd like to display all memberships with the reverse related members. </p>
  
  <p>The output would look something like this: </p>
  
  <pre><code>membership 1
      member 1, member 2, member 3
  
  membership 2
      member 2, member 4
  
  membership 3
      member 1, member 5
  </code></pre>
  
  <p>How would I do this? </p>
  
  <p>I have the following code, but it's very slow: </p>
  
  <pre><code>{# 
  
      Get all membership entries 
  
  #}
  {% set entries = craft.entries({
      section: 'membership',
      order: 'cf_membershipShorthand',
  }) %}
  
  {% for entry in entries %}
      &lt;h2&gt;&lt;a class="external" href="{{ entry.cf_membershipLink }}"&gt;{% if entry.cf_membershipShorthand %}{{ entry.cf_membershipShorthand }} – {% endif %}{{ entry.title }}&lt;/a&gt;&lt;/h2&gt;
      {# 
  
          Get the related members entries
  
      #}
      {% set members = craft.entries({
              section: 'member',
              relatedTo: entry,
          })
      %}
      {% if members|length %}
          {% for member in members %}
              {% set image = member.cf_ma_asset[0] ?? null %}
              {% if image|length %}
                  {% set imageParams = { width: 30, height: 30 } %}
                  &lt;figure class="mini_portrait"&gt;
                      &lt;a href="{{ member.getUrl() }}" title="{{ member.title }}"&gt;&lt;img class="mini_portrait_image" src="{{ image.getUrl(imageParams) }}" alt="Portrait {{ image.title }}"&gt;&lt;/img&gt;&lt;/a&gt;
                  &lt;/figure&gt;
              {% endif %}
          {% endfor %}
      {% endif %}
  {% endfor %}
  </code></pre>
  
  <p>I even tried to eager load the related members entries, but, to my surprise, that even added queries. </p>
  
  <p>I must be on the wrong path. Could anyone enlighten me? </p>
  
  <p>Thanks</p>
  
  <p>Update to my question: 
  I know how to eager load the related entries: </p>
  
  <pre><code>{% set member = craft.entries({
          section: 'member',
          relatedTo: entry,
          with: [
              ['cf_ma_asset', { withTransforms: [{ width: 30, height: 30 }] }],
          ]
      })
  %}
  </code></pre>
  
  <p>To my surprise that adds queries, but is much faster... </p>
  
Tags: <performance>
Title: How to optimize nested reverse relations?
ThreadText: # How to optimize nested reverse relations?
  
  (Asked by: username_0 on 2016-05-18)
  
  I have the following situation:
  
  A section called "membership" and a section called "member".  
  Within the "member" section I can choose one or more memberships.  
  Now I'd like to display all memberships with the reverse related members.
  
  The output would look something like this:
  
  ```
  membership 1
      member 1, member 2, member 3
  
  membership 2
      member 2, member 4
  
  membership 3
      member 1, member 5
  
  ```
  
  How would I do this?
  
  I have the following code, but it's very slow:
  
  ```
  {# 
  
      Get all membership entries 
  
  #}
  {% set entries = craft.entries({
      section: 'membership',
      order: 'cf_membershipShorthand',
  }) %}
  
  {% for entry in entries %}
      <h2><a class="external" href="{{ entry.cf_membershipLink }}">{% if entry.cf_membershipShorthand %}{{ entry.cf_membershipShorthand }} – {% endif %}{{ entry.title }}</a></h2>
      {# 
  
          Get the related members entries
  
      #}
      {% set members = craft.entries({
              section: 'member',
              relatedTo: entry,
          })
      %}
      {% if members|length %}
          {% for member in members %}
              {% set image = member.cf_ma_asset[0] ?? null %}
              {% if image|length %}
                  {% set imageParams = { width: 30, height: 30 } %}
                  <figure class="mini_portrait">
                      <a href="{{ member.getUrl() }}" title="{{ member.title }}"><img class="mini_portrait_image" src="{{ image.getUrl(imageParams) }}" alt="Portrait {{ image.title }}"></img></a>
                  </figure>
              {% endif %}
          {% endfor %}
      {% endif %}
  {% endfor %}
  
  ```
  
  I even tried to eager load the related members entries, but, to my surprise, that even added queries.
  
  I must be on the wrong path. Could anyone enlighten me?
  
  Thanks
  
  Update to my question:
  I know how to eager load the related entries:
  
  ```
  {% set member = craft.entries({
          section: 'member',
          relatedTo: entry,
          with: [
              ['cf_ma_asset', { withTransforms: [{ width: 30, height: 30 }] }],
          ]
      })
  %}
  
  ```
  
  To my surprise that adds queries, but is much faster...
  
  **Tags:** performance
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2016-05-19. Score: 1
  
  There’s a lot going on here. Let’s first work out the outline of your algorithm, and therefore where things could be improved.
  
  * Query and iterate memberships
    + Query and iterate members
      - Query first asset
        * Get asset transform
  
  *Note: Every time a transform is used, Craft first has to check whether the transform has been generated ([from the docs](https://craftcms.com/docs/templating/eager-loading-elements#eager-loading-image-transform-indexes))*
  
  As you can see, there are multiple levels of nested relations going on. You’ve mentioned using eager-loading already, but it is definitely beneficial here. One of the cool things Craft can do is eager-load nested relations.
  
  Have you tried something like this?
  
  ```
  {% set memberships = craft.entries({
      section: 'membership',
      order: 'cf_membershipShorthand',
      with: [
          '{relatedMembersField}.cf_ma_asset'
      ]
  }) %}
  
  {% for membership in memberships %}
      {% for member in membership.{relatedMembersField} %}
          {% set image = member.cf_ma_asset[0] ?? null %}
          {% set imageParams = { width: 30, height: 30 } %}
  
          {% if image %}
              {# Output image with transform #}
          {% endif %}
      {% endfor %}
  {% endfor %}
  
  ```
  
  *Where {relatedMembersField} is the handle of your entries field.*
  
  > **Comments:**
  > **username_0**: Hello username_1. Thanks a lot for your answer but I think you misunderstood my problem. I don't have a {relatedMembersField} within the memberships section. The relations are made within the members section and I want to get the members with a REVERSE relation. Maybe I'll have to get the members and memberships entries independently and somehow merge them... but I don't know how you'd do this...  
  
  ---
  
  