Basic query monitoringBasic monitoring of a query statement is easy with PECL/mysqlnd_uh. Combined with debug_print_backtrace it can become a powerful tool, for example, to find the origin of certain statement. This may be desired when searching for slow queries but also after database refactoring to find code still accessing deprecated databases or tables. The latter may be a complicated matter to do otherwise, especially if the application uses auto-generated queries.
Example #1 Basic Monitoring
<?php The above example will output: #0 conn_proxy->query(Resource id #19, SELECT 1 AS _one FROM DUAL) #1 PDO->query(SELECT 1 AS _one FROM DUAL) called at [example.php:19] array(1) { [0]=> array(1) { ["_one"]=> string(1) "1" } } #0 stmt_proxy->prepare(Resource id #753, SELECT 1 AS _two FROM DUAL) #1 mysqli->prepare(SELECT 1 AS _two FROM DUAL) called at [example.php:22] For basic query monitoring you should install a connection and a prepared statement proxy. The connection proxy should subclass MysqlndUhConnection::query. All database queries not using native prepared statements will call this method. In the example the query function is invoked by a PDO call. By default, PDO_MySQL is using prepared statement emulation. All native prepared statements are prepared with the prepare method of mysqlnd exported through MysqlndUhPreparedStatement::prepare. Subclass MysqlndUhPreparedStatement and overwrite prepare for native prepared statement monitoring. |