mk-index-usage(1) Read queries from a log and analyze how they use indexes.


mk-index-usage /path/to/slow.log --host localhost


The following section is included to inform users about the potential risks, whether known or unknown, of using this tool. The two main categories of risks are those created by the nature of the tool (e.g. read-only tools vs. read-write tools) and those created by bugs.

This tool is read-only. It tries to read a log of queries and EXPLAIN them. It also gathers information about all tables in all databases. It should be very low-risk.

At the time of this release, we know of no bugs that could cause serious harm to users.

The authoritative source for updated information is always the online issue tracking system. Issues that affect this tool will be marked as such. You can see a list of such issues at the following URL: <>.

See also ``BUGS'' for more information on filing bugs and getting help.


This tool connects to a MySQL database server, reads through a query log, and asks MySQL how it will use each query. When it is finished, it prints out a report on indexes that the queries didn't use.

The query log needs to be in MySQL's slow query log format. If you need to input a different format, you can use mk-query-digest to translate the formats. If you don't specify a filename, the tool reads from STDIN.

The tool runs two stages. In the first stage, the tool takes inventory of all the tables and indexes in your database, so it can compare the existing indexes to those that were actually used by the queries in the log. In the second stage, it runs EXPLAIN on each query in the query log. If the query is not a SELECT, it tries to transform it to a roughly equivalent SELECT query. This is not a perfect process, but it is good enough to be useful.

The tool skips the EXPLAIN step for queries that are exact duplicates of those seen before. It assumes that the same query will generate the same EXPLAIN plan as it did previously (usually a safe assumption, and generally good for performance), and simply increments the count of times that the indexes were used. However, queries that have the same fingerprint but different checksums will be re-EXPLAINed. Queries that have different literal constants can have different execution plans, and this is important to measure.

After EXPLAIN-ing the query, it is necessary to try to map aliases in the query back to the original table names. For example, consider the EXPLAIN plan for the following query:

  SELECT * FROM tbl1 AS foo;

The EXPLAIN output will show access to table "foo", and that must be translated back to "tbl1". This process involves complex parsing. It is generally very accurate, but there is some chance that it might not work right. If you find cases where it fails, submit a bug report and a reproducible test case.

Queries that cannot be EXPLAIN'ed will cause all subsequent queries with the same fingerprint to be blacklisted. This is to reduce the work they cause, and prevent them from continuing to print error messages. However, at least in this stage of the tool's development, it is my opinion that it's not a good idea to pre-emtively silence these, or prevent them from being EXPLAIN'ed at all. I am looking for lots of feedback on how to improve things like the query parsing. So please submit your test cases based on the errors the tool prints!


After it reads all the events in the log, the tool prints out DROP statements for every index that was not used. It skips indexes for tables that were never accessed by any queries in the log, to avoid false-positive results.

If you don't specify ``--quiet'', the tool also outputs warnings about statements that cannot be EXPLAIN'ed and similar. These go to standard error.

Progress reports are enabled by default (see ``--progress''). These also go to standard error.


Prompt for a password when connecting to MySQL.
short form: -A; type: string

Default character set. If the value is utf8, sets Perl's binmode on STDOUT to utf8, passes the mysql_enable_utf8 option to DBD::mysql, and runs SET NAMES UTF8 after connecting to MySQL. Any other value sets binmode on STDOUT without the utf8 layer, and runs SET NAMES after connecting to MySQL.

type: Array

Read this comma-separated list of config files; if specified, this must be the first option on the command line.

short form: -F; type: string

Only read mysql options from the given file. You must give an absolute pathname.

type: Hash; default: non-unique

Suggest dropping only these types of unusued indexes.

By default mk-index-usage will only suggest to drop unused secondary indexes, not primary or unique indexes. You can specify which types of unused indexes the tool suggests to drop: primary, unique, non-unique, all.

A separate "ALTER TABLE" statement for each type is printed. So if you specify "--drop all" and there is a primary key and a non-unique index, the "ALTER TABLE ... DROP" for each will be printed on separate lines.

Show help and exit.
short form: -h; type: string

Connect to host.

short form: -p; type: string

Password to use when connecting.

short form: -P; type: int

Port number to use for connection.

type: array; default: time,30

Print progress reports to STDERR. The value is a comma-separated list with two parts. The first part can be percentage, time, or iterations; the second part specifies how often an update should be printed, in percentage, seconds, or number of iterations.

short form: -q

Do not print any warnings. Also disables ``--progress''.

type: string; default: wait_timeout=10000

Set these MySQL variables. Immediately after connecting to MySQL, this string will be appended to SET and executed.

short form: -S; type: string

Socket file to use for connection.

short form: -u; type: string

User for login if not current user.

Show version and exit.


These DSN options are used to create a DSN. Each option is given like "option=value". The options are case-sensitive, so P and p are not the same option. There cannot be whitespace before or after the "=" and if the value contains whitespace it must be quoted. DSN options are comma-separated. See the maatkit manpage for full details.
  • A

    dsn: charset; copy: yes

    Default character set.

  • D

    dsn: database; copy: yes

    Database to connect to.

  • F

    dsn: mysql_read_default_file; copy: yes

    Only read default options from the given file

  • h

    dsn: host; copy: yes

    Connect to host.

  • p

    dsn: password; copy: yes

    Password to use when connecting.

  • P

    dsn: port; copy: yes

    Port number to use for connection.

  • S

    dsn: mysql_socket; copy: yes

    Socket file to use for connection.

  • u

    dsn: user; copy: yes

    User for login if not current user.


You can download Maatkit from Google Code at <>, or you can get any of the tools easily with a command like the following:


Where "toolname" can be replaced with the name (or fragment of a name) of any of the Maatkit tools. Once downloaded, they're ready to run; no installation is needed. The first URL gets the latest released version of the tool, and the second gets the latest trunk code from Subversion.


The environment variable "MKDEBUG" enables verbose debugging output in all of the Maatkit tools:

   MKDEBUG=1 mk-....


You need Perl and some core packages that ought to be installed in any reasonably new version of Perl.


For list of known bugs see <>.

Please use Google Code Issues and Groups to report bugs or request support: <>. You can also join #maatkit on Freenode to discuss Maatkit.

Please include the complete command-line used to reproduce the problem you are seeing, the version of all MySQL servers involved, the complete output of the tool when run with ``--version'', and if possible, debugging output produced by running with the "MKDEBUG=1" environment variable.


This program is copyright 2010 Baron Schwartz. Feedback and improvements are welcome.


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, version 2; OR the Perl Artistic License. On UNIX and similar systems, you can issue `man perlgpl' or `man perlartistic' to read these licenses.

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.


Baron Schwartz, Daniel Nichter


This tool is part of Maatkit, a toolkit for power users of MySQL. Maatkit was created by Baron Schwartz; Baron and Daniel Nichter are the primary code contributors. Both are employed by Percona. Financial support for Maatkit development is primarily provided by Percona and its clients.


This manual page documents Ver 0.9.0 Distrib 6652 $Revision: 6644 $.