Upgrade to Pro — share decks privately, control downloads, hide ads and more …

DBIx::TracerでN+1クエリを検知できるようにした

return520
January 25, 2019

 DBIx::TracerでN+1クエリを検知できるようにした

- DBIx::TracerでN+1クエリを検知できるようにした
- YAPC::Tokyo 2019 前夜祭 LTソン presented by 吉祥寺.pm

return520

January 25, 2019
Tweet

Other Decks in Programming

Transcript

  1. SQLͷ࣮ߦΛϑοΫ͢Δɻ __PACKAGE__->add_trigger( BEFORE_DISPATCH => sub { my ($c) = @_;

    $c->{__dbix_tracer} = DBIx::Tracer->new(sub { my %args = @_; my $sql = $args{sql}; # ࣮ߦ͞ΕΔSQL }); } ) 

  2. ϑοΫͨ͠SQL͸͜Μͳײͩͬͨ͡ SELECT `id`, `fuga_id`, `read_fg`, `created_at`, `updated_at` FROM `hoge` WHERE

    (`read_fg` = ?) AND (`fuga_id` = ?) վߦফ͚ͩ͢Ͱେৎ෉ͦ͏ɻ ྲྀΕ͍ͯΔSQLΛਖ਼نԽ͢Δ
  3. count͕Ұఆ਺Λ௒͑ͨΒɺlogʹు͖ग़͢ɻ sub check_n1_query {
 my ($class, $query_count) = @_;
 


    foreach my $sql (keys %$query_count) {
 my $count = $query_count{$sql};
 warn “warning n+1 query count:[$count] SQL:[$sql]” if ($query_count >= 5); } }
  4. ϦϑΝΫλ݁Ռ લʢඵʣ ޙʢඵʣ ճ਺ લʢ࣌ؒʣ ޙʢ࣌ؒʣ ࠩʢ࣌ؒʣ 1.3 0.7 2504868

    904.54 487.06 417.48 0.8 0.1 597798 132.84 16.61 116.23 0.8 0.4 1406491 312.55 156.28 156.27 0.45 0.2 5547284 693.41 308.18 385.23 0.2 0.09 1605089 89.17 40.13 49.04 0.2 0.1 8369158 464.95 232.48 232.47 etc...