Like the blog? Get the book »

Show Off Your WordPress Database Statistics

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

  1. Excellent tip to use an if statement for keeping the statistics private.

  2. 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)

  3. Shouldn’t it be
    if (current_user_can('level_10')) {
    without the ! ?

  4. Ryan Rampersad

    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.

    • Thanks for the feedback, Ryan. Of course there’s probably a plugin for everything these days, but we may not learn as much from using them ;)

  5. 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.

  6. 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.

    • That’s an excellent question, Steven. About the only thing I have seen posted on the subject is from Konstruktors Notes.

      Not sure about a one-size-fits-all “gold standard,” but it is interesting to read about others’ statistics.

  7. Nice tip

  8. 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

    • Lol, that’s my sad attempt at finding an interesting image for “showing off,” as per the post title. Also wanted something “summer-ish.” Fail?

      • hehe…Ok I see it now. I was looking at it like, “Sheesh, I’m giving up! Yikes . . . and awayyy.”

        I know what you’re saying though, Jeff. I’ve given up on providing an image for every post. It sometimes takes me as long to find an image as it does to write a post. I haven’t really noticed any difference in blog readership.

  9. 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.

  10. weh.. nice info… i like it

Comments are closed for this post. Contact us with any critical information.
© 2009–2024 Digging Into WordPress Powered by WordPress Monzilla Media shapeSpace