Class::ReluctantORM::Manual::Monitors - Inspecting SQL Activity
One of the most frustrating tasks when working with an ORM in a production environment is tracing and optimizing the SQL that is generated. CRO eases this pain somewhat by providing monitors - objects that you can configure to watch for and report on the status of SQL generation. You can even abort a query if it exceeds certain parameters. CRO comes with a useful basic toolkit of 8 Monitors, to assist in tracing and optimizing queries. It's also easy to create new monitors of your own design, which can execute arbitrary Perl.
We'll first work with Class::ReluctantORM::Monitor::Dump, which simply dumps out various information to STDERR as queries execute.
use Class::ReluctantORM::Monitor::Dump; # The 'all' flag says to dump everything, all the time my $mon = Class::ReluctantORM::Monitor::Dump->new(all => 1); # To monitor just queries on Ship objects: Ship->
Dump can also send its output to a log file of your choice, and you can also tone down the avalanche of output. See Class::ReluctantORM::Monitor::Dump.
Now run some queries. You'll see output like this:
# In code $ship->insert(); # On STDERR, gobs of info
As a query makes its journey from SQL object to rendered SQL string, through execution, several events occur which a Monitor may choose to respond to. You can pass these as flags under the 'when' option to the Monitor's new().
Notifies the monitoring system that the driver has begun work to render the given SQL object.
Notifies the monitoring system that the driver has finished transforming the SQL object.
Notifies the monitoring system that the driver has finished rendering the SQL object.
Notifies the monitoring system that the driver is about to perform a DBI execute.
Notifies the monitoring system that the driver has returned from performing a DBI execute.
Notifies the monitoring system that the driver has returned from performing a DBI fetchrow.
Notifies the monitoring system that the driver has finished the query (called $sth->finish).
Dumps the query structures to the log.
Counts the number of statements executed.
Counts the number of columns returned by a query.
Counts the number of JOINs in the query.
Monitors the total size, in bytes, of the data returned by a query.
Monitors the number of rows returned by the query.
Monitors the size, in bytes, of each individual row.
Tracks execution time of each query.
To create a Monitor, simply inherit from Class::ReluctantORM. Then implement any of the notify_* methods you see fit.
Clinton Wolfe clwolfe@cpan.org March 2010
To install Class::ReluctantORM, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Class::ReluctantORM
CPAN shell
perl -MCPAN -e shell install Class::ReluctantORM
For more information on module installation, please visit the detailed CPAN module installation guide.