Show Off Your WordPress Database Statistics
Hello WordPress peeps! Did you know that WordPress makes it super-easy to display some basic statistics about your database performance? The information may be displayed publicly on your web page, slightly hidden in your source code, or entirely private so only you can see it. There are two basic statistics that are drop-dead easy to include on your pages:
- The number of database queries that your page is making
- How many seconds it took your server to complete those queries
The number of database queries is generated from the following WordPress template tag:
<?php echo get_num_queries(); ?>
This is a very straightforward, parameter-less function that will output the number of database queries required to generate all of the dynamic content on your page. Then, the amount of time required to complete those “x” number of queries is provided by this template tag:
<?php timer_stop(7); ?>
As shown here, this function accepts a parameter that specifies the number of significant digits to be used for the output. Here, the number “7
” tells the function to output the query-processing time to seven digits of accuracy. So we would see a number like this:
.0173788
When used together, these two WordPress functions provide an instant snapshot into the basic performance of your web pages. Here is an example of the statistical output generated by these tags working together:
33 queries in 0.333 seconds
Many of you have probably seen this information while snooping around the far corners of your favorite websites. Lots of people use this code, although not everybody uses it the same way. Here are three great techniques for including this information on your site.
Show it off publicly
If you are particularly proud of your server’s performance and would like to show off these stats to your visitors, place the following code into the footer or sidebar of your active theme:
<p><?php echo get_num_queries(); ?> queries in <?php timer_stop(1,3); ?> seconds</p>
Display it under the hood
If you would rather not clutter your design with extraneous information, but still want to keep an eye on these stats, you can “comment out” the output string so that it only appears as a comment in the source code of your web pages. To do so, place this code in your theme’s footer.php file:
<!-- <?php echo get_num_queries(); ?> queries in <?php timer_stop(1,3); ?> seconds -->
Keep it private
Last but not least, if you don’t like the idea of sharing this information with the entire world, you can limit its display to logged-in administrators only with the following code:
<?php if (current_user_can('level_10')) {
echo '<!-- ' . get_num_queries() . ' queries in ' . timer_stop(0,3) . ' seconds -->';
} ?>
This tells WordPress to check if the current user is an Administrator (i.e., Level-10 privileges) and if so output the statistical information to the web page. Here we have restricted the display of this information to the source code, but you can change this to display on the page itself by replacing the “<!--
” and “ -->
” with “<p>
” and “</p>
”, respectively.
Update
Note that there are two ways to display the timer_stop
output on the page. The timer_stop()
function actually accepts two parameters, $display
and $precision
. Here are their default values:
<?php timer_stop($display = 0, $precision = 3) ?>
As you might guess, $display
is an integer that determines whether or not the function outputs the results to the web page (1
for output, 0
for no output). Thus, you may either use 0
for the $display
parameter and explicitly echo
the results; or you may use 1
for $display
and omit the explicit echo
.
You can see the latter method in the first two examples in this post, and the former in the third. Thanks to Rarst for pointing this out :)
24 responses
-
Excellent tip to use an if statement for keeping the statistics private.
-
Points off for the use of the idea of Levels, other than that, all good :)
(‘manage_options’ or ‘administrator’ would be a better option IMO)
-
Could you show us how you would write that piece of code? :)
-
@Jeff
Thanks a lot! :) -
How could I enclose all the capabilities of the Admin? (not only ‘manage_options’)
-
-
Shouldn’t it be
if (current_user_can('level_10')) {
without the!
? -
I really like the idea of using levels to determine whether to show it to the viewer. There’s probably a plugin somewhere that does this automatically.
-
I wrote a small plugin to store and track load times for WP last year. I’ve been meaning to release it to wordpress.org, but haven’t had the time. I could probably extend it to include a sidebar widget.
-
Do you guys normally shoot for a target number, or do you typically let the queries pile up however high? I’ve not heard of a “gold standard” for database queries with WP.
-
Nice tip
-
Jeff, excellent post and I’ve pasted in the code for our site. But I’m left wondering, what’s the deal with that guy jumping off the cliff in the picture? hehe
-
Implementing some tweaks from here in my new theme. :)
I think it is supposed to be timer_stop(0,3) otherwise it echoes time on execution, duplicating it before comment opens.
-
Thanks for that little update Rarst, had been wondering where the extra output came from!
-
-
weh.. nice info… i like it