<p>It's natural that you're seeing a lot of extra queries; Craft does a ton of work when saving an entry (creating/pruning revisions, updating search indexes, propagating elements etc). You're probably also going to have your template caches invalidated on every pageload. In short, re-saving entries on every frontend request is a bad idea.</p>
<p>A much better approach is using Craft's <a href="https://craftcms.com/docs/3.x/dev/functions.html#seq" rel="nofollow noreferrer"><code>seq()</code></a> function:</p>
<pre><code>{# Increment page views for the current entry, without outputting the current count #}
{% do seq('pageviews:' ~ entry.id) %}

{# Output page views for the current entry, without incrementing #}
{{ seq('pageviews:' ~ entry.id, next=false) }}

{# Increment _and_ output the current count #}
{{ seq('pageviews:' ~ entry.id) }}
</code></pre>
<p>The <code>seq()</code> function doesn't interface with custom fields, though. If you do need to have the page view count stored in a custom field for some reason, a possible workaround in terms of the performance issue is going through the <a href="https://docs.craftcms.com/api/v3/craft-services-content.html#public-properties" rel="nofollow noreferrer">Content service</a> to save the updated custom field value instead of actually re-saving the entry using <code>craft.app.getElements()</code>:</p>
<pre><code>{% set pageviews = entry.pageviews %}
{% do entry.setFieldValue('pageviews', pageviews+1) %}
{% do craft.app.getContent().saveContent(entry) %}
</code></pre>
<p>The above is pretty hacky though, and not really a <em>much</em> better idea than actually re-saving the entry in full (although it does result in less queries being executed). It's definitely not an approach I would recommend.</p>
<p><em>Finally, there's also a few plugins available for storing view counts, for example <a href="https://plugins.craftcms.com/view-count" rel="nofollow noreferrer">this</a> or <a href="https://plugins.craftcms.com/entry-count" rel="nofollow noreferrer">this</a>.</em></p>

CC BY-SA 4.0
2022-03-07T11:03:29.260Z
Great thank you so much! I did not know the seq() function, very useful.
Samuel
CC BY-SA 4.0
2022-03-07T10:40:20.220Z
2022-03-07T10:45:51.493Z
2022-03-07T10:45:51.493Z
Mats Mikkel Rummelhoff
<p>I need to save page views for some pages.</p>
<p>I ended up doing it with this code:</p>
<pre><code>{% set pageviews = entry.pageviews %}
{% do entry.setFieldValue('pageviews', pageviews+1) %}
{% do craft.app.getElements().saveElement(entry) %}
</code></pre>
<p>It's working fine but it slows down significantly the page with an increase of around 100 database queries which I cannot explain.</p>
<p>Is there a more efficient way of doing this?</p>

<entries><performance><saving>
Updating entry field
# Updating entry field

(Asked by: username_0 on 2022-03-07)

I need to save page views for some pages.
I ended up doing it with this code:
{% set pageviews = entry.pageviews %}
{% do entry.setFieldValue('pageviews', pageviews+1) %}
{% do craft.app.getElements().saveElement(entry) %}

It's working fine but it slows down significantly the page with an increase of around 100 database queries which I cannot explain.
Is there a more efficient way of doing this?

**Tags:** entries, performance, saving


---

## 1 answers

### Answer by username_1 on 2022-03-07. Score: 1 | Accepted answer

It's natural that you're seeing a lot of extra queries; Craft does a ton of work when saving an entry (creating/pruning revisions, updating search indexes, propagating elements etc). You're probably also going to have your template caches invalidated on every pageload. In short, re-saving entries on every frontend request is a bad idea.
A much better approach is using Craft's seq() function:
{# Increment page views for the current entry, without outputting the current count #}
{% do seq('pageviews:' ~ entry.id) %}

{# Output page views for the current entry, without incrementing #}
{{ seq('pageviews:' ~ entry.id, next=false) }}

{# Increment _and_ output the current count #}
{{ seq('pageviews:' ~ entry.id) }}

The seq() function doesn't interface with custom fields, though. If you do need to have the page view count stored in a custom field for some reason, a possible workaround in terms of the performance issue is going through the Content service to save the updated custom field value instead of actually re-saving the entry using craft.app.getElements():
{% set pageviews = entry.pageviews %}
{% do entry.setFieldValue('pageviews', pageviews+1) %}
{% do craft.app.getContent().saveContent(entry) %}

The above is pretty hacky though, and not really a much better idea than actually re-saving the entry in full (although it does result in less queries being executed). It's definitely not an approach I would recommend.
Finally, there's also a few plugins available for storing view counts, for example this or this.

> **Comments:**
> **username_0**: Great thank you so much! I did not know the seq() function, very useful.  

---

