Profiling queries with Zend_Db_Profiler_Firebug
October 14, 2008

Eran Galperin has written a great post on profiling MySQL queries with Zend_Db and optimizing them by hand.

He explains how to use Zend_Db_Profiler to analyze all SQL queries on a page and how to improve the performance of the queries by adding indexes.

Here is a quick modification to his example that will send the profiling information to firebug instead of printing it to the page.

To use Firebug all you need to do is use Zend_Db_Profiler_Firebug as the profiler instead of the default one.

$profiler = new Zend_Db_Profiler_Firebug('All DB Queries');
$params = array(
    'host' => 'localhost',
    'username' => 'dbusername',
    'password' => 'dbpassword',
    'dbname' => 'dbname',
    'profiler' => $profiler
$db = Zend_Db::factory('PDO_MYSQL', $params);

You can skip the whole second step of printing the profiling information to the page.

This code is assuming you are using Zend_Controller_Front and executing your queries in your model, view or controller files. For other use-cases see here.

Now open your Firefox web browser with Firebug and FirePHP installed and open the page you want to profile.

When you view the Firebug console you should see something like this: