Answers.Body: <p>I don't think you'll be able to avoid the database queries, but you'll definitely want to use cacheing here, and set a high cache time if possible.</p>
  
  <p>Wrap your code in {% cache %} <a href="http://buildwithcraft.com/docs/templating/cache" rel="nofollow">tags</a> like below: </p>
  
  <pre><code>{% cache globally for 1 month %}
  {% nav category in craft.categories.group('...') %}
     {% set entryCount = craft.entries.relatedTo(category).total() %}
     {{ category.title}} ({{ entryCount }})
  {% endnav %}
  {% endcache %}
  </code></pre>
  
  <p>If this navigation appears on every page, use the "globally" tag.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-03T18:00:27.797Z
Answers.LastActivityDate: 2015-02-03T18:00:27.797Z
Answers.UserDisplayName: Aaron Berkowitz
Answers.Body: <p>Another option would be to use the rarely talked about <a href="http://buildwithcraft.com/classreference/services/TemplatesService#hook-detail" rel="nofollow">template hook</a> feature in Craft.  The link shows some examples of how to use it and the control panel uses it in one or two places to speed things up.</p>
  
  <p>But from the PHP side, you'd be able to grab all of the data you'd need from the database (using commandBuilder(), joins, etc.), then return it all to the template formatted in a way it can easily consume.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-10T21:16:14.347Z
Answers.Text: Thanks for the answer Brad. Is there an advantage to using a template hook vs a plugin variable to achieve this? Other than with the template hook you get access to the context?
Answers.UserDisplayName: Peter Tell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-10T21:20:56.333Z
Answers.Text: Access to the context is the main difference, I think.
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-03T18:58:39.457Z
Answers.LastActivityDate: 2015-02-03T18:58:39.457Z
Answers.UserDisplayName: Brad Bell
Body: <p>Let's say I'm outputting a list/nav of categories, with the count of entries in each category, as described in <a href="https://craftcms.stackexchange.com/a/395/689">other</a> <a href="https://craftcms.stackexchange.com/a/6868/689">questions</a>:</p>
  
  <pre><code>{% nav category in craft.categories.group('...') %}
     {% set entryCount = craft.entries.relatedTo(category).total() %}
     {{ category.title}} ({{ entryCount }})
  {% endnav %}
  </code></pre>
  
  <p>On a site with plenty of categories (more than 100 in this case), that seems like <strong>a lot</strong> of relatedTo queries. Is there a way I could accomplish the same thing with fewer queries / better performance?</p>
  
Tags: <categories><relations><performance>
Title: Performance of a categories list with post count
ThreadText: # Performance of a categories list with post count
  
  (Asked by: username_0 on 2015-02-03)
  
  Let's say I'm outputting a list/nav of categories, with the count of entries in each category, as described in [other](https://craftcms.stackexchange.com/a/395/689) [questions](https://craftcms.stackexchange.com/a/6868/689):
  
  ```
  {% nav category in craft.categories.group('...') %}
     {% set entryCount = craft.entries.relatedTo(category).total() %}
     {{ category.title}} ({{ entryCount }})
  {% endnav %}
  
  ```
  
  On a site with plenty of categories (more than 100 in this case), that seems like **a lot** of relatedTo queries. Is there a way I could accomplish the same thing with fewer queries / better performance?
  
  **Tags:** categories, relations, performance
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2015-02-03. Score: 2
  
  I don't think you'll be able to avoid the database queries, but you'll definitely want to use cacheing here, and set a high cache time if possible.
  
  Wrap your code in {% cache %} [tags](http://buildwithcraft.com/docs/templating/cache) like below:
  
  ```
  {% cache globally for 1 month %}
  {% nav category in craft.categories.group('...') %}
     {% set entryCount = craft.entries.relatedTo(category).total() %}
     {{ category.title}} ({{ entryCount }})
  {% endnav %}
  {% endcache %}
  
  ```
  
  If this navigation appears on every page, use the "globally" tag.
  
  
  ---
  
  ### Answer by username_2 on 2015-02-03. Score: 2
  
  Another option would be to use the rarely talked about [template hook](http://buildwithcraft.com/classreference/services/TemplatesService#hook-detail) feature in Craft. The link shows some examples of how to use it and the control panel uses it in one or two places to speed things up.
  
  But from the PHP side, you'd be able to grab all of the data you'd need from the database (using commandBuilder(), joins, etc.), then return it all to the template formatted in a way it can easily consume.
  
  > **Comments:**
  > **username_3**: Thanks for the answer username_2. Is there an advantage to using a template hook vs a plugin variable to achieve this? Other than with the template hook you get access to the context?  
  > **username_2**: Access to the context is the main difference, I think.  
  
  ---
  
  