Skip to content

Instantly share code, notes, and snippets.

@WebEndevSnippets
Created October 27, 2013 15:37
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save WebEndevSnippets/7183912 to your computer and use it in GitHub Desktop.
Save WebEndevSnippets/7183912 to your computer and use it in GitHub Desktop.
WordPress: Reduce Database Queries
Reduce database queries (http://www.catswhocode.com/blog/speeding-up-your-wordpress-blog-7-effective-solutions)
It is important to reduce unecessary queries to your database as each query take a few milliseconds to execute. First, you might want to know how many queries your blog execute in order to display a page. To do so, paste the code below in your functions.php file. Once done, just have a look to your site footer to know how many queries has been executed and how many time it took to completely load the page.
add_action( 'wp_footer', 'tcb_note_server_side_page_speed' );
function tcb_note_server_side_page_speed() {
date_default_timezone_set( get_option( 'timezone_string' ) );
$content = '[ ' . date( 'Y-m-d H:i:s T' ) . ' ] ';
$content .= 'Page created in ';
$content .= timer_stop( $display = 0, $precision = 2 );
$content .= ' seconds from ';
$content .= get_num_queries();
$content .= ' queries';
if( ! current_user_can( 'administrator' ) ) $content = "<!-- $content -->";
echo $content;
}
Then, you have to remove useless queries from your blog. Start by making sure that you are not using too many plugins, as most plugins are making database queries. Then you can remove theme-related queries that are not useful to your blog.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment