WordPress

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.';
}
?>
Back to top ^

Join Paulund

  • Get access to 100s of premium tutorials and downloadable content
  • Members content consists of Premium WordPress plugins, CSS packages, jQuery packages, tutorial demo files and templates for 100s of web development tutorials
  • In-depth development tutorials
  • Priority tutorial requests
  • Priority email development support
  • No ads

Here at Paulund you'll find Web Development tutorials and snippets focusing mainly on PHP, WordPress and CSS. Members will get access to premium tutorials that go into more detail about development with these languages. You will also get access to downloadable premium content based on the tutorial.

Join Paulund

Leave a Reply

Your email address will not be published. Required fields are marked *