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

pt-query-digestはPerl!

 pt-query-digestはPerl!

2019-11-03 YAPC::Nagoya::Tiny 2019
https://yapcjapan.connpass.com/event/146727/

Takafumi ONAKA

November 03, 2019
Tweet

More Decks by Takafumi ONAKA

Other Decks in Technology

Transcript

  1. ޮ͍ͯͳ͍ΫΤϦൃݟʂ /* lib/Giga/Core/Repository/SeriesComment.pm at line 200 */ SELECT COUNT(*) FROM

    `series_comment` WHERE (`is_public` = '1') AND (`series_id` = ‘10834108156628842496');
 +----+-------------+-||-+-------+------------------------------------+ | id | select_type | || | rows | Extra | +----+-------------+-||-+-------+------------------------------------+ | 1 | SIMPLE | || | 30872 | Using index condition; Using where | +----+-------------+-||-+-------+------------------------------------+ 1 row in set (0.05 sec)
  2. ͪΐͬͱҧ͏ͷ͕͋Δͧʜʜʁ /* lib/Giga/Core/Repository/SeriesComment.pm at line 200 */ SELECT COUNT(*) FROM

    `series_comment` WHERE (`series_id` = ‘10834108156628842496') AND (`is_public` = '1')\G 
 /* lib/Giga/Core/Repository/SeriesComment.pm at line 200 */ SELECT COUNT(*) FROM `series_comment` WHERE (`is_public` = '1') AND (`series_id` = '10834108156628842496')\G
  3. આ໌͠Α͏ʂ w )BTI3BOEPNJ[BUJPOͱ͸ w ϋογϡ͝ͱʹLFZT ΍WBMVFT FBDI ͕ϥϯμϜͳॱ൪ʹͳ Δ͜ͱ w

    ಉ͡಺༰ LFZ WBMVF ͷϋογϡͰ΋ҧ͏ΦϒδΣΫτͳΒҧ͏ॱ ংͰฦΔ w 1FSMWͰ࠾༻͞Ε·ͨ͠
  4. ࣮ίʔυ sub get_public_comments_count { args my $class => 'ClassName', my

    $series => 'Giga::Core::Model::Series', ; my $core_db = Giga::DB->new->core; return $core_db->count( 'series_comment', '*', +{ series_id => $series->id, is_public => 1, }, ); }
  5. 42-.BLFSͷ࣮૷ my $w = $self->new_condition; my @w = ref $where

    eq 'ARRAY' ? @$where : %$where; while (my ($col, $val) = splice @w, 0, 2) { $w->add($col => $val); }
  6. 1FSM͡ΌΜ #!/usr/bin/env perl # This program is part of Percona

    Toolkit: http://www.percona.com/soft # See "COPYRIGHT, LICENSE, AND WARRANTY" at the end of this file for l # notices and disclaimers. use strict; use warnings FATAL => 'all'; # This tool is "fat-packed": most of its dependent modules are embedde
  7. ߦ਺ͷଟ͍ཧ༝͸෼͔ͬͨ # This tool is "fat-packed": most of its dependent

    modules are embedded # in this file. Setting %INC to this file for each module makes Perl aware # of this so it will not try to load the module from @INC. See the tool's # documentation for a full list of dependencies. wQBDLBHFQU@RVFSZ@EJHFTU͸ߦ໨ ͔Βͷߦɻ͜ΕͳΒಡΊΔ
  8. pOHFSQSJOU͸2VFSZ3FXSJUFSʹ ࣮૷͞Ε͍ͯΔ $query =~ s/\\["']//g; # quoted strings $query =~

    s/".*?"/?/sg; # quoted strings $query =~ s/'.*?'/?/sg; # quoted strings $query =~ s/\bfalse\b|\btrue\b/?/isg; # boolean values ... $query =~ s/\A\s+//; # Chop off leading whitespace chomp $query; # Kill trailing whitespace $query =~ tr[ \n\t\r\f][ ]s; # Collapse whitespace $query = lc $query; $query =~ s/\bnull\b/?/g; # Get rid of NULLs $query =~ s{ # Collapse IN and VALUES lists \b(in|values?)(?:[\s,]*\([\s?,]*\))+ } {$1(?+)}gx;
  9. ࣮຿Ͱ͸42-.BLFSΛ্ॻ͖ sub select_query { my ($self, $table, $fields, $where, $opt)

    = @_; if (ref($where) eq 'HASH') { $where = [map { ($_, $where->{$_}) } (sort keys %$where)]; } $self->SUPER::select_query($table, $fields, $where, $opt); }