MySQL Profiler Plugin for WordPress

WordPress is extremely flexible. This flexibility has not only made it a major hit, but it has also allowed non-developers to customize it with ease. Which brings about the issue of performance. While most of the Wordpress functions are optimized for acceptable performance, incorrect usage can cause undesirable speed loss.

Code Profiling

Quoting from Wikipedia:

A (code) profiler is a performance analysis tool that, most commonly, measures only the frequency and duration of function calls, but there are other specific types of profilers (e.g. memory profilers) in addition to more comprehensive profilers, capable of gathering extensive performance data.

You may fail to notice any performance issues unless you use code profiling to assist you. For general PHP Profiling, programmers often use xdebug with all sort of PHP applications including wordpress. But, if you’re a wordpress theme developer, usually all you need is a MySQL profiler.

WP MySQL Profiler

I have created this simple plugin to assist both theme and plugin developers in MySQL Profiling. Once installed, MySQL statistics will be available at page footer.

Installation

  1. Download WP MySQL Profiler and extract the archive.
  2. Rename your wp-db.php file in wp-includes directory to wp-db-backup.php.
  3. Upload wp-db.php included in the archive to your wp-includes directory.
  4. Upload wpSqlProfiler.php to wp-content/plugins/ directory.
  5. Edit your wp-config.php. Find define (‘WPLANG’, ”); and add below:
    define('SAVEQUERIES', true);
  6. Enable “WP MySQL Profiler” plugin from the admin CP.

Usage

When logged in as an admin, you will see MySQL query details at page footer. Keep an eye on both total queries and time. Each time you add code to your templates, use the profiling information to check if an optimized function, or code snippet has been used.

If you notice high total queries or time, examine the queries and check the backtrace (more below) to find where the query originated from. Then remove or replace the unoptimized function/code.

The Backtrace

Third column in the profiler gives you the most useful information. In the example above, it shows the functions that were involved in generating the MySQL query. It started at themes/default/single.php on line 7 with the function get_header() which called function wp_head(), kubrick_header_display() and finally get_option() in the file functions.php on line 83.

Note that there’s a link “Show Full Trace”. For simplicity, the profiler doesn’t show all the function calls by default. Instead, it tries to determine function calls from plugins and themes. Since you are going to use it when coding plugins and themes, it makes sense to assume that you’re concerned with function calls originating therein.

Final Steps

If you’re going to use the same wordpress copy on a live site, make sure to:

  • delete the wp-db.php and rename wp-db-backup.php to wp-db.php. (optional)
  • remove define(‘SAVEQUERIES’, true); from wp-config.php.

Similarly, disable the WP MySQL Profiler plugin.

Ideas And Suggestions?

Lack of tools or lack of information; neither of them are valid excuses anymore. Go ahead and make sure every theme and plugin of yours performs efficiently. Do you use any similar tools to assist you in profiling? Got any suggestions? I would love to hear about them. Leave a comment!

15 Comments
  1. Wow this should be in core…
    something like debug mode would be great… combined with yoasts debug theme would be also nice: http://yoast.com/wordpress-debug-theme/

  2. My site is extremely slow. I have tried installing in the super caching plugin, and I installed this plugin–

    But now that I’m getting the information I don’t understand what it means, or what to do about it… any newbie tips? I certainly appreciate what you have done.

  3. This plugin was very helpful to me, and essentially works in WP 3.0. Details:

    You can use it to run/profile existing pages/posts (the main goal), but when you try to EDIT posts, you will get:
    PHP Fatal error: Call to undefined method wpdb::get_blog_prefix() in /Users/dwertheimer/Zend/workspaces/eclipse_pdt_2010/ETC_WP/wp-admin/includes/user.php on line 260

    That’s a function that exists in the new wp-db.php file but is not in the profiling file that you have to use to run the profiler. So, enable this plugin, and use the plugin’s wp-db.php file to VIEW existing pages, but then immediately move your wp-db.php file back into place.

  4. Awesome plugin!!!

    I was running 41-45 queries on my wp site, and was able to take it down to 14 queries after optimizing!!

    Thanks!!

  1. MySQL Profiler Plugin for WordPress | WordPressPlanet.com
  2. MySQL Profiler Plugin for WordPress | Neorack Tutorials
  3. Wordpress Belarus » Blog Archive » MySQL Profiler Plugin for WordPress
  4. Wordpress Plugins »  MySQL Profiler Plugin for WordPress
  5. MySQL Profiler Plugin for WordPress - wpparty.com
  6. WordPress Plugin Releases for 03/05 : | BlogCash4You
  7. BuddyPress Social Networking « WordPress Community Podcast - Online Radio - WebmasterRadio.FM
Leave a Reply