mysqldumpslow.pl 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #!/usr/bin/perl
  2. # Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU Library General Public
  6. # License as published by the Free Software Foundation; version 2
  7. # of the License.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. # Library General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Library General Public
  15. # License along with this library; if not, write to the Free
  16. # Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
  17. # MA 02110-1301, USA
  18. # mysqldumpslow - parse and summarize the MySQL slow query log
  19. # Original version by Tim Bunce, sometime in 2000.
  20. # Further changes by Tim Bunce, 8th March 2001.
  21. # Handling of strings with \ and double '' by Monty 11 Aug 2001.
  22. use strict;
  23. use Getopt::Long;
  24. # t=time, l=lock time, r=rows
  25. # at, al, and ar are the corresponding averages
  26. my %opt = (
  27. s => 'at',
  28. h => '*',
  29. );
  30. GetOptions(\%opt,
  31. 'v|verbose+',# verbose
  32. 'help+', # write usage info
  33. 'd|debug+', # debug
  34. 's=s', # what to sort by (al, at, ar, c, t, l, r)
  35. 'r!', # reverse the sort order (largest last instead of first)
  36. 't=i', # just show the top n queries
  37. 'a!', # don't abstract all numbers to N and strings to 'S'
  38. 'n=i', # abstract numbers with at least n digits within names
  39. 'g=s', # grep: only consider stmts that include this string
  40. 'h=s', # hostname of db server for *-slow.log filename (can be wildcard)
  41. 'i=s', # name of server instance (if using mysql.server startup script)
  42. 'l!', # don't subtract lock time from total time
  43. ) or usage("bad option");
  44. $opt{'help'} and usage();
  45. unless (@ARGV) {
  46. my $defaults = `my_print_defaults mysqld`;
  47. my $basedir = ($defaults =~ m/--basedir=(.*)/)[0]
  48. or die "Can't determine basedir from 'my_print_defaults mysqld' output: $defaults";
  49. warn "basedir=$basedir\n" if $opt{v};
  50. my $datadir = ($defaults =~ m/--datadir=(.*)/)[0];
  51. my $slowlog = ($defaults =~ m/--slow-query-log-file=(.*)/)[0];
  52. if (!$datadir or $opt{i}) {
  53. # determine the datadir from the instances section of /etc/my.cnf, if any
  54. my $instances = `my_print_defaults instances`;
  55. die "Can't determine datadir from 'my_print_defaults mysqld' output: $defaults"
  56. unless $instances;
  57. my @instances = ($instances =~ m/^--(\w+)-/mg);
  58. die "No -i 'instance_name' specified to select among known instances: @instances.\n"
  59. unless $opt{i};
  60. die "Instance '$opt{i}' is unknown (known instances: @instances)\n"
  61. unless grep { $_ eq $opt{i} } @instances;
  62. $datadir = ($instances =~ m/--$opt{i}-datadir=(.*)/)[0]
  63. or die "Can't determine --$opt{i}-datadir from 'my_print_defaults instances' output: $instances";
  64. warn "datadir=$datadir\n" if $opt{v};
  65. }
  66. if ( -f $slowlog ) {
  67. @ARGV = ($slowlog);
  68. die "Can't find '$slowlog'\n" unless @ARGV;
  69. } else {
  70. @ARGV = <$datadir/$opt{h}-slow.log>;
  71. die "Can't find '$datadir/$opt{h}-slow.log'\n" unless @ARGV;
  72. }
  73. }
  74. warn "\nReading mysql slow query log from @ARGV\n";
  75. my @pending;
  76. my %stmt;
  77. $/ = ";\n#"; # read entire statements using paragraph mode
  78. while ( defined($_ = shift @pending) or defined($_ = <>) ) {
  79. warn "[[$_]]\n" if $opt{d}; # show raw paragraph being read
  80. my @chunks = split /^\/.*Version.*started with[\000-\377]*?Time.*Id.*Command.*Argument.*\n/m;
  81. if (@chunks > 1) {
  82. unshift @pending, map { length($_) ? $_ : () } @chunks;
  83. warn "<<".join(">>\n<<",@chunks).">>" if $opt{d};
  84. next;
  85. }
  86. s/^#? Time: \d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+(Z|[+-]\d{2}:\d{2}).*\n//;
  87. my ($user,$host,$dummy,$thread_id) = s/^#? User\@Host:\s+(\S+)\s+\@\s+(\S+)\s+\S+(\s+Id:\s+(\d+))?.*\n// ? ($1,$2,$3,$4) : ('','','','','');
  88. s/^# Query_time: ([0-9.]+)\s+Lock_time: ([0-9.]+)\s+Rows_sent: ([0-9.]+).*\n//;
  89. my ($t, $l, $r) = ($1, $2, $3);
  90. $t -= $l unless $opt{l};
  91. # remove fluff that mysqld writes to log when it (re)starts:
  92. s!^/.*Version.*started with:.*\n!!mg;
  93. s!^Tcp port: \d+ Unix socket: \S+\n!!mg;
  94. s!^Time.*Id.*Command.*Argument.*\n!!mg;
  95. s/^use \w+;\n//; # not consistently added
  96. s/^SET timestamp=\d+;\n//;
  97. s/^[ ]*\n//mg; # delete blank lines
  98. s/^[ ]*/ /mg; # normalize leading whitespace
  99. s/\s*;\s*(#\s*)?$//; # remove trailing semicolon(+newline-hash)
  100. next if $opt{g} and !m/$opt{g}/io;
  101. unless ($opt{a}) {
  102. s/\b\d+\b/N/g;
  103. s/\b0x[0-9A-Fa-f]+\b/N/g;
  104. s/''/'S'/g;
  105. s/""/"S"/g;
  106. s/(\\')//g;
  107. s/(\\")//g;
  108. s/'[^']+'/'S'/g;
  109. s/"[^"]+"/"S"/g;
  110. # -n=8: turn log_20001231 into log_NNNNNNNN
  111. s/([a-z_]+)(\d{$opt{n},})/$1.('N' x length($2))/ieg if $opt{n};
  112. # abbreviate massive "in (...)" statements and similar
  113. s!(([NS],){100,})!sprintf("$2,{repeated %d times}",length($1)/2)!eg;
  114. }
  115. my $s = $stmt{$_} ||= { users=>{}, hosts=>{} };
  116. $s->{c} += 1;
  117. $s->{t} += $t;
  118. $s->{l} += $l;
  119. $s->{r} += $r;
  120. $s->{users}->{$user}++ if $user;
  121. $s->{hosts}->{$host}++ if $host;
  122. warn "{{$_}}\n\n" if $opt{d}; # show processed statement string
  123. }
  124. foreach (keys %stmt) {
  125. my $v = $stmt{$_} || die;
  126. my ($c, $t, $l, $r) = @{ $v }{qw(c t l r)};
  127. $v->{at} = $t / $c;
  128. $v->{al} = $l / $c;
  129. $v->{ar} = $r / $c;
  130. }
  131. my @sorted = sort { $stmt{$b}->{$opt{s}} <=> $stmt{$a}->{$opt{s}} } keys %stmt;
  132. @sorted = @sorted[0 .. $opt{t}-1] if $opt{t};
  133. @sorted = reverse @sorted if $opt{r};
  134. foreach (@sorted) {
  135. my $v = $stmt{$_} || die;
  136. my ($c, $t,$at, $l,$al, $r,$ar) = @{ $v }{qw(c t at l al r ar)};
  137. my @users = keys %{$v->{users}};
  138. my $user = (@users==1) ? $users[0] : sprintf "%dusers",scalar @users;
  139. my @hosts = keys %{$v->{hosts}};
  140. my $host = (@hosts==1) ? $hosts[0] : sprintf "%dhosts",scalar @hosts;
  141. printf "Count: %d Time=%.2fs (%ds) Lock=%.2fs (%ds) Rows=%.1f (%d), $user\@$host\n%s\n\n",
  142. $c, $at,$t, $al,$l, $ar,$r, $_;
  143. }
  144. sub usage {
  145. my $str= shift;
  146. my $text= <<HERE;
  147. Usage: mysqldumpslow [ OPTS... ] [ LOGS... ]
  148. Parse and summarize the MySQL slow query log. Options are
  149. --verbose verbose
  150. --debug debug
  151. --help write this text to standard output
  152. -v verbose
  153. -d debug
  154. -s ORDER what to sort by (al, at, ar, c, l, r, t), 'at' is default
  155. al: average lock time
  156. ar: average rows sent
  157. at: average query time
  158. c: count
  159. l: lock time
  160. r: rows sent
  161. t: query time
  162. -r reverse the sort order (largest last instead of first)
  163. -t NUM just show the top n queries
  164. -a don't abstract all numbers to N and strings to 'S'
  165. -n NUM abstract numbers with at least n digits within names
  166. -g PATTERN grep: only consider stmts that include this string
  167. -h HOSTNAME hostname of db server for *-slow.log filename (can be wildcard),
  168. default is '*', i.e. match all
  169. -i NAME name of server instance (if using mysql.server startup script)
  170. -l don't subtract lock time from total time
  171. HERE
  172. if ($str) {
  173. print STDERR "ERROR: $str\n\n";
  174. print STDERR $text;
  175. exit 1;
  176. } else {
  177. print $text;
  178. exit 0;
  179. }
  180. }