Remove Unnecessary Mysql Query

my single posts is getting 31 query from mysql. I checked all queries. I saw this query.

I am using last posts and categories widget. I think there query is unnecessary.How to remove theme

Read More
Query: SELECT option_value FROM xy_options WHERE option_name = 'widget_pages' LIMIT 1
Query: SELECT option_value FROM xy_options WHERE option_name = 'widget_calendar' LIMIT 1
Query: SELECT option_value FROM xy_options WHERE option_name = 'widget_tag_cloud' LIMIT 1
Query: SELECT option_value FROM xy_options WHERE option_name = 'widget_nav_menu' LIMIT 1

my sidebar is here https://gist.github.com/4546327

Related posts

Leave a Reply

2 comments

  1. Best I can tell is that those default widgets get options from the database as their classes are constructed, so there’s no way to prevent the DB queries without disabling those widgets entirely. Depending on your needs, you may want that slight performance bump and do not need the widgets, in which case you can use this code to disable them (place in functions.php):

    // Remove unneeded widgets that have undesirable query overhead
    add_action( 'widgets_init', 'remove_unneeded_widgets' );
    function remove_unneeded_widgets() {
        unregister_widget('WP_Widget_Pages');
        unregister_widget('WP_Widget_Calendar');
        unregister_widget('WP_Widget_Tag_Cloud');
        unregister_widget('WP_Nav_Menu_Widget');
    }
    

    See here for more default widget class names:
    http://codex.wordpress.org/Function_Reference/unregister_widget

    UPDATE:

    I did some more research and discovered that these four widgets do not have any auto-loaded options, even though several other widgets do have auto-loaded options set in the wp_install_defaults() function in /wp-admin/includes/upgrade.php. This may be an oversight. In any event, if these widgets have never been loaded into a sidebar, the WP_Widget parent class or the widget class have never had the opportunity to set any defaults. The other widgets are constructed and use get_option() but don’t produce a query because they’re autoloaded with lots of other WordPress options. These four don’t have autoloaded options so get_option() has to query the database for each.

    The simplest solution is just to assign defaults when the theme is first activated, like so:

    // Clean up widget settings that weren't set at installation
    add_action( 'after_switch_theme', 'set_missing_widget_options' );
    function set_missing_widget_options( ){
        add_option( 'widget_pages', array ( '_multiwidget' => 1 ) );
        add_option( 'widget_calendar', array ( '_multiwidget' => 1 ) );
        add_option( 'widget_tag_cloud', array ( '_multiwidget' => 1 ) );
        add_option( 'widget_nav_menu', array ( '_multiwidget' => 1 ) );
    }
    
  2. From a glance at your Code, your sidebar.php is quite extensive and variable.

    The Options are not directly loaded in your sidebar.php, I suppose them to be loaded in your widgets, of which you seem to have a few. As your Theme seems to have a lot of Options available, you need to load all those values to ensure all features.

    I strongly recommend not messing with the code in this case, as you could lose features you may later need, and the 4 calls of get_option() that produce the above query should be handled in no time.