Answers.Body: <p>You could use a <a href="http://buildwithcraft.com/classreference/services/TemplatesService#hook-detail" rel="nofollow">template hook</a> to eager load the related data from a plugin and cache the results for future requests.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-08-20T18:41:44.770Z
Answers.Text: I'm especially interested in the eager loading part. Could you give an example of how to do that? Can't seem to get it working. I have some criteria (group, limit f.i.) and want users with related entries. A little help to get me on the right path would be greatly appreciated!
Answers.UserDisplayName: Paul
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-08-20T17:18:52.710Z
Answers.LastActivityDate: 2015-08-20T17:18:52.710Z
Answers.UserDisplayName: Brad Bell
Body: <p>I need to show a list of users (around 400). No problem, the list loads in 1.5s, for which I could use <code>{% cache %}</code> to speed it up.
  Now, the problem arises whenever I want to display the title of an entry related to the user. The log shows 400 extra queries, so a query per user, if I do this:</p>
  
  <pre><code>{% for user in user %}
      {{ user.relationFieldName.first() }}
  {% endfor %}
  </code></pre>
  
  <p>This seems a bit faster, but not fast:</p>
  
  <pre><code>{% for user in user %}
      {{ user.relationFieldName.find()[0] }}
  {% endfor %}
  </code></pre>
  
  <p>Is there a way to get this information directly when querying for users? Or is there another way to speed this up?</p>
  
  <p>Would eager loading these relations through a plugin help? And if so, what service/model/record would I have to extend to go about this?</p>
  
Tags: <templating><database><elementcriteriamodel>
Title: How could I make related entries load faster?
ThreadText: # How could I make related entries load faster?
  
  (Asked by: username_0 on 2015-08-20)
  
  I need to show a list of users (around 400). No problem, the list loads in 1.5s, for which I could use `{% cache %}` to speed it up.
  Now, the problem arises whenever I want to display the title of an entry related to the user. The log shows 400 extra queries, so a query per user, if I do this:
  
  ```
  {% for user in user %}
      {{ user.relationFieldName.first() }}
  {% endfor %}
  
  ```
  
  This seems a bit faster, but not fast:
  
  ```
  {% for user in user %}
      {{ user.relationFieldName.find()[0] }}
  {% endfor %}
  
  ```
  
  Is there a way to get this information directly when querying for users? Or is there another way to speed this up?
  
  Would eager loading these relations through a plugin help? And if so, what service/model/record would I have to extend to go about this?
  
  **Tags:** templating, database, elementcriteriamodel
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2015-08-20. Score: 1
  
  You could use a [template hook](http://buildwithcraft.com/classreference/services/TemplatesService#hook-detail) to eager load the related data from a plugin and cache the results for future requests.
  
  > **Comments:**
  > **username_0**: I'm especially interested in the eager loading part. Could you give an example of how to do that? Can't seem to get it working. I have some criteria (group, limit f.i.) and want users with related entries. A little help to get me on the right path would be greatly appreciated!  
  
  ---
  
  