Display The Number of Database Queries In WordPress

When you are preforming maintenance on a WordPress blog one of things you need to check for is the speed of your WordPress database. You need to make sure that the amount of queries is down to a minimum and that there isn't anything holding up the database.

In WordPress there is a couple of inbuilt functions that allow you to display the amount of SQL queries and the time it took them to run on any page.

Using this display you can then see what affect different changes make to your WordPress database.

Some of the tips mentioned in 11 Steps To Speed Up Your WordPress Site talk about database optimisation and reducing database calls by caching. It is very useful to take note of how long a page takes to load before and after you make these changes to see what affect they have.

Below a snippet you can use to display both the amount of queries and the time it took them to run. But you don't want these stats to be displayed to every visitor to your site so make sure you wrap them in a if statement to check if the user is logged in.

<?php 
if(is_user_logged_in()){
     echo get_num_queries().' queries in '. timer_stop(1) .' seconds.';
}
?>

Keep updated with the latest tutorials and snippets.

Join me on Twitter | G+ | Facebook

Comment