Information about database queries, memory consumption and page load time can greatly help any developer and even common user. Lets examine situation when your wordpress theme takes several seconds to load themselves, or while loading something cause a high loading of db. How to identify where is a problem? First of all – check all queries, check memory and then locate bottleneck.
There are dozen plugins which adds data like page loading time or queries count to WP admin panel or show them below footer. You can often find this in the footer of many sites and it may say something like: “db - 80 queries; loading time - 2.2 seconds”.
But in a case of performance and, as we also note it, better understanding of WP engine (how to deal with WP function and so on) we can retrieve all what we need by utilizing our own code.
The easiest way is to paste the following code at the end of footer.php (for your current theme), right before closing
Save changes and refresh the page. You will get at the bottom the number of queries and the execution time, something like this: 20 queries in 0.305 seconds
Now a little bit about these functions. get_num_queries() - retrieve the number of database queries during the WordPress execution (located in wp-includes/functions.php.)
timer_stop() - returns the amount of time (in seconds) to generate the page. Function timer_stop has two parameters: display and precision.
Note: following code should not be used on a live site, it have a performance impact.
Let’s define 'SAVEQUERIES' in wp-config.php.
define('SAVEQUERIES', true );
The SAVEQUERIES definition saves the database queries to a array and that array can be displayed to help analyze those queries. The information saves each query, what function called it, and how long that query took to execute.
Then in the footer of your theme put this:
<?php
global $wpdb;
echo "<pre wp-pre-tag-0></pre>";
?>