5

I'm trying to figure out if there's a more performant way of getting entries grouped by category.

As you can see, I'm using NSM Categories module. Right now, this costing me 263 queries and 5 seconds of load. If I remove my if statements (only there because we have different markup for the first entry of a category and also spit out an opening ul element), performance increases dramatically using 63 queries in 2 seconds.

Can anyone suggest any improvements? With something so intensive, we'll be using CE Cache anyway but would like to improve even more before we get to that stage.

{exp:nsm_categories:archive
            enable="entries"
            category_channel_entry_limit="6"
            category="3|6|5|1|7|2"
        }
            <div class="category category--{category_url_title}">
                <h3 class="category__title"><a class="category__link" href="/channel/{category_url_title}">{category_name}</a></h3>

                {category_channel_entries}
                {if {category_channel_entry_count} == '1'}
                <div class="category__firstentry relative" style=
                "background-image: url(''); background-size: cover;">
                <div class="category__overlay absolute"></div><a class="title relative" href="">{title}</a>
                    <p class="author relative">by {exp:playa:children var_prefix="children" entry_id="{entry_id}" backspace="5"}{children:title}</a> and {/exp:playa:children}</p>
                </div>
                {/if}

                {if {category_channel_entry_count} == '2'}
                <ul class="category__entries">
                {/if}
                {if {category_channel_entry_count} > '1'}
                <li class="entry__single">
                    <a href="/article/{url_title}">{title}</a>
                </li>
                {/if}

                {/category_channel_entries}
            </div>
            {/exp:nsm_categories:archive}
3

So my experimentation here brought me to Stash.

Much more succinct now and gives me everything I need in just 66 queries before caching :)

{exp:channel:entries dynamic="no" channel="articles"}
     {categories}
      {exp:stash:append_list name="news_{category_id}"}
       {stash:item_title}{title} - {entry_date {gv_date_short}}{/stash:item_title}
      {/exp:stash:append_list}
     {/categories}
{/exp:channel:entries}


{exp:channel:category_archive channel="articles" show="not 4|22|24" style="linear"}
  {categories}
    <h2>{category_name} - {category_id}</h2>
    {exp:stash:get_list limit="6" name="news_{category_id}"}
    {if count == '1'}
    <h3>{item_title}</h3>
    {/if}
    {if count == '2'}
      <ul class="list">
    {/if}
    {if count > '1'}
    <li>{item_title}</li>
    {/if}
    {if count == total_results}
      </ul>
    {/if}
    {/exp:stash:get_list}
    {/categories}
{/exp:channel:category_archive}
| improve this answer | |
1

Interesting approach. I would typically have used Stash to

  • set a list of all my entries (capturing categories for each of them)
  • set a list of all the categories I want

And then used nested {exp:stash:get_list} with match_against to get entries ordered by categories. Same principle as this gist by Mark Croxton

Don't know if that would lower your number of queries, though.

| improve this answer | |
0

When using if conditionals you're going to have the overhead of parsing the template code within every condition and then getting rid of the ones that don't apply, for every iteration of your loop.

Using Switchee should greatly reduce this overhead as it's going to get rid of the non-matching conditions and only parse the one that matches.

I've seen some good performance improvements just by moving to using Switchee rather than using regular conditionals.

| improve this answer | |
  • yeah, I found that as well. I've actually even had to ditch this approach because when I introduced a Playa field, the query count went through the roof. Now using the query module with an embedded template, I'm sure there must be a way to do this with Stash that's less intensive but I can't continue to spend time on it. – Steven Grant Mar 5 '15 at 11:37
  • custom queries that don't retrieve every field and parse every entry through every condition :) – foamcow Mar 5 '15 at 13:44
  • yeah, I like the query module – Steven Grant Mar 5 '15 at 13:52
  • It is restricted though. For anything beyond the basics you could wrap your custom queries in a plugin. – foamcow Mar 5 '15 at 14:00

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.