• 欢迎访问DBA的辛酸事儿,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站
  • 欢迎大家关注博主公众号:DBA的辛酸事儿
  • 博文中若有错误的地方,请大家指正,大家的指正是我前进的动力

【Percona-toolkit系列】Percona Toolkit工具之pt-duplicate-key-checker

Percona-toolkit SEian.G 5年前 (2019-05-19) 1591次浏览 已收录 0个评论

Percona-toolkit工具包系列文章

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

pt-duplicate-key-checker使用方法

首先我们创建一些测试表:

root@localhost [wjq]>create table testkey(
    -> id int ,
    -> c1 varchar(32),
    -> c2 varchar(32),
    -> c3 varchar(32),
    -> c4 varchar(32),
    -> primary key(id),
    -> index index_a(c2,c3,c4),
    -> index index_b(c2,c3),
    -> index index_c(c3,c4)
    -> );
Query OK, 0 rows affected (0.03 sec)

root@localhost [wjq]>create table parent(
    -> id int ,
    -> c1 varchar(32),
    -> c2 varchar(32),
    -> primary key(id),
    -> index index_a(c1)
    -> );
Query OK, 0 rows affected (0.04 sec)

root@localhost [wjq]>create table child(
    -> id int ,
    -> c1 varchar(32),
    -> c2 varchar(32),
    -> primary key(id),
    -> foreign key (c1) references parent(c1) on delete cascade on update cascade,
    -> foreign key (c1) references parent(c1) on delete cascade on update cascade
    -> );
Query OK, 0 rows affected (0.10 sec)

很显然,duplicate_key_check表中的index_b和index_c都是冗余索引,parent表和child表的外检约束也重复定义了。好了我们使用 pt-duplicate-key-checker来验证一把。

[root@VM_54_118_centos ~]# pt-duplicate-key-checker --user=root --sock=/tmp/mysql3306.sock --host=localhost --password=xxxxxxx --databases=wjq
# ########################################################################
# wjq.child                                                               
# ########################################################################

# FOREIGN KEY child_ibfk_2 (`c1`) REFERENCES `wjq`.`parent` (`c1`) is a duplicate of FOREIGN KEY child_ibfk_1 (`c1`) REFERENCES `wjq`.`parent` (`c1`)
# Key definitions:
#   CONSTRAINT `child_ibfk_2` FOREIGN KEY (`c1`) REFERENCES `parent` (`c1`)
#   CONSTRAINT `child_ibfk_1` FOREIGN KEY (`c1`) REFERENCES `parent` (`c1`)
# Column types:
#         `c1` varchar(32) default null
# To remove this duplicate foreign key, execute:
ALTER TABLE `wjq`.`child` DROP FOREIGN KEY `child_ibfk_2`;

# MySQL uses the c1 index for this foreign key constraint

# ########################################################################
# wjq.testkey                                                             
# ########################################################################

# index_b is a left-prefix of index_a
# Key definitions:
#   KEY `index_b` (`c2`,`c3`),
#   KEY `index_a` (`c2`,`c3`,`c4`),
# Column types:
#         `c2` varchar(32) default null
#         `c3` varchar(32) default null
#         `c4` varchar(32) default null
# To remove this duplicate index, execute:
ALTER TABLE `wjq`.`testkey` DROP INDEX `index_b`;

# ########################################################################
# Summary of indexes                                                      
# ########################################################################

# Size Duplicate Indexes   262
# Total Duplicate Indexes  2
# Total Indexes            29

也可以配置一个配置文件,加载配置文件获得结果

[root@VM_54_118_centos ~]# cat pt-duplicate-key.conf 
host=localhost
user=root
password=xxxxxxx
port=3306
sock=/tmp/mysql3306.sock
databases=wjq
clustered
charset=utf8mb4
all-structs
[root@VM_54_118_centos ~]# pt-duplicate-key-checker --config pt-duplicate-key.conf 
# ########################################################################
# wjq.child                                                               
# ########################################################################

# FOREIGN KEY child_ibfk_2 (`c1`) REFERENCES `wjq`.`parent` (`c1`) is a duplicate of FOREIGN KEY child_ibfk_1 (`c1`) REFERENCES `wjq`.`parent` (`c1`)
# Key definitions:
#   CONSTRAINT `child_ibfk_2` FOREIGN KEY (`c1`) REFERENCES `parent` (`c1`)
#   CONSTRAINT `child_ibfk_1` FOREIGN KEY (`c1`) REFERENCES `parent` (`c1`)
# Column types:
#         `c1` varchar(32) default null
# To remove this duplicate foreign key, execute:
ALTER TABLE `wjq`.`child` DROP FOREIGN KEY `child_ibfk_2`;

# MySQL uses the c1 index for this foreign key constraint

# ########################################################################
# wjq.testkey                                                             
# ########################################################################

# index_b is a left-prefix of index_a
# Key definitions:
#   KEY `index_b` (`c2`,`c3`),
#   KEY `index_a` (`c2`,`c3`,`c4`),
# Column types:
#         `c2` varchar(32) default null
#         `c3` varchar(32) default null
#         `c4` varchar(32) default null
# To remove this duplicate index, execute:
ALTER TABLE `wjq`.`testkey` DROP INDEX `index_b`;

# ########################################################################
# Summary of indexes                                                      
# ########################################################################

# Size Duplicate Indexes   262
# Total Duplicate Indexes  2
# Total Indexes            29

打印出非常详尽的信息,还给出如何修复冗余索引的SQL语句。是不是超级赞!

关于pt-duplicate-key-checker的更多使用方法详细可参考:

https://www.percona.com/doc/percona-toolkit/3.0/pt-duplicate-key-checker.html

【Percona-toolkit系列】Percona Toolkit工具之pt-duplicate-key-checker


如果您觉得本站对你有帮助,那么可以收藏和推荐本站,帮助本站更好地发展,在此谢过各位网友的支持。
转载请注明原文链接:【Percona-toolkit系列】Percona Toolkit工具之pt-duplicate-key-checker
喜欢 (3)
SEian.G
关于作者:
用心去记录工作,用心去感受生活,用心去学着成长;座右铭:苦练七十二变,笑对八十一难
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址