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

Advanced Profiler Usage

In addition to query inspection, the profiler also allows the developer to filter which queries get profiled. The following methods operate on a Zend_Db_Profiler instance:

Filter by query elapsed time

setFilterElapsedSecs() allows the developer to set a minimum query time before a query is profiled. To remove the filter, pass the method a NULL value.

<?php
// Only profile queries that take at least 5 seconds:
$profiler->setFilterElapsedSecs(5);

// Profile all queries regardless of length:
$profiler->setFilterElapsedSecs(null);

Filter by query type

setFilterQueryType() allows the developer to set which types of queries should be profiled; to profile multiple types, logical OR them. Query types are defined as the following Zend_Db_Profiler constants:

  • Zend_Db_Profiler::CONNECT: connection operations, or selecting a database.

  • Zend_Db_Profiler::QUERY: general database queries that do not match other types.

  • Zend_Db_Profiler::INSERT: any query that adds new data to the database, generally SQL INSERT.

  • Zend_Db_Profiler::UPDATE: any query that updates existing data, usually SQL UPDATE.

  • Zend_Db_Profiler::DELETE: any query that deletes existing data, usually SQL DELETE.

  • Zend_Db_Profiler::SELECT: any query that retrieves existing data, usually SQL SELECT.

  • Zend_Db_Profiler::TRANSACTION: any transactional operation, such as start transaction, commit, or rollback.

As with setFilterElapsedSecs(), you can remove any existing filters by passing NULL as the sole argument.

<?php
// profile only SELECT queries
$profiler->setFilterQueryType(Zend_Db_Profiler::SELECT);

// profile SELECT, INSERT, and UPDATE queries
$profiler->setFilterQueryType(Zend_Db_Profiler::SELECT |
                              
Zend_Db_Profiler::INSERT |
                              
Zend_Db_Profiler::UPDATE);

// profile DELETE queries
$profiler->setFilterQueryType(Zend_Db_Profiler::DELETE);

// Remove all filters
$profiler->setFilterQueryType(null);

Retrieve profiles by query type

Using setFilterQueryType() can cut down on the profiles generated. However, sometimes it can be more useful to keep all profiles, but view only those you need at a given moment. Another feature of getQueryProfiles() is that it can do this filtering on-the-fly, by passing a query type (or logical combination of query types) as its first argument; see this section for a list of the query type constants.

<?php
// Retrieve only SELECT query profiles
$profiles $profiler->getQueryProfiles(Zend_Db_Profiler::SELECT);

// Retrieve only SELECT, INSERT, and UPDATE query profiles
$profiles $profiler->getQueryProfiles(Zend_Db_Profiler::SELECT |
                                        
Zend_Db_Profiler::INSERT |
                                        
Zend_Db_Profiler::UPDATE);

// Retrieve DELETE query profiles
$profiles $profiler->getQueryProfiles(Zend_Db_Profiler::DELETE);

Zend Framework