Preface
 
    I suppose that we have a requirement of checking out how many duplicated indexes on a certain table or even on several specific databases.what will you do then?
 
Introduce
 
    pt-duplicate-key-checker is the very tool which can help us to check out the replicated keys and foreign keys in our databases.
 
Procedure
 
Usage
 pt-duplicate-key-checker [OPTIONS] [DSN]
Common parameter
 --clustered //Treats the columns of pripary key to be redundent like secondary key does.(default "yes")
--key-types //Specify the type of index to check.(default "fk" means both of f&k,f=foreign keys,k=ordinary keys)
--database //Specify the database you want to check.
--tables //Specify the tables you want to check.
--ignore-databases //Specify the ignoring database.
--ignore-tables //Specify the ignoring tables.
--ignore-order //Treats the key(a,b) to be duplicated when it is compared with the key(b,a)
--sql //Generate dropping index sql statment for those duplicated indexes.
Example
 
Check the indexes on a "sbtest4".
 (zlm@192.168.1.101 )[sysbench]>show tables;
+--------------------+
| Tables_in_sysbench |
+--------------------+
| sbtest1 |
| sbtest2 |
| sbtest3 |
| sbtest4 |
+--------------------+
rows in set (0.00 sec) (zlm@192.168.1.101 )[sysbench]>show create table sbtest4\G
*************************** . row ***************************
Table: sbtest4
Create Table: CREATE TABLE `sbtest4` (
`id` int() NOT NULL AUTO_INCREMENT,
`k` int() NOT NULL DEFAULT '',
`c` char() NOT NULL DEFAULT '',
`pad` char() NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
KEY `k_4` (`k`)
) ENGINE=InnoDB AUTO_INCREMENT= DEFAULT CHARSET=utf8
row in set (0.00 sec) (zlm@192.168.1.101 )[sysbench]>show index from sbtest4;
+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| sbtest4 | | PRIMARY | | id | A | | NULL | NULL | | BTREE | | |
| sbtest4 | | k_4 | | k | A | | NULL | NULL | | BTREE | | |
+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
rows in set (0.00 sec) (zlm@192.168.1.101 )[sysbench]>
Add an index on column id and k in table "sbtest4".
 (zlm@192.168.1.101 )[sysbench]>alter table sbtest4 add index idx_id_k1(id,k);
Query OK, rows affected (0.11 sec)
Records: Duplicates: Warnings: (zlm@192.168.1.101 )[sysbench]>show index from sbtest4;
+---------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+---------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| sbtest4 | | PRIMARY | | id | A | | NULL | NULL | | BTREE | | |
| sbtest4 | | k_4 | | k | A | | NULL | NULL | | BTREE | | |
| sbtest4 | | idx_id_k1 | | id | A | | NULL | NULL | | BTREE | | |
| sbtest4 | | idx_id_k1 | | k | A | | NULL | NULL | | BTREE | | |
+---------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
Check whether there's a duplicated index in table "sbtest4".
 [root@zlm2 :: ~]
#pt-duplicate-key-checker -d=sysbench -t=sbtest4
# ########################################################################
# Summary of indexes
# ######################################################################## # Total Indexes //No duplicated indexes were found. [root@zlm2 :: ~]
#pt-duplicate-key-checker -dsysbench -tsbtest4
# ########################################################################
# Summary of indexes
# ######################################################################## # Total Indexes
Check the conclusion with option "--no-clustered".
 [root@zlm2 :: ~]
#pt-duplicate-key-checker -dsysbench -tsbtest4 -h192.168.1. -P3306 -uzlm -pzlmzlm --no-clustered
# ########################################################################
# sysbench.sbtest4
# ######################################################################## # k_4 is a left-prefix of idx_id_k2
# Key definitions:
# KEY `k_4` (`k`),
# KEY `idx_id_k2` (`k`,`id`) //The index "idx_id_k2" contains the column in index "k_4" and in the same order.Therefore,"k_4" is indicated redundant.
# Column types:
# `k` int() not null default ''
# `id` int() not null auto_increment
# To remove this duplicate index, execute:
ALTER TABLE `sysbench`.`sbtest4` DROP INDEX `k_4`; # ########################################################################
# Summary of indexes
# ######################################################################## # Size Duplicate Indexes
# Total Duplicate Indexes
# Total Indexes
Check the conclusion with option "--clustered".(Its default is "true" what means we can also omit it)
 [root@zlm2 :: ~]
#pt-duplicate-key-checker -dsysbench -tsbtest4 -h192.168.1. -P3306 -uzlm -pzlmzlm
# ########################################################################
# sysbench.sbtest4
# ######################################################################## # k_4 is a left-prefix of idx_id_k2
# Key definitions:
# KEY `k_4` (`k`),
# KEY `idx_id_k2` (`k`,`id`)
# Column types:
# `k` int() not null default ''
# `id` int() not null auto_increment
# To remove this duplicate index, execute:
ALTER TABLE `sysbench`.`sbtest4` DROP INDEX `k_4`; # Key idx_id_k2 ends with a prefix of the clustered index
# Key definitions:
# KEY `idx_id_k2` (`k`,`id`)
# PRIMARY KEY (`id`),
# Column types:
# `k` int() not null default ''
# `id` int() not null auto_increment
# To shorten this duplicate clustered index, execute:
ALTER TABLE `sysbench`.`sbtest4` DROP INDEX `idx_id_k2`, ADD INDEX `idx_id_k2` (`k`); //The tool suggests to change the union index to a single column key. # ########################################################################
# Summary of indexes
# ######################################################################## # Size Duplicate Indexes
# Total Duplicate Indexes
# Total Indexes
Add another index on column id and k in table "sbtest4".(reverse the order this time)
 (zlm@192.168.1.101 )[sysbench]>alter table sbtest4 add index idx_id_k2(k,id);
Query OK, rows affected (0.04 sec)
Records: Duplicates: Warnings: (zlm@192.168.1.101 )[sysbench]>show index from sbtest4;
+---------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+---------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| sbtest4 | | PRIMARY | | id | A | | NULL | NULL | | BTREE | | |
| sbtest4 | | k_4 | | k | A | | NULL | NULL | | BTREE | | |
| sbtest4 | | idx_id_k1 | | id | A | | NULL | NULL | | BTREE | | |
| sbtest4 | | idx_id_k1 | | k | A | | NULL | NULL | | BTREE | | |
| sbtest4 | | idx_id_k2 | | k | A | | NULL | NULL | | BTREE | | |
| sbtest4 | | idx_id_k2 | | id | A | | NULL | NULL | | BTREE | | |
+---------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
rows in set (0.00 sec)
Check the conclusion with default option again.
 [root@zlm2 :: ~]
#pt-duplicate-key-checker -dsysbench -tsbtest4 -h192.168.1. -P3306 -uzlm -pzlmzlm
# ########################################################################
# sysbench.sbtest4
# ######################################################################## # k_4 is a left-prefix of idx_id_k2
# Key definitions:
# KEY `k_4` (`k`),
# KEY `idx_id_k2` (`k`,`id`)
# Column types:
# `k` int() not null default ''
# `id` int() not null auto_increment
# To remove this duplicate index, execute:
ALTER TABLE `sysbench`.`sbtest4` DROP INDEX `k_4`; # Key idx_id_k2 ends with a prefix of the clustered index
# Key definitions:
# KEY `idx_id_k2` (`k`,`id`)
# PRIMARY KEY (`id`),
# Column types:
# `k` int() not null default ''
# `id` int() not null auto_increment
# To shorten this duplicate clustered index, execute:
ALTER TABLE `sysbench`.`sbtest4` DROP INDEX `idx_id_k2`, ADD INDEX `idx_id_k2` (`k`); # ########################################################################
# Summary of indexes
# ######################################################################## # Size Duplicate Indexes
# Total Duplicate Indexes
# Total Indexes
 Check the conclusion another time with option "--ignore-order".
 [root@zlm2 :: ~]
#pt-duplicate-key-checker --databases=sysbench --tables=sbtest4 --ignore-order -h192.168.1. -P3306 -uzlm -pzlmzlm
# ########################################################################
# sysbench.sbtest4
# ######################################################################## # idx_id_k2 is a duplicate of idx_id_k1
# Key definitions:
# KEY `idx_id_k2` (`k`,`id`)
# KEY `idx_id_k1` (`id`,`k`),
# Column types:
# `k` int() not null default ''
# `id` int() not null auto_increment
# To remove this duplicate index, execute:
ALTER TABLE `sysbench`.`sbtest4` DROP INDEX `idx_id_k2`; //Why no dropping "idx_id_k1"?Beacause "idx_id_k2" is also a duplicate of "k_4" as we can see above. # ########################################################################
# Summary of indexes
# ######################################################################## # Size Duplicate Indexes
# Total Duplicate Indexes
# Total Indexes
Summary
  • Notice the difference between long option and short option,do not mix them up.
  • pt-duplicate-key-checker is quit convenient to generate a summay report of rudundant indexes in specific databases.
  • We can execute it with script in a certain interval of time and collect the information in a flat logfile for future analysis.
  • It also provides us the pertinent SQL statement to adjust the indexes in target tables.
  • Further more,we'd better compare the result of the tool with the data in sys.schema_redundant_indexes(5.7 or above).
 

Percona-Tookit工具包之pt-duplicate-key-checker的更多相关文章

  1. ON DUPLICATE KEY UPDATE重复插入时更新

    mysql当插入重复时更新的方法: 第一种方法: 示例一:插入多条记录 假设有一个主键为 client_id 的 clients 表,可以使用下面的语句: INSERT INTO clients (c ...

  2. 【转】MySQL的Replace into 与Insert into on duplicate key update真正的不同之处

    原文链接:http://www.jb51.net/article/47090.htm   今天听同事介绍oracle到mysql的数据migration,他用了Insert into ..... on ...

  3. ON DUPLICATE KEY UPDATE

    如果在INSERT语句末尾指定了ON DUPLICATE KEY UPDATE,并且插入行后会导致在一个UNIQUE索引或PRIMARY KEY中出现重复值,则在出现重复值的行执行UPDATE: 如果 ...

  4. 深入mysql "on duplicate key update" 语法的分析

    如果在INSERT语句末尾指定了on duplicate key update,并且插入行后会导致在一个UNIQUE索引或PRIMARY KEY中出现重复值,则在出现重复值的行执行UPDATE:如果不 ...

  5. [BTS]The join order has been enforced because a local join hint is used.;Duplicate key was ignored.".

    在一个客户的BizTalk Server 2013 R2环境中会报如下的ERROR,查找相关资料后,先试试停掉所有Trace. Log Name:      ApplicationSource:    ...

  6. INSERT INTO .. ON DUPLICATE KEY更新多行记录

    现在问题来了,如果INSERT多行记录, ON DUPLICATE KEY UPDATE后面字段的值怎么指定?要知道一条INSERT语句中只能有一个ON DUPLICATE KEY UPDATE,到底 ...

  7. insert into hi_user_score set hello_id=74372073,a=10001 on duplicate key update hello_id=74372073, a=10001

    insert into hi_user_score set hello_id=74372073,a=10001 on duplicate key update hello_id=74372073, a ...

  8. MySql避免重复插入记录方法(ignore,Replace,ON DUPLICATE KEY UPDATE)

    ON DUPLICATE KEY UPDATE  博客 http://blog.csdn.net/jbboy/article/details/46828917

  9. mysql:on duplicate key update与replace into

    在往表里面插入数据的时候,经常需要:a.先判断数据是否存在于库里面:b.不存在则插入:c.存在则更新 一.replace into 前提:数据库里面必须有主键或唯一索引,不然replace into ...

  10. mysql 插入重复值 INSERT ... ON DUPLICATE KEY UPDATE

    向数据库插入记录时,有时会有这种需求,当符合某种条件的数据存在时,去修改它,不存在时,则新增,也就是saveOrUpdate操作.这种控制可以放在业务层,也可以放在数据库层,大多数数据库都支持这种需求 ...

随机推荐

  1. 数据库连接-ADO.NET

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/huo065000/article/details/25830291       非常早就知道了ADO ...

  2. 【LOJ6045】「雅礼集训 2017 Day8」价(网络流)

    点此看题面 大致题意: 有\(n\)种药,每种药有一个权值,且使用了若干种药材.让你选择若干种药,使得药的数量与所使用的药材并集大小相等,求最小权值总和. 网络流 \(hl666\):这种数据范围,一 ...

  3. numpy中生成随机矩阵并打印出矩阵的shape

    from numpy import * c=zeros((4,5)) print c.shape print numpy.random.random((2,3))

  4. Adobe Photoshop CS6下载安装

    下载链接 http://yunpan.cn/cACgP6Lv5ygit (提取码:f37a) 第一步关键是断开网络,拔掉网线或断开无线 点击登录后,如果没有进入下面的界面,检查是否断网成功 至于选择安 ...

  5. IE问题——列表项图像

    等我们实现列表时,经常会遇到一种情况:需要为列表的每一项的前面添加一个列表项图像. 我们在查阅W3C时会发现,在CSS中已经为我们提供了实现方法——“list-style-type”,我们来看看它的实 ...

  6. Docker 日常指令

    镜像 制作镜像 docker build –t 172.0.0.1/demo/xxx:20180906_002 .[不要忘记 点] 查看镜像 docker images 删除镜像 docker rmi ...

  7. 基于 win7下虚拟机的 GNSS-SDR安装过程

    最近在安装 GNSS-SDR软件时,遇到了很多问题,这里回顾了我的安装过程,罗列了所遇到的问题和解决办法.希望后来者不要再踩这些坑了! 首先,在官方文档中看到,GNSS-SDR目前并不支持直接在 Wi ...

  8. 描述linux目录结构以及目录结构命名规定

    FHS全称(Filesystem Hierarchy Standard),中文意思是目录层次标准,是linux的目录规范标准. 详情点击查看 FHS定义了两层规范: 第一层:“/”目录下的各个目录应该 ...

  9. 网络基础-交换机、路由器、OSI7层模型

    第1章 网络基础 1.1 网络的诞生 网络的诞生使命:通过各种互联网服务提升全球人类生活品质. 让人类的生活更便捷和丰富,从而促进全球人类社会的进步.并且丰富人类的精神世界和物质世界,让人类最便捷地获 ...

  10. XStream 工具类 [ XmlUtil ]

    pom.xml <dependency> <groupId>com.thoughtworks.xstream</groupId> <artifactId> ...