Home Blogging Blogging Tips Reduce WordPress CPU Usage #3 – Reduce your database queries

Reduce WordPress CPU Usage #3 – Reduce your database queries

reduce wordpress database queries codeThis 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).
    reduce wordpress database queries css
    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!

Related Posts at Craving Tech

How to reduce inode usage (files and folders) with a web hosting limit If you are hosting your blog or website on a shared server (or I guess, any server), you may be hitting your inodes limit at some point. In a nuts...
How to enable SSH on VaultPress (at HostGator or other web hosting) How to enable SSH on VaultPress - If you are blogging on WordPress, there is a higher chance of you backing up your WordPress blog through VaultPr...
Custom Web Design vs Templates Note: This is a guest post written by Blake Davies After a while, your company’s website becomes obsolete and needs not only improvement but a ...
Why WordPress is a Perfect Solution for Your Business’ Blog Note: This is a guest post written by Blake Davies Originally launched in 2003 as a blogging platform, WordPress is probably the most widely-us...
How to speed up your WordPress blog by excluding certain JavaScript and CSS files If you own a WordPress blog, you should know by now that the more plugins you've got, the slower your blog/website gets. This is because each inst...