pt-duplicate-key-checker工具可以检测表中重复的索引,对于一些业务量很大的表,而且开发不规范的情况下有用。基本用法:
看一下我们的测试表:
  1. mysql> desc new_orders;
  2. +---------+-------------+------+-----+---------+-------+
  3. | Field | Type | Null | Key | Default | Extra |
  4. +---------+-------------+------+-----+---------+-------+
  5. | no_o_id | int() | NO | PRI | NULL | |
  6. | no_d_id | tinyint() | NO | PRI | NULL | |
  7. | no_w_id | smallint() | NO | PRI | NULL | |
  8. +---------+-------------+------+-----+---------+-------+
  9. rows in set (0.00 sec)
  10. mysql> create index idx1 on new_orders(no_o_id);
  11. Query OK, rows affected (0.92 sec)
  12. Records: Duplicates: Warnings:
  13. mysql> create index idx2 on new_orders(no_o_id);
  14. Query OK, rows affected, warning (0.93 sec)
  15. Records: Duplicates: Warnings:
  16. mysql> create index idx3 on new_orders(no_o_id);
  17. Query OK, rows affected, warning (0.87 sec)
  18. Records: Duplicates: Warnings:
  19. mysql> create index idx3 on new_orders(no_o_id,no_d_id);
  20. ERROR (): Duplicate key name 'idx3'
  21. mysql> create index idx4 on new_orders(no_o_id,no_d_id);
  22. Query OK, rows affected (1.07 sec)
  23. Records: Duplicates: Warnings:
  24. mysql> create index idx5 on new_orders(no_o_id,no_d_id,no_w_id);
  25. Query OK, rows affected (1.04 sec)
  26. Records: Duplicates: Warnings:
  27. mysql> create index idx6 on new_orders(no_o_id,no_d_id,no_w_id);
  28. Query OK, rows affected, warning (1.58 sec)
  29. Records: Duplicates: Warnings:
下面开始进行检测:
  1. [root@mxqmongodb2 bin]# ./pt-duplicate-key-checker --host=172.16.16.35 --port= --user=root --password= --database=test --tables=new_orders;
  2. # ########################################################################
  3. # test.new_orders
  4. # ########################################################################
  5.  
  6. # idx6 is a duplicate of idx5
  7. # Key definitions:
  8. # KEY `idx6` (`no_o_id`,`no_d_id`,`no_w_id`)
  9. # KEY `idx5` (`no_o_id`,`no_d_id`,`no_w_id`),
  10. # Column types:
  11. # `no_o_id` int() not null
  12. # `no_d_id` tinyint() not null
  13. # `no_w_id` smallint() not null
  14. # To remove this duplicate index, execute:
  15. ALTER TABLE `test`.`new_orders` DROP INDEX `idx6`;
  16.  
  17. # idx4 is a left-prefix of idx5
  18. # Key definitions:
  19. # KEY `idx4` (`no_o_id`,`no_d_id`),
  20. # KEY `idx5` (`no_o_id`,`no_d_id`,`no_w_id`),
  21. # Column types:
  22. # `no_o_id` int() not null
  23. # `no_d_id` tinyint() not null
  24. # `no_w_id` smallint() not null
  25. # To remove this duplicate index, execute:
  26. ALTER TABLE `test`.`new_orders` DROP INDEX `idx4`;
  27.  
  28. # idx1 is a left-prefix of idx5
  29. # Key definitions:
  30. # KEY `idx1` (`no_o_id`),
  31. # KEY `idx5` (`no_o_id`,`no_d_id`,`no_w_id`),
  32. # Column types:
  33. # `no_o_id` int() not null
  34. # `no_d_id` tinyint() not null
  35. # `no_w_id` smallint() not null
  36. # To remove this duplicate index, execute:
  37. ALTER TABLE `test`.`new_orders` DROP INDEX `idx1`;
  38.  
  39. # idx2 is a left-prefix of idx5
  40. # Key definitions:
  41. # KEY `idx2` (`no_o_id`),
  42. # KEY `idx5` (`no_o_id`,`no_d_id`,`no_w_id`),
  43. # Column types:
  44. # `no_o_id` int() not null
  45. # `no_d_id` tinyint() not null
  46. # `no_w_id` smallint() not null
  47. # To remove this duplicate index, execute:
  48. ALTER TABLE `test`.`new_orders` DROP INDEX `idx2`;
  49.  
  50. # idx3 is a left-prefix of idx5
  51. # Key definitions:
  52. # KEY `idx3` (`no_o_id`),
  53. # KEY `idx5` (`no_o_id`,`no_d_id`,`no_w_id`),
  54. # Column types:
  55. # `no_o_id` int() not null
  56. # `no_d_id` tinyint() not null
  57. # `no_w_id` smallint() not null
  58. # To remove this duplicate index, execute:
  59. ALTER TABLE `test`.`new_orders` DROP INDEX `idx3`;
  60.  
  61. # ########################################################################
  62. # Summary of indexes
  63. # ########################################################################
  64.  
  65. # Size Duplicate Indexes
  66. # Total Duplicate Indexes
  67. # Total Indexes
我们看到,除了主键以外,其他的索引按说都是不成功的,但是pt-duplicate-key-checker只检查到了五个重复索引,这个重复不是我们理解的完全一样,而是包含索引。`idx5` (`no_o_id`,`no_d_id`,`no_w_id`),包含了刚才创建的1-4的索引,而且和6的索引是一样的。而主键的排序和idx5是不一样的所以说两者不同,也是满足了最左匹配的原则。

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. es第三篇:Search APIs

    大多数search API都是可以操作多个索引的,除了explain API. 当执行一个search API时,可以指定routing参数,去搜索特定的主分片及其副本分片.routing参数值可以是 ...

  2. Java - 自定义异常(尚学堂第六章异常机制作业计算平均数)

    编写一个计算N个学生分数平均分的程序.程序应该提示用户输入N的值,如何必须输入所有N个学生分数.如果用户输入的分数是一个负数,则应该抛出一个异常并捕获,提示“分数必须是正数或者0”.并提示用户再次输入 ...

  3. 使用jsp完成商品列表的动态显示

    1数据库准备工作 1创建数据库 2 创建product表 代码如下: CREATE TABLE `product` ( `pid` ) NOT NULL, `pname` ) DEFAULT NULL ...

  4. Parcel Vs Webpack

    横空出世的Parcel近日成为了前端圈的又一大热点,在短短几周内就获得了13K的Star.作为前端构建工具新人的Parcel为什么能在短期内获得这么多赞同?他和老大哥Webpack比起来到底有什么优势 ...

  5. Truncated incorrect DOUBLE value: 'NO_REFUND'

    解决办法:Mysql中,如果一个字段是字符串,则一定要加单引号 问题原因: `item_refund_state` ) NOT NULL item_refund_state字段的类型是varchar但 ...

  6. log4j 详细讲解

    日志是应用软件中不可缺少的部分,Apache的开源项目log4j是一个功能强大的日志组件,提供方便的日志记录.在apache网站:jakarta.apache.org/log4j 可以免费下载到Log ...

  7. 在线课程笔记—.NET基础

    关于学习北京理工大学金旭亮老师在线课程的笔记. 介绍: 在线课程网址:http://mooc.study.163.com/university/BIT#/c 老师个人网站:http://jinxuli ...

  8. JSON 转 VO

    需求 将获取的json数据直接转为vo 解决 利用net.sf.json.JSONObject的toBean() 确保json中的key值和vo中的字段名称一致 JSONObject jsonObje ...

  9. System.arraycopy的测试

    ArrayList的源码中数组的拷贝用到该方法: public static void arraycopy(Object src, --源数组 int srcPos, --源数组要复制的起始位置 Ob ...

  10. 官网下载apache服务器并运行

    1.打开官网 https://httpd.apache.org/ 2.找到下载位置,比如我们要下载2.x版本 点击download,在下一页找到      Files for Microsoft Wi ...