Reduce WordPress CPU Usage #3 – Reduce your database queries
This is my 3rd post on the post series: how to reduce WordPress CPU usage. Read the complete series on my Blogging Guide series page.
A high CPU usage can be caused by the number of calls that WordPress has to make to the database. On a WordPress blog, almost all data are taken from the database. Data such as comments, posts, the URL of the blog, the location of the CSS files, and other information needed for your plug-ins.
Some of these database calls are unavoidable but some can be avoided by simply using the WP-SuperCache plug-in. However, database calls will still be needed to generate the static file after a cache is being cleared.
Here is how to check how many database queries your theme/blog is making.
You can insert this snippet of code on your Footer.php file (or anywhere) to make it display the number of queries:
<?php echo $wpdb->num_queries; ?> <?php _e(’queries’); ?>.
As an example, I got these results from using different themes:
- Vigilance Theme: 25 queries.
- Arthemia Premium (the one that I’m using now): 58 queries.
- Original Arthemia Premium (unmodified, from the demo site): 101 queries.
Vigilance theme is a light theme that I used when I was ordered to reduce my CPU usage on my old web hosting, LunarPages. As you can see, it has the smallest number of queries made to the database.
This blog’s current theme, Arthemia Premium, makes lots of database calls (for the featured post, random posts, carousel items, etc). I already made some adjustments to reduce the number of dynamic items needed to be displayed. The result is quite amazing: database queries were cut into half.
So how can you reduce the number of database queries apart from installing WP-SuperCache?
- Reduce the number of plug-ins (Each plug-in usually stores information in the database. Some plug-ins make more calls than the others.
- If you have to use a plug-in, try reducing the number of information it needs to store whenever possible. For example, if you can turn off logging, then turn it off. Most of the time you won’t need it unless when things go wrong. For example, a spam plug-in usually has an option whether or not to show the number of spam comments it has caught in the past. Turn this off! Do you know that it will make a single database query to grab the number?
- Hardcode obvious information such as the URL of your blog (rather than grabbing it from the database / your administration settings).

Find out the absolute URL by viewing the page source and replace it on your theme editor. DON’T hardcode the title and let WordPress generates the title dynamically for SEO. - Disable post revisions with Disable Revisions plug-in. When you are writing a post, it will save a new entry everytime you save the draft onto the database. If you don’t need it, switch if off!
- Reduce the number of items and widgets on your main page and/or sidebar. I removed the random posts section from my Arthemia Premium theme because it makes “expensive” calls to the database.
Feel free to add more tips on the comments section!
|
To get the latest posts on this blog, subscribe via your favorite RSS feed reader (What is RSS?) or by entering your email address on the form below: |




Nice tips. These simple little things do really make a big difference to the load time of ones pages. I also use a plugin called WP Optimize to remove post revisions, to clean-up spam comments and to optimize the database tables.
Thanks Lyndi. I had a look at WP Optimize. It seems to manually remove post revisions but doesn’t turn the feature off. I hate seeing the ID numbers incrementing quite drastically (I use the Save Draft button too much). Guess I’m just being a perfectionist
I’ll have to keep this in mind. In your opinion, how good is Wordpress when it comes to CPU usage (normal wordpress)
I heard that WordPress is quite heavy on the CPU compared to other blogging platforms but then again, I never have any problems on HostGator now
Great post Mike.
Right now, i have random posts and popular posts, I guess my queries will definitely be more. Will paste that php code in footer and see how much my new theme makes DB calls and also check the old theme.
Nice post, I also use Arthemia Theme, what other sections on the theme you found were causing a lot of queries?. thanks!
Well to reduce the number of database queries & load on the Arthemia Premium theme, you can:
a) Reduce the number of items on showing on the main page (through the WordPress Administration settings)
b) Reduce the number of items shown on the Carousel (may not affect much)
c) Turn off the Category spoilers and Category Bar
d) Remove the Random Post (quite heavy on resource)
e) Turn off the dynamic thumbnail generation through TimThumb.php script (but this is the same as changing the whole theme altogether!)
I think that’s about it. The less you show on your blog, the less resource it will use
That’s the generic rules of thumb
really a nice post
Great article! I run Arthemia Premium and it has too many queries. I will make some changes to reduce this but what I am most interested is changes to timthumb
I cant understand why timthumb needs to recreate the thumbnail on each load? The thumbnail(s) should be saved as physical files (based on the height and width needed) when you save a post then you can reference those physical images. Are you aware of a plugin or a way to do this? I can modify timthumb to save the files but I am not sure about the workpress hooks to execute it when saving a post…
Favorite Stocks“s last blog ..AstraZeneca and Bristol-Myers report mixed data (BMY, AZN)
It already does. It has a caching system but unfortunately it still needs to call a PHP script to check whether a cache has already been created or not.
TimThumb is great although a bit pricey. My webhost, HostGator can handle this theme just fine, though