home *** CD-ROM | disk | FTP | other *** search
/ Internet Magazine 2003 Autumn / INTERNET109.ISO / pc / software / windows / building / mysql / data1.cab / Development / scripts / mysqldumpslow < prev    next >
Encoding:
Text File  |  2003-08-03  |  4.9 KB  |  144 lines

  1. #!/usr/bin/perl
  2. # mysqldumpslow - parse and summarize the MySQL slow query log
  3.  
  4. # Original version by Tim Bunce, sometime in 2000.
  5. # Further changes by Tim Bunce, 8th March 2001.
  6. # Handling of strings with \ and double '' by Monty 11 Aug 2001.
  7.  
  8. use strict;
  9. use Getopt::Long;
  10.  
  11. # t=time, l=lock time, r=rows
  12. # at, al, and ar are the corresponding averages
  13.  
  14. my %opt = (
  15.     s => 'at',
  16.     h => '*',
  17. );
  18.  
  19. GetOptions(\%opt,
  20.     'v+',    # verbose
  21.     'd+',    # debug
  22.     's=s',    # what to sort by (t, at, l, al, r, ar etc)
  23.     'r!',    # reverse the sort order (largest last instead of first)
  24.     't=i',    # just show the top n queries
  25.     'a!',    # don't abstract all numbers to N and strings to 'S'
  26.     'n=i',    # abstract numbers with at least n digits within names
  27.     'g=s',    # grep: only consider stmts that include this string
  28.     'h=s',    # hostname of db server for *-slow.log filename (can be wildcard)
  29.     'i=s',    # name of server instance (if using mysql.server startup script)
  30.     'l!',    # don't subtract lock time from total time
  31. ) or die "Bad option";
  32.  
  33.  
  34. unless (@ARGV) {
  35.     my $defaults   = `my_print_defaults mysqld`;
  36.     my $basedir = ($defaults =~ m/--basedir=(.*)/)[0]
  37.     or die "Can't determine basedir from 'my_print_defaults mysqld' output: $defaults";
  38.     warn "basedir=$basedir\n" if $opt{v};
  39.  
  40.     my $datadir = ($defaults =~ m/--datadir=(.*)/)[0];
  41.     if (!$datadir or $opt{i}) {
  42.     # determine the datadir from the instances section of /etc/my.cnf, if any
  43.     my $instances  = `my_print_defaults instances`;
  44.     die "Can't determine datadir from 'my_print_defaults mysqld' output: $defaults"
  45.         unless $instances;
  46.     my @instances = ($instances =~ m/^--(\w+)-/mg);
  47.     die "No -i 'instance_name' specified to select among known instances: @instances.\n"
  48.         unless $opt{i};
  49.     die "Instance '$opt{i}' is unknown (known instances: @instances)\n"
  50.         unless grep { $_ eq $opt{i} } @instances;
  51.     $datadir = ($instances =~ m/--$opt{i}-datadir=(.*)/)[0]
  52.         or die "Can't determine --$opt{i}-datadir from 'my_print_defaults instances' output: $instances";
  53.     warn "datadir=$datadir\n" if $opt{v};
  54.     }
  55.  
  56.     @ARGV = <$datadir/$opt{h}-slow.log>;
  57.     die "Can't find '$datadir/$opt{h}-slow.log'\n" unless @ARGV;
  58. }
  59.  
  60. warn "\nReading mysql slow query log from @ARGV\n";
  61.  
  62. my @pending;
  63. my %stmt;
  64. $/ = ";\n#";        # read entire statements using paragraph mode
  65. while ( defined($_ = shift @pending) or defined($_ = <>) ) {
  66.     warn "[[$_]]\n" if $opt{d};    # show raw paragraph being read
  67.  
  68.     my @chunks = split /^\/.*Version.*started with[\000-\377]*?Time.*Id.*Command.*Argument.*\n/m;
  69.     if (@chunks > 1) {
  70.     unshift @pending, map { length($_) ? $_ : () } @chunks;
  71.     warn "<<".join(">>\n<<",@chunks).">>" if $opt{d};
  72.     next;
  73.     }
  74.  
  75.     s/^#? Time: \d{6}\s+\d+:\d+:\d+.*\n//;
  76.     my ($user,$host) = s/^#? User\@Host:\s+(\S+)\s+\@\s+(\S+).*\n// ? ($1,$2) : ('','');
  77.  
  78.     s/^# Query_time: (\d+)  Lock_time: (\d+)  Rows_sent: (\d+).*\n//;
  79.     my ($t, $l, $r) = ($1, $2, $3);
  80.     $t -= $l unless $opt{l};
  81.  
  82.     # remove fluff that mysqld writes to log when it (re)starts:
  83.     s!^/.*Version.*started with:.*\n!!mg;
  84.     s!^Tcp port: \d+  Unix socket: \S+\n!!mg;
  85.     s!^Time.*Id.*Command.*Argument.*\n!!mg;
  86.  
  87.     s/^use \w+;\n//;    # not consistently added
  88.     s/^SET timestamp=\d+;\n//;
  89.  
  90.     s/^[     ]*\n//mg;    # delete blank lines
  91.     s/^[     ]*/  /mg;    # normalize leading whitespace
  92.     s/\s*;\s*(#\s*)?$//;    # remove trailing semicolon(+newline-hash)
  93.  
  94.     next if $opt{g} and !m/$opt{g}/io;
  95.  
  96.     unless ($opt{a}) {
  97.     s/\b\d+\b/N/g;
  98.     s/\b0x[0-9A-Fa-f]+\b/N/g;
  99.         s/''/'S'/g;
  100.         s/""/"S"/g;
  101.         s/(\\')//g;
  102.         s/(\\")//g;
  103.         s/'[^']+'/'S'/g;
  104.         s/"[^"]+"/"S"/g;
  105.     # -n=8: turn log_20001231 into log_NNNNNNNN
  106.     s/([a-z_]+)(\d{$opt{n},})/$1.('N' x length($2))/ieg if $opt{n};
  107.     # abbreviate massive "in (...)" statements and similar
  108.     s!(([NS],){100,})!sprintf("$2,{repeated %d times}",length($1)/2)!eg;
  109.     }
  110.  
  111.     my $s = $stmt{$_} ||= { users=>{}, hosts=>{} };
  112.     $s->{c} += 1;
  113.     $s->{t} += $t;
  114.     $s->{l} += $l;
  115.     $s->{r} += $r;
  116.     $s->{users}->{$user}++ if $user;
  117.     $s->{hosts}->{$host}++ if $host;
  118.  
  119.     warn "{{$_}}\n\n" if $opt{d};    # show processed statement string
  120. }
  121.  
  122. foreach (keys %stmt) {
  123.     my $v = $stmt{$_} || die;
  124.     my ($c, $t, $l, $r) = @{ $v }{qw(c t l r)};
  125.     $v->{at} = $t / $c;
  126.     $v->{al} = $l / $c;
  127.     $v->{ar} = $r / $c;
  128. }
  129.  
  130. my @sorted = sort { $stmt{$b}->{$opt{s}} <=> $stmt{$a}->{$opt{s}} } keys %stmt;
  131. @sorted = @sorted[0 .. $opt{t}-1] if $opt{t};
  132. @sorted = reverse @sorted         if $opt{r};
  133.  
  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.