Improve WordPress Comments Load time when using Akismet: Adding an index to wp_comments

A customer of mine was seeing a 15 second load time on their WordPress site when clicking on “Comments”.

I installed Query Monitor and saw that the Akismet plugin was running the following SQL command on each comment.

SELECT COUNT(*) FROM wp_comments WHERE user_id = 1820 AND comment_approved = 1;

This query was to show how many comments the user had previously had approved. The query was taking 0.5 seconds per comment listed when you visit the “Comments” page. The page was taking around 15 seconds to load at times. Now if you had this page set to load 100 comments per page, then you’re looking at 60 seconds.

I decided to run an explain on the query to figure out what was going on.

explain SELECT COUNT(*) FROM wp_comments WHERE user_id = 778 AND comment_approved = 1;

Which resulted in the following. (Sorry for the screenshot, copy/pasting and reformatting was not something I wanted to do. Also this references the wp_bspr_comments. This is due to the database having a wp_bspr for it’s table prefix.

MySQL Explain on wp_comments query not indexed

As you can see, the query is having to go through 19866 rows within the wp_comments table which is the total amount in the table. Since they’re using count(*) all rows are scanned.

So I decided to add an index for the user_id and comment_approved columns.

create index wp_comments_askismet ON wp_comments (user_id,comment_approved);

No when running an explain, there are only 9 rows scanned.

MySQL Explain on wp_comments with Aksimet after index

Load times were reduced dramatically. However, this can all be mitigated if you simply turn of the Akismet feature.

Akismet option to show number of approved comments beside each comment author.


Did you like this article?


0 Shares:
You May Also Like

Western Digital developing 20,000RPM drives

That's right, this isn't a joke.
The new drive will be very similar to the recently-released VelociRaptor, in that it’ll be a 2.5in drive with a custom 3.5in housing built around it. Details are incredibly light at this stage, given that the product is still in development, and we don’t even have a release timeframe at the moment. However, our sources said that the drive will be ‘silent’ – that’s the last thing I would have expected from a drive with platters spinning at 20,000 RPM. Western Digital is apparently working on silencing the beast by improving the housing technology, which will now not just act as a heatsink, but also as a noise cancelling device. We’d also hope that the drive enclosure has some vibration dampening technology as well, because that’s also likely to be a problem given the high spindle speeds.
Now they just need push SATA 6.0Gbit/s onto motherboards http://en.wikipedia.org/wiki/SATA#SATA_6.0_Gbit.2Fs Full artile at bit-tech.net
Read More