• Resolved gnif

    (@gnif)


    One of my clients is using this plugin on a rather busy shop and after investigating why the site is horrendously slow this plugin was identified. Each page load generates thousands of database requests to generate the counts, and the included cache feature makes matters worse.

    1) The cache table is created with two text columns with no index as below.
    CREATE TABLE IF NOT EXISTS woof_cache_table (mkey text NOT NULL,mvalue text NOT NULL)

    This should be not only indexed but the first column doesn’t even need to be text as it’s only storing a md5 hash of the query. Huge performance gains are to be had by changing this to

    CREATE TABLE IF NOT EXISTS woof_cache_table (mkey VARCHAR(255) NOT NULL,mvalue text NOT NULL, PRIMARY KEY(mkey))

    2) The cache table is not checked for preexisting entries allowing it to grow infinitely. The INSERT INTO query should be changed to use ON DUPLICATE KEY UPDATE syntax after adding the primary key as noted in #1.

    3) Finally, why is this plugin even using it’s own cache table to begin with? Even with the above changes the performance is still very poor due to the hundreds of queries it runs simply to obtain the cached counts. Instead it should be using wp_cache_set and wp_cache_get which not only would handle the cache expire time, but also allow cache plugins like Memcached to be used.

    You can get this functionality by altering classes/counter.php, replacing the contents of set_value with:

    
    wp_cache_set($this->key_string, $this->post_count, 604800);
    

    where 604800 is your desired cache expire time in seconds. And replacing the contents of get_value with:

    
    $value = wp_cache_get($this->key_string);
    if ($value === false)
      return -1;
    return $value;
    

    The total MySQL execution time before the above, WITH the caching AFTER the fixes in #1 and #2 was ~400ms, after this it has been reduced to ~97ms.

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Works well but needs some performance improvements.’ is closed to new replies.