5

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?

1

You could use a template hook to eager load the related data from a plugin and cache the results for future requests.

1
  • 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!
    – Paul
    Aug 20 '15 at 18:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.