PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Using the Profiler

At any point, grab the profiler using the adapter's getProfiler() method:

<?php
$profiler 
$db->getProfiler();

This returns a Zend_Db_Profiler object instance. With that instance, the developer can examine your queries using a variety of methods:

  • getTotalNumQueries() returns the total number of queries that have been profiled.

  • getTotalElapsedSecs() returns the total number of seconds elapsed for all profiled queries.

  • getQueryProfiles() returns an array of all query profiles.

  • getLastQueryProfile() returns the last (most recent) query profile, regardless of whether or not the query has finished (if it hasn't, the end time will be NULL)

  • clear() clears any past query profiles from the stack.

The return value of getLastQueryProfile() and the individual elements of getQueryProfiles() are Zend_Db_Profiler_Query objects, which provide the ability to inspect the individual queries themselves:

  • getQuery() returns the SQL text of the query. The SQL text of a prepared statement with parameters is the text at the time the query was prepared, so it contains parameter placeholders, not the values used when the statement is executed.

  • getQueryParams() returns an array of parameter values used when executing a prepared query. This includes both bound parameters and arguments to the statement's execute() method. The keys of the array are the positional (1-based) or named (string) parameter indices.

  • getElapsedSecs() returns the number of seconds the query ran.

The information Zend_Db_Profiler provides is useful for profiling bottlenecks in applications, and for debugging queries that have been run. For instance, to see the exact query that was last run:

<?php
$query 
$profiler->getLastQueryProfile();

echo 
$query->getQuery();

Perhaps a page is generating slowly; use the profiler to determine first the total number of seconds of all queries, and then step through the queries to find the one that ran longest:

<?php
$totalTime    
$profiler->getTotalElapsedSecs();
$queryCount   $profiler->getTotalNumQueries();
$longestTime  0;
$longestQuery null;

foreach (
$profiler->getQueryProfiles() as $query) {
    if (
$query->getElapsedSecs() > $longestTime) {
        
$longestTime  $query->getElapsedSecs();
        
$longestQuery $query->getQuery();
    }
}

echo 
'Executed ' $queryCount ' queries in ' $totalTime .
     
' seconds' "\n";
echo 
'Average query length: ' $totalTime $queryCount .
     
' seconds' "\n";
echo 
'Queries per second: ' $queryCount $totalTime "\n";
echo 
'Longest query length: ' $longestTime "\n";
echo 
"Longest query: \n" $longestQuery "\n";

Zend Framework