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

Official Jailbreak for iPhone 2.1 Firmware Released OSX Only

There is now an official iphone-dev.org version of QuickPwn that supports the 2.1 firmware released by Apple on friday.
Some of the popular press and blogs have been backing the opposition. :-) While criticism and competition is fine it should be reported correctly, with all the facts. and certainly minus the FUD. Do you guys think we are “less and less relevant with each passing day” ? We don’t think so, and we certainly prefer our hacks to theirs. ;-) Though even if the world deems us irrelevant, the iPhone family of devices is still fun to hack!
Read More

Mobile Chat When Will it Work?

If you've had an iPhone since the launch of Apples Application Store, then you most likely have bought the app Mobile Chat. I mean who wouldn't, just looking at their features http://mobilech.at/features you'd be silly not to pick the only instant messenger on the iPhone. Granted there are Web App versions, but nothing application wise. Here are some of the features.
MobileChat supports the following protocols: * AIM/ICQ/.Mac/MobileME * Windows Live/MSN * Yahoo Messenger * GTalk * Jabber/XMPP Multiple Accounts
Read More

New Pwnage Tool 2.0.3 and QuickPwn 1.50

I haven't been keeping up with my submissions as of late so this one squeaked through. But the http://iphone-dev.org team has updated both their PwnageTool and QuickPwn Tool. There is still however no baseband unlock, just support for new jailbreaking of the new firmware.
Read More