DBIx-AssertIndex
view release on metacpan or search on metacpan
lib/DBIx/AssertIndex.pm view on Meta::CPAN
__assert_explain($explain_sth->fetchall_arrayref( +{} ), $statement);
}
sub __assert_explain {
my($explains, $statement) = @_;
my $clean_statement = __clean_statement($statement);
my @using_no_key = grep {__should_alert($_) } @$explains;
return unless @using_no_key;
__warn->('[explain alert] ', $clean_statement);
}
sub __should_alert {
my ( $explain_by_table ) = @_;
my $extra = $explain_by_table->{Extra};
my $type = $explain_by_table->{type};
my $possible_key = $explain_by_table->{possible_keys};
# search uniq/primary key but not found any rows
return 0 if $extra and $extra =~ m/^Impossible/;
# not using any index
return 0 if defined $possible_key;
return 1 if $type and $type eq 'ALL';
return 0;
}
sub __clean_statement {
my $statement = shift;
$statement =~ s/\n/ /g;
$statement =~ s/\s+/ /g;
return $statement;
}
sub __warn {
my($message, $statement, $using_no_key) = @_;
if (ref $OUTPUT eq 'CODE') {
$OUTPUT->(
message => $message,
statement => $statement,
using_no_key => $using_no_key,
);
} else {
print {$OUTPUT} $message, ' statement:', $statement;
}
}
1;
__END__
=head1 NAME
DBIx::AssertIndex - show error when SQL query doesn't use index.
=head1 SYNOPSIS
use DBIx::AssertIndex;
my $row = $dbh->selectrow_hashref(q{SELECT * FROM some_table WHERE no_indexed_column = 'foo'});
or
> starman -MDBIx::AssertIndex app.psgi
=head1 DESCRIPTION
DBIx::AssertIndex is run explain with SELECT SQL and detect query without any index.
Works only DBD::mysql.
=head1 AUTHOR
daichi hiroki E<lt>daichi.hiroki@mixi.co.jpE<gt>
=head1 SEE ALSO
=head1 LICENSE
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=cut
( run in 1.185 second using v1.01-cache-2.11-cpan-e93a5daba3e )