<p>When you first load the template using the <code>{% cache %}</code> tags, the number of queries will likely be higher because it still has to retrieve the correct data from the database.</p>

<p>Subsequent loads of that template should show a lower amount of queries because it's returning actual cached content at that point.</p>

<p>The same thing will happen once the cache is busted from expiration on the next template load as well.</p>

CC BY-SA 4.0
2018-11-02T22:10:05.297Z
2018-11-02T22:10:05.297Z
Brad Bell
<p>I have a question about the {% cache %} tag with Craft 3. I am reading this article (<a href="https://nystudio107.com/blog/the-craft-cache-tag-in-depth" rel="nofollow noreferrer">https://nystudio107.com/blog/the-craft-cache-tag-in-depth</a>), and trying out implementing the tags; however, when I check the amount of db queries before and after adding the tags, the amount of queries goes up instead of down as suggested in the article. </p>

<p>What would cause this to happen? This is the code I’m currently testing the cache tag on, there are 5000+ entires in the 'articles' section. Without the cache tag there are 19 database queries, with it there are 32 queries.</p>

<pre><code>{% cache %}
    {% for entry in craft.entries.section('articles').all() %}
      {{ entry.title }}&lt;br&gt;
    {% endfor %}
{% endcache %}
</code></pre>

CC BY-SA 4.0
2018-10-27T21:27:46.130Z
When you're checking the database queries, I'm assuming that's on first load, when there is currently no data cached?  Do subsequent loads have a smaller number of queries?
Brad Bell
CC BY-SA 4.0
2018-10-31T14:16:32.593Z
Hi Brad, thanks for the reply, that was the issue! The next few loads had 17 queries.
Brianne Hostutler
<craft3><cache>
Craft 3 - Cache tag increasing number of database queries
# Craft 3 - Cache tag increasing number of database queries

(Asked by: username_0 on 2018-10-25)

I have a question about the {% cache %} tag with Craft 3. I am reading this article (https://nystudio107.com/blog/the-craft-cache-tag-in-depth), and trying out implementing the tags; however, when I check the amount of db queries before and after adding the tags, the amount of queries goes up instead of down as suggested in the article. 
What would cause this to happen? This is the code I’m currently testing the cache tag on, there are 5000+ entires in the 'articles' section. Without the cache tag there are 19 database queries, with it there are 32 queries.
{% cache %}
    {% for entry in craft.entries.section('articles').all() %}
      {{ entry.title }}
    {% endfor %}
{% endcache %}

**Tags:** craft3, cache

> **Comments:**
> **username_1**: When you're checking the database queries, I'm assuming that's on first load, when there is currently no data cached?  Do subsequent loads have a smaller number of queries?  
> **username_0**: Hi Brad, thanks for the reply, that was the issue! The next few loads had 17 queries.  

---

## 1 answers

### Answer by username_1 on 2018-11-02. Score: 2 | Accepted answer

When you first load the template using the `{% cache %}` tags, the number of queries will likely be higher because it still has to retrieve the correct data from the database.

Subsequent loads of that template should show a lower amount of queries because it's returning actual cached content at that point.

The same thing will happen once the cache is busted from expiration on the next template load as well.


---

