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. Git由来

    很多人都知道,Linus在1991年创建了开源的Linux,从此,Linux系统不断发展,已经成为最大的服务器系统软件了. Linus虽然创建了Linux,但Linux的壮大是靠全世界热心的志愿者参与 ...

  2. HDU 4871 Shortest-path tree

    先用dijkstra把最短路树建出来,然后就是树的质心分治了. 经过k个点的路径,要么全在子树上,要么经过根结点,因此可以分治. 如果分治的时候选点不好会变成O(n^2),比较极端的情况是比如树是一条 ...

  3. vue中的js动画与Velocity.js结合

    vue里面除了用css写动画,还可以用js写动画,vue的transition中,定义了几个动画钩子 第一个动画钩子:@before-enter <div id='app'> <tr ...

  4. 初始化mysql数据库时提示字符编码错误的解决办法

    有时候在安装完数据库并初始化的时候会出现如下错误: root@localhost mysql-5.5.19]# bash scripts/mysql_install_db --user=mysql - ...

  5. 2018.7.15 解决css中input输入框点击时去掉外边框方法

    .input_css{ background:no-repeat 0 0 scroll #EEEEEE; border:none; outline:medium; }

  6. mapping4java源码下载(alibab的开源项目)

    项目中需要频繁实现json-->pojo,Bean-->Bean的转换,使用了mapping4java这个开源的框架: 网上查资料,该框架是愤怒的苹果提供的,附上原文地址 我也造了个轮子: ...

  7. C#使用事件方式Winform窗体之间传值

    [摘自:http://www.cnblogs.com/codeToUp/p/5371062.html] 工程的源代码地址:https://github.com/yes-or-no/WinFormTra ...

  8. youku服务端

    文件结构 config import os IP_PORT = () BACKLOG = BASE_DIR = os.path.dirname(os.path.dirname(__file__)) B ...

  9. 实战 Lucene2.0

    Lucene 简介 Lucene 是一个基于 Java 的全文信息检索工具包,它不是一个完整的搜索应用程序,而是为你的应用程序提供索引和搜索功能.Lucene 目前是 Apache Jakarta 家 ...

  10. springMVC集成logback日志系统

    一.项目结构 项目介绍:maven搭建的web项目,实现Java日志记录功能.其中logback.xml为日志配置文件,spring-mvc-servlet.xml为spring controller ...