<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>

CC BY-SA 3.0
2018-01-11T12:18:03.877Z
Thanks, this whole answer worked, including the final edit.
Mark
CC BY-SA 3.0
2018-01-11T10:51:53.137Z
2018-01-11T11:15:17.477Z
2018-01-11T11:15:17.477Z
Robin Schambach
<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>

CC BY-SA 3.0
2018-01-11T10:40:24.377Z
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
Robin Schambach
CC BY-SA 3.0
2018-01-11T10:42:25.537Z
Yeah I could do that, which part?
Mark
CC BY-SA 3.0
2018-01-11T10:57:54.660Z
please see my answer (note if you already saw it, i edited it multiple times should be the final version for now
Robin Schambach
<entry><performance><eager-loading><count>
Entry Count plugin performance / template optimisation
# 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 %}                       
        {{ entry.postDate.format('jS F Y') }}{{ entry.title }}                        
    {% 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
// 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 to
public function getEntries($limit = null)
{
    return craft()->entryCount->getEntries($limit);
}

> **Comments:**
> **username_0**: Thanks, this whole answer worked, including the final edit.  

---

