6

I've created an alphabetical index of entries in my template. The code I'm using is in the gist below:

https://gist.github.com/jamiepittock/2635516e95317e5c6ed6

It results in a layout like the one in the following image (screenshot only shows "B":

Screenshot of the resulting layout

The problem is it takes 704 database queries. If I don't include the category name ("Business People" under the Jeff Bezos entry on the screenshot) that drops to 22.

Is there anything other than caching I can do here?

3

Update:

I'll leave the original answer because they're certainly still viable options, this is now probably best solved by using the eager loading features that Craft 2.6 added.


A bit difficult to properly debug without seeing things like your content architecture, database profiling information, logs, etc.

But using the {% cache %} tag is certainly an option here.

If you're adverse to that, then you could also use a template hook that goes to a plugin method that would "eager load" the data you know you'll be needing for this particular page directly from PHP.

You can search Craft's app folder for '{% hook' to see how we're using that for the CP in a few places as well.

3
  • Thanks Brad. Caching does help, bringing the query count down to a tidy 18 queries, but the problem is these entries are being regularly edited which keeps clearing the cache. I'll look at the template hooks. I'll also look at debugging what's going on a little deeper tomorrow as the time the template's taking to load seems a little excessive - ~15s - even for the query count. Sep 3 '14 at 17:44
  • Eager loading is now available in Craft 2.6, so does that affect your answer now Brad?
    – Simon East
    Aug 5 '16 at 12:09
  • @SimonEast Absolutely... I'll update my answer.
    – Brad Bell
    Aug 5 '16 at 16:56

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.