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. MySQL入门很简单: 15 java访问MySQL数据库

    1. 连接数据库 1.1 下载安装驱动 java通过JDBC(Java Database Connectivity,Java数据库连接)来访问MySQL数据库.JDBC的编程接口提供的接口和类与MyS ...

  2. Radix Sort

    为了完成二维数据快速分类,最先使用的是hash分类. 前几天我突然想,既然基数排序的时间复杂度也不高,而且可能比hash分类更稳定,所以不妨试一下. 在实现上我依次实现: 1.一维数组基数排序 基本解 ...

  3. ABI (应用程序二进制接口)

    http://baike.baidu.com/link?url=ybErtZo0zAB5_P-kKZmT_nd9FdxaAAPqW4jqtwN5Tmu_q8weayOOFOJBrXw1RLbR20sK ...

  4. Uva 11582 巨大的斐波那契数 模运算

    题目链接:https://vjudge.net/contest/156903#problem/A 题意:计算 f(a^b)%n 分析: 1.斐波那契数列是 f(i+2) = f(i+1) + f(i) ...

  5. vuejs中v-if的深层用法v-else,v-else-if,key

    <div id='root'> <div v-if='show'>helle world</div> <button @click='handleClick' ...

  6. Buffer实例

    互联网的基础是数据的传送,一切都围绕着数据展开,比如发送啊,接收啊,这一切都离不开网络,通过之前,学会了通过http模块来搭建一个服务器,也实现了网络爬虫,nodejs中网络的部分,Net这个模块,对 ...

  7. 获取页面的url

    设当前页完整地址是:http://www.jb51.net/aaa/bbb.aspx?id=5&name=kelli "http://"是协议名 "www.jb5 ...

  8. Notepad++插件Emmet和Python Script的安装

    最近在做一个项目,涉及到大量的HTML.CSS代码的编写,手动写代码效率实在 是低下.于是想搜索一下,有没有Notepad++插件可以支持自动生成的,果不其然还真有.Emmet,这款神器其实就是 Ze ...

  9. MySql客户端远程连接MySql服务器

    设置MySql服务器以接听端口及以绑定IP地址 MySql服务器默认监听3306端口,确定防火墙以开放此端口. 编辑/etc/my.cnf 添加绑定IP地址.bind-address=192.168. ...

  10. JAVAOOP继承

    继承:修饰符 子类 extends 父类{    //类定义部分},不可以使用private和protected修饰类 减少代码量,实现无损替换 必须符合A is a B的关系 宝马  车 狗     ...