Logo Background RSS

Wordpress + Plugins = More DB Queries

  • Written by George SerradinhoGeorge Serradinho 22 Comments22 Comments Comments
    Last Updated: July 13th, 2008

    Hi,

    Wordpress + Plugins = More DB Queries

    I was thinking the other day that the queries on my site was way to high.

    Averages from my site varied from 200 to 350. Now, those number would scare off the top marketers, never mind me. I knew exactly why as I had so many queries on my page loads, is was because I had over 50 plugins installed on my wordpress platform. Many of the plugins were fancy gadgets that would help you the user and myself the webmaster.

    I finally got the guts to ask weblogtoolscollection.com the question.

    So why did I choose them?

    They seem to have loads of wordpress experience and I wanted to ask someone who could honestly give me a decent answer.

    This is my question that I posted to them:
    Hi,this is somewhat of a weird question, but I need to ask it.When you have a wordpress website, how many database queries is to much.

    I have changed my website to include many plugins and the queries vary from
    just over 200 to just below 350, depending on the page you request.

    Is that to much or is it ok?

    Thanks
    George

    The reply I got was from Mark Ghosh was:
    That is a very good question with a variety of answers.
    WeblogToolsCollection uses a lot of plugins and is very query heavy but the
    front page uses 59 queries. The caveat is that it is highly optimized to
    reduce load because of the sheer number of requests. I think the default (on
    a vanilla WordPress install on the default theme) is 27 or something of that
    nature. The larger the number of queries, the slower the page is going to

    load and the more load you are going to have on your MySql server.

    In your case, I think 200-350 is quite a large number and is probably caused
    by plugins that you have installed. Plugin authors are notorious for having
    non-optimized and useless queries. I suspect that you could follow the 80-20
    rule with 80% of your queries being caused by 20% of the plugins. If you can
    identify the top hitters and either optimize or replace them, you will have
    a happier blog and a happier MySql server.

    Hope that helps,

    Thanks,
    Mark

    After reading his reply a few times to make sure I understood all of it, I knew what I had to do. I logged into my Wordpress admin panel to check my plugins and see if I could find the problem. This may seem like a job and a half, but it took me no time at all. I’m sure I was very, very lucky. I started out by looking at what fancy gadgets I could go without, there were a few, actually over 50. So I started deactivating them one by one and checked my page load queries after each one. I found two plugins that after deactivating, my total queries dropped dramatically. I’m not saying that they are bad plugins, but I think I will do without them for now.

    The plugins were tested on my post pages:

    • Wp-Print (includes widget) by Lester ‘GaMerZ‘ Chan (added +100 extra queries)
    • Wp-Email by Lester ‘GaMerZ‘ Chan (added +100 extra queries)

    My queries were so much better after deactivating these plugins, my homepage is below 120 queries and my post pages are below 110 queries. This is good news for you, the users and me, the webmaster. I will still have to investigate further to getting my queries down to a minimum. Wish me luck. If the page loads quicker then you might browse longer, I hate when websites take long to load. I’m a bit impatient sometimes and get upset and then close the window.

    Now I ask you, is having all these nice gadgets on your website worth it?

    Thats a hard one, but the answer is simple.

    NO!

    The more plugins you have the more your database works and the more queries you’ll have at the end of loading your page. All webmasters will have this on their minds, they want the users to be happy.

    So, what I’m going to do is notify the developer about this post and see if he has anything to say. I like the concept of both plugins and have had it on my blog for sometime, it could be that the plugins conflict with other plugins. I don’t know, I will have to wait and see what the developer says. I hope there is a solution.

    They are great plugins - Thanks GaMerZ - Let me know.

Advertisement

22 Comments
  1. #1 Richard
    July 13th, 2008 at 6:23 pm

    This is well known issue and I’m sorry to say that deactivating the plugins and removing them from your directory is only HALF the job. If you go into your database using phpMyadmin or something you will probably see several tables and option settings left behind after deactivation. Ok, they may not be doing anythying now the plugin is gone, but they are still there as dead weight slowing your database down and making larger backups. You have to go into the database and manually delete these tables, entries and options and this can be quite daunting if you are not used to working with phpmyadmin. Make a backup first of course. The word plugin ‘deactivation’ is aptly used. It does not uninstall the database tables, unless the developer of the plugin writes it properly and makes a complete unintall routine. Many just don’t bother. Maybe it’s time WP make a default check to test unused tables and settings and force a notification to administrators.

    Post ReplyPost Reply
  2. #2 constant
    July 13th, 2008 at 11:34 pm

    Ok, i know that this could be a srange question too, but i have to know :mancovermouth: well, can you tell me how can i see how many queries are on my page load?
    I think i have once seen a plugin in the wordpress.org/extend/plugins pages with a description like “remove all entries from your database when a plugin is deactivate” (or something like thet), but i don’t know anymore.
    [sorry for my bad english]
    Thanks!

    Post ReplyPost Reply
  3. #3 Richard
    July 14th, 2008 at 12:13 am

    put this code in your footer I hope it renders ok here

    num_queries; ?> .

    Post ReplyPost Reply
  4. #4 Richard
    July 14th, 2008 at 12:18 am

    No, it didnt. ok, I am replacing all < for [

    =] Ok, you make the changes back to correct code.

    [?php echo $wpdb->num_queries; ?] [?php _e('queries'); ?]. [?php timer_stop(1); ?] [?php _e('seconds'); ?]

    Post ReplyPost Reply
  5. #5 George Serradinho
    July 14th, 2008 at 2:27 am

    Thanks Richard and constant for your responses. I have made a few changes and have gotten it down to over 200 now. I will have a look at my options table and see what is happening there.

    Thanks for responding.

    Post ReplyPost Reply
  6. #6 George Serradinho
    July 14th, 2008 at 2:32 am

    @Richard -
    Hi Richard, you are right in saying that the options for the plugins might still be in the options table. I will have to check.

    Post ReplyPost Reply
  7. #7 George Serradinho
    July 14th, 2008 at 2:33 am

    @Richard -
    Hi constant, Richard has been so kind to share the code with you. You can see mine in the bottom left corner next to my copyright (queries/seconds)

    Post ReplyPost Reply
  8. #8 George Serradinho
    July 14th, 2008 at 2:36 am

    @Richard -
    Thanks Richard, I appreciate it. GaMerZ who wrote most of the plugins that I installed on my site says that it is probably some other plugin that is making my queries so high. I will have to check my table first

    Post ReplyPost Reply
  9. #9 constant
    July 14th, 2008 at 6:51 am

    Thanks Richard and George. The code doesn’t seems to work, or it’s just me. I’ll try again, now i have to go to work :(
    Have a nice day!

    Post ReplyPost Reply
  10. #10 George Serradinho
    July 14th, 2008 at 8:10 am

    Hi constant,

    try this piece of code, this is exactly how I have my footer.

    < ?php echo '' . get_num_queries(); ?>/< ?php timer_stop(1); ?>

    I found a plugin that checks the options table. It seems to list the options, I will still have to manually check and remove them.

    Clean Options Plugin - http://www.mittineague.com/dev/co.php

    Post ReplyPost Reply
  11. #11 Richard
    July 14th, 2008 at 9:32 am

    I checked this site on http://pingdom.com to see how much was loading in the page. See results here http://tools.pingdom.com/fpt/?url=http://www.serradinho.com/Blog&treeview=0&column=objectID&order=1&type=0&save=false unfortunately it timed out before completing the test. What I did see was several js scripts loading. You may also need to check your header and index files to make sure they are clean of unwanted stuff. That’s another problem, WP sometimes does not clean up well after removing plugins. I see you have jquerry and prototype js and both of these add big overheads.

    Post ReplyPost Reply
  12. #12 George Serradinho
    July 14th, 2008 at 9:51 am

    @Richard -
    Thanks Richard, i will double check some things this week and see if I get my time down. Thanks for all the help.

    Post ReplyPost Reply
  13. #13 Gary Serradinho
    July 15th, 2008 at 10:32 pm

    Wow! Your query time have really come down - from an average of 8 secs the other day to under 4 secs today, well done boet :)

    Post ReplyPost Reply
  14. #14 George Serradinho
    July 16th, 2008 at 7:05 am

    @Gary Serradinho -
    Hi Gary,
    yes I have decreased it a bit which helps you the visitor. I would still like to bring down the total number of queries. I am getting to know the ins and outs of wordpress and am loving it.

    I made you an author so you can also publish posts if you want.

    Post ReplyPost Reply
  15. #15 Richard
    July 16th, 2008 at 9:41 am

    I visited here today with a clean cache and the page loads much faster that previous visits. Another thought is to use a compressed version of jquery java script. JQ is a monster of a script and you can use an online compressor to slice it down. Here’s another point. I can also see that you are calling 2 versions of jquery http://www.serradinho.com/Blog/wp-includes/js/jquery/jquery.js?ver=1.2.6‘ AND serradinho.com/Blog/wp-includes/js/jquery/jquery.js?ver=1.2.3 but then you are using serradinho.com/Blog/wp-includes/js/jquery/ui.core.js?ver=1.5.1 in the page. Something is wrong there. Plus there are generally far too many java script calls generally. Things like round corners and thickbox, (should consider slimbox) its smaller.

    Post ReplyPost Reply
  16. #16 George Serradinho
    July 16th, 2008 at 10:40 am

    @Richard -
    Richard, you are so right in the total number of scripts on my site. I’m looking into that. I know for a fact that it has to do with most of my plugins. You mentioned that you can compress the java scripts, where and how?

    Thanks for all your help.

    Post ReplyPost Reply
  17. #17 Richard
    July 16th, 2008 at 12:29 pm

    use this google search | online javascript compressor| you’ll see quite a few options. The Rhino engine seems the best. I use cForms plugin for all my forms and they use a compressed version of jquery that weighs in at 30k ! so it makes a big difference.

    Post ReplyPost Reply
  18. #18 George Serradinho
    July 17th, 2008 at 8:26 pm

    @Richard -
    Hi Richard, I see there is a plugin called ‘WP JS’ that compresses the script files into a smaller file. I will try and use this plugin. I have it installed now and the compressed version is about 26% less than normal. I have only added a few scripts to the file. I will have to check which scripts are big and add them. I think I’m using the plugin right. My file is called serradinho.js. The original file size is just under 200kb.

    Post ReplyPost Reply
  19. #19 Richard
    July 17th, 2008 at 8:55 pm

    Certainly looks smaller at 149k. I still think there is a lot of trimming you could do with some plugins. The other thing to consider is offloading images. I use steady offload here http://www.steadyoffload.com/ It cost a little bit, but basically serves all your images leaving your sever just for php stuff. Maybe worth a look

    Post ReplyPost Reply
  20. #20 George Serradinho
    July 17th, 2008 at 9:09 pm

    @Richard -
    Hi again, I see someone mention google ajax libraries. I’m having a look at that now. Thanks for the link, will check later.

    Post ReplyPost Reply
  21. #21 Mr Gray
    July 21st, 2008 at 2:32 pm

    Thanks for the code provided in the comments - I was happy to find I only had 18 queries.

    Post ReplyPost Reply
  22. #22 George Serradinho
    July 21st, 2008 at 2:35 pm

    @Mr Gray -
    Not a problem - happy to help.

    Post ReplyPost Reply
Leave a Comment