Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-03-07T11:03:29.260Z
Answers.Text: Great thank you so much! I did not know the seq() function, very useful.
Answers.UserDisplayName: Samuel
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-03-07T10:40:20.220Z
Answers.LastActivityDate: 2022-03-07T10:45:51.493Z
Answers.LastEditDate: 2022-03-07T10:45:51.493Z
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Body: <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>
  
Tags: <entries><performance><saving>
Title: Updating entry field
ThreadText: # 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()`](https://craftcms.com/docs/3.x/dev/functions.html#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](https://docs.craftcms.com/api/v3/craft-services-content.html#public-properties) 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](https://plugins.craftcms.com/view-count) or [this](https://plugins.craftcms.com/entry-count).*
  
  > **Comments:**
  > **username_0**: Great thank you so much! I did not know the seq() function, very useful.  
  
  ---
  
  