#!/usr/bin/perl eval 'exec /usr/bin/perl -S $0 ${1+"$@"}' if 0; # not running under some shell $VERSION = "0.6.6"; $RELEASE = "20070304"; # # $Id: mtop.PL,v 1.61 2004/09/12 22:22:03 mdprewitt Exp $ # # mtop - Shows the MySQL commands consuming the greatest time # Copyright (C) 2002 Marc Prewitt/Chelsea Networks # # This program is free software; you can redistribute it and/or modify it # under the terms of the GNU General Public License as published by the # Free Software Foundation; either version 2 of the License, or (at your # option) any later version. # # This program is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License # for more details. # # You should have received a copy of the GNU General Public License along # with this program; if not, write to the Free Software Foundation, Inc., # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # # =head1 NAME B - Shows top mysql threads =head1 SYNOPSIS mtop [--host={mysql_host}] [--dbuser={mysql_user}] [--password={mysqluser_pw}] [--seconds={refresh}] [--[no]idle] [--filter-user={regex}] [--filter-host={regex}] [--filter-db={regex}] [--filter-command={regex}] [--filter-state={regex}] [--filter-info={{regex}}] [--fold-select-columns] [--user={user}] [--manualrefresh] [--slow={seconds}] [--vs|veryslow={seconds}] [--vvs|veryveryslow={seconds}] mtop --help mtop --version =head1 DESCRIPTION Shows the MySQL commands consuming the greatest time. By default, only non-sleeping threads are shown, the B<--idle> option shows idle threads. While running several keys will affect the operation of B. Hitting B or B will show the available options. Normally, run as a console program this will allow you to see errant or badly optimized queries as they will stay on the screen for a while. However, if you are hunting for short lived queries, running in the B mode with a short refresh time will allow you to catch short lived queries as well. The following keys are active while B is running: q - quit ? - help Filtering/display s - change the number of seconds to delay between updates m - toggle manual refresh mode on/off d - filter display with regular expression (user/host/db/command/state/info) F - fold/unfold column names in select statement display h - display process for only one host u - display process for only one user i - toggle all/non-Sleeping process display o - reverse the sort order Control/Detail k - kill processes; send a kill to a list of ids e - explain a process; show query optimizer info z - zoom in on a process; show sql statement detail f - flush stats (reset show status variables) t - show mysqld stats (show status/mysqladmin ext) T - show short/important status v - show mysqld variables (show variables/mysqladmin vars) r - show replication status for master/slaves B
The main query screen shows the following information as well as the currently active queries (explanations are from the MySQL online manual and references refer to the section in the manual where the explanation came from): =over 4 =item n Threads: running, cached The B Threads represents how many threads the mysqld has allocated. One thread is allocated for each user connection. Additional threads are allocated for replication. =item Queries/slow: Total queries / Total SLOW QUERIES The first number is the total number of queries sent to the server since the last 'flush status' or since server start. The second number is the number of queries that have taken more than long_query_time. See section 4.9.5 The Slow Query Log. =item Cache Hit: Cache hit ratio This is the percentage of times a key read is handled from the key buffer cache. See section 4.5.7.4 B of the MySQL manual for more information. =item Opened tables: tables opened MySQL has a cache for open tables. If 'opened tables' is high, your cache may be too small. Look at the MySQL manual section: B<5.4.7 How MySQL Opens and Closes Tables> for further information. =item RRN: Handler_read_rnd_next Number of requests to read the next row in the datafile. This will be high if you are doing a lot of table scans. Generally this suggests that your tables are not properly indexed or that your queries are not written to take advantage of the indexes you have. (4.5.7.3) =item TLW: Table_locks_waited Number of times a table lock could not be acquired immediately and a wait was needed. If this is high, and you have performance problems, you should first optimise your queries, and then either split your table(s) or use replication. Available after 3.23.33. (4.5.7.3) =item SFJ: Select_full_join Number of joins without keys (If this is not 0, you should carefully check the indexes of your tables). (4.5.7.3) =item SMP: Sort_merge_passes Number of merges passes the sort algoritm have had to do. If this value is large you should consider increasing sort_buffer. (4.5.7.3) =item QPS: Questions per second The total number of sql commands handled by the MySQL server since startup or the last B command. =back B When viewing the I screen (B), the screen will refresh until a key is pressed at which point you will return to the main screen. The bottom of the I screen is denoted with a line containing B<--->. If you do not see that line, resize your screen until you do. The statistics screen has the following format: Stat: total [avg per sec / instant per sec ] For example: Questions: 720,672 [30/12] The I status screen is a list of recommendations from the MySQL manual. The first number is the total since startup or the last 'flush status'. The second number is the number per second since startup or flush. The last is the number per second since the last screen refresh. The I screen only shows the information once and returns to the main screen as the variables do not change after server startup. B The replication monitor screen looks for a master or slave server running on the currently monitored mysqld. If a master server is found, it then tries to connect to each slave connected to the master. Replication is shown for all masters and slaves found. Offsets from the master for each of the slaves is shown. Note: the offset may be less than zero because the slave position is checked after the master position. The offset shown is the number of queries in the binlog that the slave has to process before being caught up with the master. =head1 OPTIONS All options can be abbreviated by their shortest unique abbreviation. =over 4 =item -?, --help Show the help screen and exit. =item -v, --version Show the version number and exit. =item -h {mysql_host}, --host={mysql_host} By default, the mysqld on localhost is monitored. Specify an alternate host with this option. =item -dbu {mysql_user}, --dbuser={mysql_user} By default, the user 'mysqltop' is used to connect to the database. Specify an alternate user with this option. =item -p {mysqluser_pw}, --password={mysqluser_pw} By default, there is no password associated with the mysqltop user, specify a password with this option. =item -se {refresh}, --seconds={refresh} The default screen refresh is 5 seconds. =item -sl {seconds}, --slow={seconds} The number of seconds before a slow query is highlighted. The default is the server's long_query configuration variable. =item -vs {seconds}, --veryslow={seconds} The number of seconds before a very slow query is highlighted. The default is the the --slow option * 2. =item -vvs {seconds}, --veryveryslow={seconds} The number of seconds before a very very slow query is highlighted. The default is the the --slow option * 4. =item -i, --[no]idle By default, processes in the B command state are not shown. This option turns on display of idle threads. =item -u {user}, --user={user} Show only threads owned by this user. =item -fu {regex_pattern}, --filter-user={regex_pattern} =item -fh {regex_pattern}, --filter-host={regex_pattern} =item -fd {regex_pattern}, --filter-db={regex_pattern} =item -fs {regex_pattern}, --filter-state={regex_pattern} =item -fc {regex_pattern}, --filter-command={regex_pattern} =item -fi {regex_pattern}, --filter-info={regex_pattern} Filter the display based on the B provided. The B is a perl regular expression. The regular expression match is done with case insensitivity. For example, to only show B