使用pt-duplicate-key-checker检查MySQL重复索引

pt-duplicate-key-checker这款工具也是percona-toolkit中一款非常适用的工具,它可以帮助你检测表中重复的索引或者主键。我们知道索引会更查询带来好处,但是过量的索引反而可能会使数据库的性能降低,这款工具可以帮助我们找到重复的索引并且还会给你删除重复索引的建议语句,非常好用。

首先看我的这张表的索引结构

mysql> show index from test.dd; +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ | dd | 1 | idx_a_b | 1 | a | A | 0 | NULL | NULL | YES | BTREE | | | | dd | 1 | idx_a_b | 2 | b | A | 0 | NULL | NULL | YES | BTREE | | | | dd | 1 | idx_a | 1 | a | A | 0 | NULL | NULL | YES | BTREE | | | +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 3 rows in set (0.00 sec)

1

2

3

4

5

6

7

8

9

mysql> show index from test.dd;

+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |

+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

| dd    |          1 | idx_a_b  |            1 | a           | A         |           0 |     NULL | NULL   | YES  | BTREE      |         |               |

| dd    |          1 | idx_a_b  |            2 | b           | A         |           0 |     NULL | NULL   | YES  | BTREE      |         |               |

| dd    |          1 | idx_a    |            1 | a           | A         |           0 |     NULL | NULL   | YES  | BTREE      |         |               |

+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

3 rows in set (0.00 sec)

里面有重复索引,比如idx_a与idx_a_b重复。下面看下怎么使用pt-duplicate-key-checker来检查重复的索引。

$ pt-duplicate-key-checker --host=192.168.1.10 --port=3306 --user='root' --password='123456' --databases=test --tables=dd

1

$ pt-duplicate-key-checker --host=192.168.1.10 --port=3306 --user='root' --password='123456'  --databases=test --tables=dd

# ######################################################################## # test.dd # ######################################################################## # idx_a is a left-prefix of idx_a_b # Key definitions: # KEY `idx_a` (`a`) # KEY `idx_a_b` (`a`,`b`), # Column types: # `a` int(11) default null # `b` int(11) default null # To remove this duplicate index, execute: ALTER TABLE `test`.`dd` DROP INDEX `idx_a`; # ######################################################################## # Summary of indexes # ######################################################################## # Size Duplicate Indexes 5 # Total Duplicate Indexes 1 # Total Indexes 2

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

# ########################################################################

# test.dd                                                                

# ########################################################################

# idx_a is a left-prefix of idx_a_b

# Key definitions:

#   KEY `idx_a` (`a`)

#   KEY `idx_a_b` (`a`,`b`),

# Column types:

#         `a` int(11) default null

#         `b` int(11) default null

# To remove this duplicate index, execute:

ALTER TABLE `test`.`dd` DROP INDEX `idx_a`;

# ########################################################################

# Summary of indexes                                                      

# ########################################################################

# Size Duplicate Indexes   5

# Total Duplicate Indexes  1

# Total Indexes            2

从结果可以看出,这个工具给出了重复的索引以及相关的列类型。下面还记录了重复索引占用了多少大小,重复索引的个数以及总共的索引数目。

这里面有一处很有意思的地方是,为什么删除idx_a个重复索引,而没有删除idx_a_b这个组合索引呢?因为现在的索引设计尽可能的设计成组合索引,而不要单独的列进行索引,所以pt-duplicate-key-checker在判断的时候尽可能的让组合索引保持下来。

pt-duplicate-key-checker中还有很多其它的选项与参数,可以通过pt-duplicate-key-checker –help查看

猜你喜欢

转载自blog.csdn.net/demonson/article/details/87269883