1

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!

  • You would have to change the php code in the plugin in order to boost it's performance. Would you do that? This function github.com/putyourlightson/craft-entry-count/blob/master/… causes a really heavy overload – Robin Schambach Jan 11 '18 at 10:40
  • Yeah I could do that, which part? – Mark Jan 11 '18 at 10:42
  • please see my answer (note if you already saw it, i edited it multiple times should be the final version for now – Robin Schambach Jan 11 '18 at 10:57
1

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);
}
| improve this answer | |
  • Thanks, this whole answer worked, including the final edit. – Mark Jan 11 '18 at 12:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.