Free Zinio Magazines for iPad (or your web browser)

FreeMagazines.jpg

Thanks to a post from Gear Diary, I found out that you can download quite a few magazines on your iPad through the Zinio app (or actually, you can also read these magazines on the web using your web browser, if you don’t own an iPad!). At the moment you have to pay a subscription [...]

Reduce WordPress CPU Usage #3 – Reduce your database queries

by Michael Aulia on 11 June, 2009 in Blogging



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!

Bookmark and Share

{ 28 comments… read them below or add one }

Lyndi June 11, 2009 at 3:58 am

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.

Reply to this comment

Michael Aulia June 12, 2009 at 1:06 am

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

Megaphase.info June 11, 2009 at 6:32 am

I’ll have to keep this in mind. In your opinion, how good is Wordpress when it comes to CPU usage (normal wordpress)

Reply to this comment

Michael Aulia June 12, 2009 at 1:06 am

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

Nihar June 11, 2009 at 4:21 pm

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.

Reply to this comment

Ricky June 13, 2009 at 1:15 am

Nice post, I also use Arthemia Theme, what other sections on the theme you found were causing a lot of queries?. thanks!

Reply to this comment

Michael Aulia June 13, 2009 at 1:37 am

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

Atul June 23, 2009 at 1:50 pm

really a nice post

Reply to this comment

Favorite Stocks October 23, 2009 at 12:04 pm

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) My ComLuv Profile

Reply to this comment

Michael Aulia October 25, 2009 at 2:23 am

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

kompi November 17, 2009 at 3:00 am

HI!

I have big problems to, with my Wp site (Arthemia) memorie usage, caused by TimThumb. How can i reduce this?

website: http://filmtrailer.hu/

Reply to this comment

Michael Aulia November 17, 2009 at 11:29 pm

Well you can reduce the number of posts on the home page, hack the template to remove the video and random posts as well. Or move to a different web hosting? :) I moved and the problems went away

Aaron December 18, 2009 at 8:37 am

Is there anything like num_queries; ?> which will actually list the queries that were run on that page? It would be a useful diagnostic tool.

Reply to this comment

Michael Aulia December 19, 2009 at 7:36 pm

You mean what the queries are rather than just the total number of queries? I guess that’d be cool to know

Aaron December 23, 2009 at 6:58 am

It’d be ESSENTIAL to know if you want to really optimize a page. Even better, optionally display only if logged in as an administrator, like when you see an Edit link for a post or page.

Michael Aulia January 18, 2010 at 9:47 pm

Yeah that’d be useful. We as the admin normally don’t really care if we have to go to our own page for 2 seconds longer. The visitors, on the other hand, “don’t have time” to spare :)

Fernando Quirino January 12, 2010 at 2:21 pm

Can’t find an alternative to timthumb anywhere. Trying to contact some sites I’ve seen that have some perfect automated thumbnails but aren’t using timthumb. Any ideas?

Reply to this comment

Michael Aulia January 13, 2010 at 1:44 am

I’ve tried finding a replacement before but so far timthumb is the best script for doing auto image thumbnails unfortunately..

Fernando Quirino January 13, 2010 at 2:19 am

Found a perfect replacemant for my case scenario. The get_the_post_thumbnail tag does the job to use the post thumbnail on wordpress 2.9+, as long as the image is of the same proportions (4:3 for the example in my case) as the required thumbnail space. You need a proportional thumbnail because the crop mechanism in the “the_post_thumbnail” tag don’t work properly. Took me a few hours last night, but a I figured it out. :)

Example in the horizontal doc tab of 11 thumbnails here: http://www.zerooitocentos.org (my movie blog)

Reply to this comment

Michael Aulia January 18, 2010 at 9:45 pm

Great to hear that Fernando! I wish that WordPress crops the thumbnails like the timthumb script. It creates nice even thumbnails

Ishan@ILoveFreeSoftware March 6, 2010 at 12:14 pm

Really good post buddy. I tried to add this line of code to my theme, but it just printed “queries”. I must be doing something wrong, but I really like all other suggestions that you have in there. I will definitely try these out over the weekend.

Reply to this comment

APlusSites.com April 8, 2010 at 12:55 pm

the ‘queries’ is the last snippet of the 2 codes he provided above. i use this on my footer, you are more welcoem to use it yourself…

queries in seconds.

it will provide the amount of queries as well as the time it took to load them

APlusSites.com April 8, 2010 at 1:04 pm

woops, lets try this again this should time it should appear ;)

<?php echo get_num_queries(); ?> queries in <?php timer_stop(1); ?> seconds.

Crazy Chris Loves Wordpress April 8, 2010 at 10:37 am

thanks for the great post, i am with a multi-domain host right now and get cpu-overload notifications all the time … its such a bummer to have to go into wordpress codex and plugin strings to optomize coding. the info you mentioned above is excellent

i also want to reccomend wp-optimize for the beginners that dont know much about sql queries and tables. it will be a good tool to optimize some portions of wordpress on-the-fly and get better acquainted with understanding queries and sql tables

Reply to this comment

Michael Aulia April 9, 2010 at 11:34 pm

Thanks Chris. Sometimes it’s just the server on your web hosting and not the WordPress’ fault

Hema Latha May 8, 2010 at 11:07 am

I have added the snippet in my blog footer.

Sometimes it shows 54, 128, 19, 27 and different number of queries on each on every refresh/visit on home page (index).

I’m not a tech savvy to understand Sql database and stuff.
Though i have implemented the code you have said, I dont know what i need to do after seeing this queries.

Like,
What is the number of queries i should be having?
Should it be less than (x)-no. of queries?
Hema Latha´s last blog ..Indian Girl Sleeping Pics My ComLuv Profile

Reply to this comment

Michael Aulia May 10, 2010 at 11:17 am

Hm well there is no specific number that is right. But you can try changing themes to find out whether the theme is doing less queries to database or not (or plug-ins as comparisons)
The lesser your blog has to make queries to the database, the better, that’s the whole idea

Chris June 13, 2010 at 4:35 pm

A timely post for me. I’ve recently found myself more conscious of site performance and this is one area I’d overlooked. One recommendation I do have is to use W3 Total Cache, I’ve found that it has a huge impact on performance once it’s fully configured.

Reply to this comment

Leave a Comment

CommentLuv Enabled

{ 1 trackback }

Previous post:

Next post:



ss_blog_claim=90a03beb48f2cae080e36591e278f2e0