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. *1 Two Sum two pointers(hashmap one scan)

    Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...

  2. CRUD全栈式编程架构之控制器的设计

    页面 这里界面我采用jquery miniui来做的,当你完全了解了整个设计之后可以轻松切换到其他的js框架,个人认为类似muniui,easyui等等这类可以将web界面做得和winform类似的框 ...

  3. JavaRebel 2.0 发布,一个JVM插件

    JavaRebel是一个JVM插件(-javaagent),能够即时重载java class更改,因此不需要重新部署一个应用或者重启容器,节约开发者时间. JavaRebel 2.0的新特征: 改变了 ...

  4. C# do while语句

    一.C# do while语句 do while语句是先执行一次循环体,然后再判断是否需要重复执行循环体的循环控制语句. 语法格式如下: do{    embedded-statement}while ...

  5. jQuery性能优化和技巧

    jQuery性能优化 ①使用最新版本的jQuery类库 ②使用合适的选择器 ③缓存对象 ④循环时的DOM操作 ⑤数组方式使用jQuery对象 ⑥事件代理 ⑦将你的代码转化成jQuery插件 ⑧使用jo ...

  6. 什么是 BIND 变量?

    变量绑定会使联机事务处理过程(OLTP)系统数据库中的SQL执行速度飞快,内存效率极高:不使用绑定变量可能会使OLTP数据库不堪重负,资源被SQL解析严重耗尽,系统运行缓慢. 当一个用户与数据库建立连 ...

  7. MySQL存储引擎MyISAM与InnoDB

    一. MySQL存储引擎MyISAM与InnoDB如何选择 MySQL有多种存储引擎,每种存储引擎有各自的优缺点,可以择优选择使用:MyISAM.InnoDB.MERGE.MEMORY(HEAP).B ...

  8. Python 统计不同url svn代码变更数

    #!/bin/bash/python # -*-coding:utf-8-*- #svn统计不同url代码行数变更脚本,过滤空行,不过滤注释. import subprocess,os,sys,tim ...

  9. Q&A - Nginx与Tomcat的区别?

    web上的server都叫web server,但是大家分工也有不同的. nginx常用做静态内容服务和代理服务器(不是你FQ那个代理),直面外来请求转发给后面的应用服务(tomcat,django什 ...

  10. python 获取项目的根路径

    root_path = os.path.abspath(os.path.dirname(__file__)).split('shippingSchedule')[0] shippingSchedule ...