Feb17

Query Profiling with Zend Framework and FirePHP

at 8.47am

My last post was about logging with Zend Framework and FirePHP. This one is similar but, for me at least, just as useful. The Zend Framework can use the WildFire protocol (that allows client-server communication through HTTP headers) to allow the PHP database adapter to report directly to FirePHP about every query that goes through it. Along with a record of the query is the time the query took to execute. This allows you to see where the bottlenecks are in your SQL or simply whether you are running too many queries.

The set-up for this is even easier than before. Again, if you are using Zend_Controller_Front then this needs to be executed before you dispatch, otherwise before you send headers. The variable $db mentioned should be an instance of Zend_Db_Adapter_Abstract.

$profiler = new Zend_Db_Profiler_Firebug('SQL Queries');
$profiler->setEnabled(true);
 
$db->setProfiler($profiler);

As in the last example I normally wrap this in a conditional statement that detects whether profiling is turned on in the config file. This allows for the output to be disabled at the change of a paramter.

if($config->profiling->db == 'true') {
    $profiler = new Zend_Db_Profiler_Firebug('SQL Queries');
    $profiler->setEnabled(true);
 
    $db->setProfiler($profiler);
}

More information can be found in the relevant section of the Zend Framework Reference Guide.

Comment on This Post

The XHTML tags <a>, <abbr>, <acronym>, <b>, <blockquote>, <cite>, <code>, <del>, <em>, <i>, <q>, <strike>, <strong> and <pre> can be used in your comment. Your e-mail address will not be displayed.

* Required field

This Post

This entry was posted on Tuesday, February 17th, 2009 at 8:47 am in PHP, Web, Zend Framework.

You can leave a comment below or trackback from your own site. Discussion can be followed through the entry's RSS 2.0 feed.