pt-duplicate-key-checker檢查資料庫的重複索引
pt-duplicate-key-checker這款工具也是percona-toolkit中一款非常適用的工具,它可以幫助你檢測表中重複的索引或者主鍵。我們知道索引會更查詢帶來好處,但是過量的索引反而可能會使資料庫的效能降低,這款工具可以幫助我們找到重複的索引並且還會給你刪除重複索引的建議語句,非常好用。
例子:
mysql> create table curs (id int ,name varchar(30),age char(2));
Query OK, 0 rows affected (0.03 sec)
mysql> create index name on curs(name);
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> create index age on curs(age);
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> create index name_age on curs(name,age);
Query OK, 0 rows affected (0.35 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> create index age_name on curs(age,name);
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> create index age_1 on curs(age);
Query OK, 0 rows affected, 1 warning (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 1
mysql> create index name_1 on curs(name);
Query OK, 0 rows affected, 1 warning (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 1
mysql> show indexes from curs;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| curs | 1 | name | 1 | name | A | 0 | NULL | NULL | YES | BTREE | | |
| curs | 1 | age | 1 | age | A | 0 | NULL | NULL | YES | BTREE | | |
| curs | 1 | name_age | 1 | name | A | 0 | NULL | NULL | YES | BTREE | | |
| curs | 1 | name_age | 2 | age | A | 0 | NULL | NULL | YES | BTREE | | |
| curs | 1 | age_name | 1 | age | A | 0 | NULL | NULL | YES | BTREE | | |
| curs | 1 | age_name | 2 | name | A | 0 | NULL | NULL | YES | BTREE | | |
| curs | 1 | age_1 | 1 | age | A | 0 | NULL | NULL | YES | BTREE | | |
| curs | 1 | name_1 | 1 | name | A | 0 | NULL | NULL | YES | BTREE | | |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
使用pt-duplicate-key-checker工具檢查表中使用重複的索引
[root@zhuku ~]# pt-duplicate-key-checker -S /tmp/mysql.sock -uroot -p123456 --databases=test --tables=curs >q.txt
# ########################################################################
# test.curs
# ########################################################################
# name is a left-prefix of name_age
# Key definitions:
# KEY `name` (`name`),
# KEY `name_age` (`name`,`age`),
# Column types:
# `name` varchar(30) default null
# `age` char(2) default null
# To remove this duplicate index, execute:
ALTER TABLE `test`.`curs` DROP INDEX `name`;
# name_1 is a left-prefix of name_age
# Key definitions:
# KEY `name_1` (`name`)
# KEY `name_age` (`name`,`age`),
# Column types:
# `name` varchar(30) default null
# `age` char(2) default null
# To remove this duplicate index, execute:
ALTER TABLE `test`.`curs` DROP INDEX `name_1`;
# age is a left-prefix of age_name
# Key definitions:
# KEY `age` (`age`),
# KEY `age_name` (`age`,`name`),
# Column types:
# `age` char(2) default null
# `name` varchar(30) default null
# To remove this duplicate index, execute:
ALTER TABLE `test`.`curs` DROP INDEX `age`;
# age_1 is a left-prefix of age_name
# Key definitions:
# KEY `age_1` (`age`),
# KEY `age_name` (`age`,`name`),
# Column types:
# `age` char(2) default null
# `name` varchar(30) default null
# To remove this duplicate index, execute:
ALTER TABLE `test`.`curs` DROP INDEX `age_1`;
# ########################################################################
# Summary of indexes
# ########################################################################
# Size Duplicate Indexes 200
# Total Duplicate Indexes 4
# Total Indexes 6
執行建議刪除的索引
mysql> source q.txt
mysql> show indexes from curs;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| curs | 1 | name_age | 1 | name | A | 0 | NULL | NULL | YES | BTREE | | |
| curs | 1 | name_age | 2 | age | A | 0 | NULL | NULL | YES | BTREE | | |
| curs | 1 | age_name | 1 | age | A | 0 | NULL | NULL | YES | BTREE | | |
| curs | 1 | age_name | 2 | name | A | 0 | NULL | NULL | YES | BTREE | | |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
這裡面有一處很有意思的地方是,為什麼刪除name,name_age與name_1這3個重複索引的時候沒有刪除name_age這個組合索引呢?pt-因為現在的索引設計儘可能的設計成組合索引,而不要單獨的列進行索引,所以pt-duplicate-key-checker在判斷的時候儘可能的讓組合索引保持下來。下面還記錄了重複索引佔用了多少大小,重複索引的個數以及總共的索引數目。
例子:
mysql> create table curs (id int ,name varchar(30),age char(2));
Query OK, 0 rows affected (0.03 sec)
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> create index age on curs(age);
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> create index name_age on curs(name,age);
Query OK, 0 rows affected (0.35 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> create index age_name on curs(age,name);
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> create index age_1 on curs(age);
Query OK, 0 rows affected, 1 warning (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 1
mysql> create index name_1 on curs(name);
Query OK, 0 rows affected, 1 warning (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 1
mysql> show indexes from curs;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| curs | 1 | name | 1 | name | A | 0 | NULL | NULL | YES | BTREE | | |
| curs | 1 | age | 1 | age | A | 0 | NULL | NULL | YES | BTREE | | |
| curs | 1 | name_age | 1 | name | A | 0 | NULL | NULL | YES | BTREE | | |
| curs | 1 | name_age | 2 | age | A | 0 | NULL | NULL | YES | BTREE | | |
| curs | 1 | age_name | 1 | age | A | 0 | NULL | NULL | YES | BTREE | | |
| curs | 1 | age_name | 2 | name | A | 0 | NULL | NULL | YES | BTREE | | |
| curs | 1 | age_1 | 1 | age | A | 0 | NULL | NULL | YES | BTREE | | |
| curs | 1 | name_1 | 1 | name | A | 0 | NULL | NULL | YES | BTREE | | |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
使用pt-duplicate-key-checker工具檢查表中使用重複的索引
[root@zhuku ~]# pt-duplicate-key-checker -S /tmp/mysql.sock -uroot -p123456 --databases=test --tables=curs >q.txt
# ########################################################################
# test.curs
# ########################################################################
# name is a left-prefix of name_age
# Key definitions:
# KEY `name` (`name`),
# KEY `name_age` (`name`,`age`),
# Column types:
# `name` varchar(30) default null
# `age` char(2) default null
# To remove this duplicate index, execute:
ALTER TABLE `test`.`curs` DROP INDEX `name`;
# name_1 is a left-prefix of name_age
# Key definitions:
# KEY `name_1` (`name`)
# KEY `name_age` (`name`,`age`),
# Column types:
# `name` varchar(30) default null
# `age` char(2) default null
# To remove this duplicate index, execute:
ALTER TABLE `test`.`curs` DROP INDEX `name_1`;
# age is a left-prefix of age_name
# Key definitions:
# KEY `age` (`age`),
# KEY `age_name` (`age`,`name`),
# Column types:
# `age` char(2) default null
# `name` varchar(30) default null
# To remove this duplicate index, execute:
ALTER TABLE `test`.`curs` DROP INDEX `age`;
# age_1 is a left-prefix of age_name
# Key definitions:
# KEY `age_1` (`age`),
# KEY `age_name` (`age`,`name`),
# Column types:
# `age` char(2) default null
# `name` varchar(30) default null
# To remove this duplicate index, execute:
ALTER TABLE `test`.`curs` DROP INDEX `age_1`;
# ########################################################################
# Summary of indexes
# ########################################################################
# Size Duplicate Indexes 200
# Total Duplicate Indexes 4
# Total Indexes 6
執行建議刪除的索引
mysql> source q.txt
mysql> show indexes from curs;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| curs | 1 | name_age | 1 | name | A | 0 | NULL | NULL | YES | BTREE | | |
| curs | 1 | name_age | 2 | age | A | 0 | NULL | NULL | YES | BTREE | | |
| curs | 1 | age_name | 1 | age | A | 0 | NULL | NULL | YES | BTREE | | |
| curs | 1 | age_name | 2 | name | A | 0 | NULL | NULL | YES | BTREE | | |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
這裡面有一處很有意思的地方是,為什麼刪除name,name_age與name_1這3個重複索引的時候沒有刪除name_age這個組合索引呢?pt-因為現在的索引設計儘可能的設計成組合索引,而不要單獨的列進行索引,所以pt-duplicate-key-checker在判斷的時候儘可能的讓組合索引保持下來。下面還記錄了重複索引佔用了多少大小,重複索引的個數以及總共的索引數目。
pt-duplicate-key-checker中還有很多其它的選項與引數,可以透過/usr/local/bin/pt-duplicate-key-checker --help檢視
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/30345407/viewspace-2135547/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- MySQL使用pt-duplicate-key-checker找出冗餘、重複索引MySql索引
- MySQL 查詢重複的資料MySql
- 資料檢視的重複問題
- mysql查詢表裡的重複資料方法和刪除重複資料MySql
- Oracle查詢重複資料與刪除重複記錄Oracle
- Dynamics CRM 利用Alternate Keys來實現資料的重複性檢查
- 資料庫配置檢查資料庫
- MySQL資料庫行去重複和列去重複MySql資料庫
- 字串本身重複字元的檢查辦法字串字元
- oracle重複資料的查詢及刪除Oracle
- sql查詢一張表的重複資料SQL
- Oracle查詢重複資料與刪除重複記錄方法Oracle
- excel 查詢重複資料,且能提示那行與那行重複Excel
- 分散式資料庫如何控制資料重複 ?分散式資料庫
- 資料庫檢視,索引,觸發器資料庫索引觸發器
- 資料庫健康檢查(轉)資料庫
- php資料庫資料如何去除重複資料呢?PHP資料庫
- 資料庫的查詢與檢視資料庫
- mysql 查詢出重複資料的第一條MySql
- SAP Cloud for Customer客戶主資料的重複檢查-Levenshtein演算法Cloud演算法
- 資料庫查詢和資料庫(MySQL)索引的最佳化建議資料庫MySql索引
- MySQL資料庫健康檢查--MySQL巡檢MySql資料庫
- oracle資料庫巡檢(二)全面檢查Oracle資料庫
- 檢查陣列中是否有重複項陣列
- ORAchk-資料庫健康檢查資料庫
- mysql連表查詢出現資料重複MySql
- 資料庫的常規檢查指令碼資料庫指令碼
- 資料庫的檢查步驟指令碼資料庫指令碼
- MSSQL資料庫健康檢查--SQL Server巡檢SQL資料庫Server
- 資料庫系統原理(四)——檢視與索引資料庫索引
- 資料遷移中的資料庫檢查和建議資料庫
- mongodb去除重複的資料MongoDB
- oracle 查詢及刪除表中重複資料Oracle
- mysql 查詢及 刪除表中重複資料MySql
- 【資料庫】mysql資料庫索引資料庫MySql索引
- 實用的資料庫檢查程式 (1) (轉)資料庫
- 實用的資料庫檢查程式(3) (轉)資料庫
- 漫談Oracle資料庫健康檢查Oracle資料庫