package Catalyst::TraitFor::Model::DBIC::Schema::QueryLog; our $VERSION = '0.03'; # ABSTRACT: L support for L use namespace::autoclean; use Moose::Role; use Carp::Clan '^Catalyst::Model::DBIC::Schema'; use DBIx::Class::QueryLog; use DBIx::Class::QueryLog::Analyzer; with 'Catalyst::Component::InstancePerContext'; has 'querylog' => ( is => 'rw', isa => 'DBIx::Class::QueryLog', ); has 'querylog_args' => ( is => 'rw', isa => 'HashRef', default => sub { {} }, ); has 'querylog_analyzer' => ( is => 'rw', isa => 'DBIx::Class::QueryLog::Analyzer', lazy_build => 1 ); sub _build_querylog_analyzer { my $self = shift; return DBIx::Class::QueryLog::Analyzer->new( { querylog => $self->querylog } ); } sub build_per_context_instance { my ( $self, $c ) = @_; my $schema = $self->schema; my $querylog_args = $self->querylog_args; my $querylog = DBIx::Class::QueryLog->new($querylog_args); $self->querylog($querylog); $self->clear_querylog_analyzer; $schema->storage->debugobj($querylog); $schema->storage->debug(1); return $self; } 1; __END__ =pod =head1 NAME Catalyst::TraitFor::Model::DBIC::Schema::QueryLog - L support for L =head1 VERSION version 0.03 =head1 SYNOPSIS use base qw/Catalyst::Model::DBIC::Schema/; __PACKAGE__->config({ traits => ['QueryLog'], connect_info => ['dbi:mysql:master', 'user', 'pass'], }); # or __PACKAGE__->config({ traits => ['QueryLog'], connect_info => ['dbi:mysql:master', 'user', 'pass'], querylog_args => { passthrough => 1, }, }); =head1 DESCRIPTION check L for more details Enable L support for L. =head2 METHODS =over 4 =item querylog an instance of L. =item querylog_analyzer an instance of L. =item querylog_args passed to DBIx::Class::QueryLog->new; =back =head2 EXAMPLE CODE

Query Log Report

[% SET total = c.model('FilmDB').querylog.time_elapsed | format('%0.6f') %]
Total SQL Time: [% total | format('%0.6f') %] seconds
[% SET qcount = c.model('FilmDB').querylog.count %]
Total Queries: [% qcount %]
[% IF qcount %]
Avg Statement Time: [% (c.model('FilmDB').querylog.time_elapsed / qcount) | format('%0.6f') %] seconds.
[% SET i = 0 %] [% FOREACH q = c.model('FilmDB').querylog_analyzer.get_sorted_queries %] [% IF i == 5 %] [% LAST %] [% END %] [% SET i = i + 1 %] [% END %]
5 Slowest Queries
Time % SQL
[% q.time_elapsed | format('%0.6f') %] [% ((q.time_elapsed / total ) * 100 ) | format('%i') %]% [% q.sql %] : ([% q.params.join(', ') %])
[% END %]
OR my $total = sprintf('%0.6f', $c->model('DBIC')->querylog->time_elapsed); $c->log->debug("Total SQL Time: $total seconds"); my $qcount = $c->model('DBIC')->querylog->count; if ($qcount) { $c->log->debug("Avg Statement Time: " . sprintf('%0.6f', $total / $qcount)); my $i = 0; my $qs = $c->model('DBIC')->querylog_analyzer->get_sorted_queries(); foreach my $q (@$qs) { my $q_total = sprintf('%0.6f', $q->time_elapsed); my $q_percent = sprintf('%0.6f', ( ($q->time_elapsed / $total) * 100 )); my $q_sql = $q->sql . ' : ' . join(', ', @{$q->params}); $c->log->debug("SQL: $q_sql"); $c->log->debug("Costs: $q_total, takes $q_percent"); last if ($i == 5); $i++; } } =head2 SEE ALSO L L L =head1 AUTHOR Fayland Lam =head1 COPYRIGHT AND LICENSE This software is copyright (c) 2010 by Fayland Lam. This is free software; you can redistribute it and/or modify it under the same terms as perl itself. =cut