MySQL 冗余索引排查及优化
这里使用到 pt 工具 pt-duplicate-key-checker 。
pt 工具下载:https://www.percona.com/downloads/percona-toolkit/LATEST/
压缩包下载完成后,上传到服务器,解压即可,这里安装过程跳过,直接进入工具使用及冗余索引排查环节。
1.查看工具帮助说明:
[mysql@dbtestsitdb01 bin]$ ./pt-duplicate-key-checker --help
pt-duplicate-key-checker examines MySQL tables for duplicate or redundant
indexes and foreign keys. Connection options are read from MySQL option files.
For more details, please use the --help option, or try 'perldoc
./pt-duplicate-key-checker' for complete documentation.
Usage: pt-duplicate-key-checker [OPTIONS] [DSN]
Options:
--all-structs Compare indexes with different structs (BTREE, HASH,
etc)
--ask-pass Prompt for a password when connecting to MySQL
--charset=s -A Default character set
--[no]clustered PK columns appended to secondary key is duplicate (
default yes)
--config=A Read this comma-separated list of config files; if
specified, this must be the first option on the command
line
--databases=h -d Check only this comma-separated list of databases
--defaults-file=s -F Only read mysql options from the given file
--engines=h -e Check only tables whose storage engine is in this comma-
separated list
--help Show help and exit
--host=s -h Connect to host
--ignore-databases=H Ignore this comma-separated list of databases
--ignore-engines=H Ignore this comma-separated list of storage engines
--ignore-order Ignore index order so KEY(a,b) duplicates KEY(b,a)
--ignore-tables=H Ignore this comma-separated list of tables
--key-types=s Check for duplicate f=foreign keys, k=keys or fk=both (
default fk)
--password=s -p Password to use when connecting
--pid=s Create the given PID file
--port=i -P Port number to use for connection
--set-vars=A Set the MySQL variables in this comma-separated list of
variable=value pairs
--socket=s -S Socket file to use for connection
--[no]sql Print DROP KEY statement for each duplicate key (
default yes)
--[no]summary Print summary of indexes at end of output (default yes)
--tables=h -t Check only this comma-separated list of tables
--user=s -u User for login if not current user
--verbose -v Output all keys and/or foreign keys found, not just
redundant ones
--version Show version and exit
--[no]version-check Check for the latest version of Percona Toolkit, MySQL,
and other programs (default yes)
Option types: s=string, i=integer, f=float, h/H/a/A=comma-separated list, d=DSN, z=size, m=time
Rules:
This tool accepts additional command-line arguments. Refer to the SYNOPSIS and usage information for details.
DSN syntax is key=value[,key=value...] Allowable DSN keys:
KEY COPY MEANING
=== ==== =============================================
A yes Default character set
D yes Default database
F yes Only read default options from the given file
P yes Port number to use for connection
S yes Socket file to use for connection
h yes Connect to host
p yes Password to use when connecting
u yes User for login if not current user
If the DSN is a bareword, the word is treated as the 'h' key.
Options and values after processing arguments:
--all-structs FALSE
--ask-pass FALSE
--charset (No value)
--clustered TRUE
--config /etc/percona-toolkit/percona-toolkit.conf,/etc/percona-toolkit/pt-duplicate-key-checker.conf,/home/mysql/.percona-toolkit.conf,/home/mysql/.pt-duplicate-key-checker.conf
--databases (No value)
--defaults-file (No value)
--engines (No value)
--help TRUE
--host (No value)
--ignore-databases
--ignore-engines
--ignore-order FALSE
--ignore-tables
--key-types fk
--password (No value)
--pid (No value)
--port (No value)
--set-vars
--socket (No value)
--sql TRUE
--summary TRUE
--tables (No value)
--user (No value)
--verbose FALSE
--version FALSE
--version-check TRUE
2.查看冗余索引
[mysql@dbtestsitdb01 bin]$ ./pt-duplicate-key-checker -uroot -p‘123456’ -h127.1 --socket=/mysql/data/mysql.sock -d dbtest01
# ########################################################################
# dbtest01.t3
# ########################################################################
# idx_xx is a left-prefix of idx_uniq
# Key definitions:
# KEY `idx_xx` (`xxx`) USING BTREE,
# UNIQUE KEY `idx_uniq` (`xxx`,`yyy`,`zzz`),
# Column types:
# `xxx` varchar(20) not null
# `yyy` varchar(20) not null
# `zzz` varchar(20) not null
# To remove this duplicate index, execute:
ALTER TABLE `dbtest01`.`t3` DROP INDEX `idx_xx`;
# ########################################################################
# dbtest01.ttt
# ########################################################################
# name is a left-prefix of xxx
# Key definitions:
# KEY `name` (`name`),
# KEY `xxx` (`name`,`age`)
# Column types:
# `name` varchar(20) default null
# `age` varchar(10) default null
# To remove this duplicate index, execute:
ALTER TABLE `dbtest01`.`ttt` DROP INDEX `name`;
# ########################################################################
# Summary of indexes
# ########################################################################
# Size Duplicate Indexes 741
# Total Duplicate Indexes 2
# Total Indexes 79
由上述检查信息中,我们可以看到,表 t3 、ttt 中存在冗余索引,并且给出对应可以删除重复索引的SQL命令。
结尾的统计信息:
- Size Duplicate Indexes,检查的索引占用空间大小
- Total Duplicate Indexes,检查的冗余索引数量
- Total Indexes,检查的总的索引数量