Answers.Body: <p>This is untested since I don't have time to load the plugin, so please comment if there is a syntax error but changing <a href="https://github.com/putyourlightson/craft-entry-count/blob/master/entrycount/services/EntryCountService.php#L38" rel="nofollow noreferrer">this</a></p>
  
  <pre><code>// get all records from DB ordered by count descending
  $entryCountRecords = EntryCountRecord::model()-&gt;findAll(array(
      'order'=&gt;'count desc'
  ));
  </code></pre>
  
  <p>To something like the following should work</p>
  
  <pre><code>public function getEntries($limit = null)
  {
      $where = [
          'order' =&gt; 'count desc' 
      ];
      if($limit !== null){
          $where['limit'] = $limit;
      }
      // get all records from DB ordered by count descending
      $entryCountRecords = EntryCountRecord::model()-&gt;findAll($where);
     ....
  </code></pre>
  
  <p>And in your template</p>
  
  <pre><code>{% set countedEntries = craft.entryCount.entries(4).section('news').limit(4) %}
  </code></pre>
  
  <p>Explanation: you include a limit to the query that fetches all entries to load the same amount like the <code>ElementCriteriaModel</code> </p>
  
  <p>Edit: Maybe you have to change <a href="https://github.com/putyourlightson/craft-entry-count/blob/master/entrycount/variables/EntryCountVariable.php#L26" rel="nofollow noreferrer">this</a> to</p>
  
  <pre><code>public function getEntries($limit = null)
  {
      return craft()-&gt;entryCount-&gt;getEntries($limit);
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-01-11T12:18:03.877Z
Answers.Text: Thanks, this whole answer worked, including the final edit.
Answers.UserDisplayName: Mark
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-01-11T10:51:53.137Z
Answers.LastActivityDate: 2018-01-11T11:15:17.477Z
Answers.LastEditDate: 2018-01-11T11:15:17.477Z
Answers.UserDisplayName: Robin Schambach
Body: <p>I'm using Ben Croker's Entry Count plugin in my News channel but have noticed a huge performance hit, I know its probably down to how I am loading the entries in to my template so I am looking for some help on that please. </p>
  
  <p>The News Channel where it is being used has tens of thousands of entries.</p>
  
  <p>The server logs highlighted this query <a href="https://pastebin.com/02uF0Mat" rel="nofollow noreferrer">https://pastebin.com/02uF0Mat</a> taking up to 3 minutes to complete, therefore maxing the CPU and crashing the site.</p>
  
  <p>My template code:</p>
  
  <pre><code>{% cache globally using key "trending-news" %}                                  
      {% set countedEntries = craft.entryCount.entries.section('news').limit(4) %}
  
      {% for entry in countedEntries %}                       
          &lt;p&gt;{{ entry.postDate.format('jS F Y') }}&lt;br/&gt;&lt;a href="{{ entry.url }}"&gt;{{ entry.title }} &lt;/a&gt;&lt;/p&gt;                       
      {% endfor %}
  {% endcache %}
  </code></pre>
  
  <p>So I think this code is loading thousands of news entries and then displaying only 4, is there a way I can just load the top 4 only? </p>
  
  <p>Massive thanks in advance!</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-01-11T10:40:24.377Z
Comments.Text: You would have to change the php code in the plugin in order to boost it's performance. Would you do that? This function https://github.com/putyourlightson/craft-entry-count/blob/master/entrycount/services/EntryCountService.php#L38 causes a really heavy overload
Comments.UserDisplayName: Robin Schambach
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-01-11T10:42:25.537Z
Comments.Text: Yeah I could do that, which part?
Comments.UserDisplayName: Mark
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-01-11T10:57:54.660Z
Comments.Text: please see my answer (note if you already saw it, i edited it multiple times should be the final version for now
Comments.UserDisplayName: Robin Schambach
Tags: <entry><performance><eager-loading><count>
Title: Entry Count plugin performance / template optimisation
ThreadText: # Entry Count plugin performance / template optimisation
  
  (Asked by: username_0 on 2018-01-11)
  
  I'm using Ben Croker's Entry Count plugin in my News channel but have noticed a huge performance hit, I know its probably down to how I am loading the entries in to my template so I am looking for some help on that please.
  
  The News Channel where it is being used has tens of thousands of entries.
  
  The server logs highlighted this query <https://pastebin.com/02uF0Mat> taking up to 3 minutes to complete, therefore maxing the CPU and crashing the site.
  
  My template code:
  
  ```
  {% cache globally using key "trending-news" %}                                  
      {% set countedEntries = craft.entryCount.entries.section('news').limit(4) %}
  
      {% for entry in countedEntries %}                       
          <p>{{ entry.postDate.format('jS F Y') }}<br/><a href="{{ entry.url }}">{{ entry.title }} </a></p>                       
      {% endfor %}
  {% endcache %}
  
  ```
  
  So I think this code is loading thousands of news entries and then displaying only 4, is there a way I can just load the top 4 only?
  
  Massive thanks in advance!
  
  **Tags:** entry, performance, eager-loading, count
  
  > **Comments:**
  > **username_1**: You would have to change the php code in the plugin in order to boost it's performance. Would you do that? This function https://github.com/putyourlightson/craft-entry-count/blob/master/entrycount/services/EntryCountService.php#L38 causes a really heavy overload  
  > **username_0**: Yeah I could do that, which part?  
  > **username_1**: please see my answer (note if you already saw it, i edited it multiple times should be the final version for now  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-01-11. Score: 1 | Accepted answer
  
  This is untested since I don't have time to load the plugin, so please comment if there is a syntax error but changing [this](https://github.com/putyourlightson/craft-entry-count/blob/master/entrycount/services/EntryCountService.php#L38)
  
  ```
  // get all records from DB ordered by count descending
  $entryCountRecords = EntryCountRecord::model()->findAll(array(
      'order'=>'count desc'
  ));
  
  ```
  
  To something like the following should work
  
  ```
  public function getEntries($limit = null)
  {
      $where = [
          'order' => 'count desc' 
      ];
      if($limit !== null){
          $where['limit'] = $limit;
      }
      // get all records from DB ordered by count descending
      $entryCountRecords = EntryCountRecord::model()->findAll($where);
     ....
  
  ```
  
  And in your template
  
  ```
  {% set countedEntries = craft.entryCount.entries(4).section('news').limit(4) %}
  
  ```
  
  Explanation: you include a limit to the query that fetches all entries to load the same amount like the `ElementCriteriaModel`
  
  Edit: Maybe you have to change [this](https://github.com/putyourlightson/craft-entry-count/blob/master/entrycount/variables/EntryCountVariable.php#L26) to
  
  ```
  public function getEntries($limit = null)
  {
      return craft()->entryCount->getEntries($limit);
  }
  
  ```
  
  > **Comments:**
  > **username_0**: Thanks, this whole answer worked, including the final edit.  
  
  ---
  
  