Ditch database queries, speed up WordPress

WordPress is made so it is user friendly and easy to implement; because of this fact, there are several ways that it can be optimized from it’s standard configuration. One way this can be accomplished is to reduce the number of database queries that are needed to retrieve the information to create the page, thus reducing database load and making your site load faster and saving server resources for when your post gets to the front page on Digg and just make the world a happier place overall.

The first place to go to reduce queries is your header.php file for your theme. If you open it in your favorite editor you will see many lines of code that looks something like this:

<link rel=”stylesheet” href=”<?php bloginfo(‘stylesheet_url’); ?>” type=”text/css” media=”screen” />

Specifically, the section that looks like <?php bloginfo(‘stylesheet_url’); ?> which is the php portion itself, will be what you want to replace with whatever html it generates, saving a database query each time you get rid of one of these.

*woohoo you found it SHMAY2010

While there is nothing wrong with all these lines of code being set up how they are, it is not necessary to use php to create the end content. The themes are created this way to be site/domain independent, if you are not going to change your domain name you can hard code these lines into your header, footer and additionally, some areas within your other page template files if you know what you are doing. For example, if you view your page source and locate the line above, you will see then end result looks like this:

<link rel=”stylesheet” href=”http://www.yourdomain.com/wp-content/themes/yourtheme/style.css” type=”text/css” media=”screen” />

Simply replace the line and any others like it that are in your template with the html equivalent and you will be good to go. Just remember you have done this if you ever want to change domains or utilize this theme on a different site, you will have to change the files to reflect the correct domain.

Happy experimenting, and remember.. before you attempt any file edits it is always a good idea to make sure you have a backup in case you completely screw something up :-)

2 thoughts on “Ditch database queries, speed up WordPress”

  1. Wouldn’t it be a better use of your time to use a wp caching plugin thatll take care of saving pages once they have run. It should have a much bigger impact for less effort.

    Also if you have control, I’d suggest enabling MySQL query caching – it can have a massive effect too.

    Chris’s last blog post..Choosing your niche and keywords

    • I agree, and I do use wp-super cache, but optimizing or limiting the number of queries is beneficial as well, saving the resources that are required to generate new pages, update content etc when the cached versions expire or new pages or comments are posted. Obviously the impact of this will only be noticed by very busy sites, but it never hurts to be prepared :-)

Comments are closed.