暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

MySQL日常运维之toolkit:pt-index-usage和pt-duplicate-key-checker详解

数据与人 2020-12-15
1440

MySQL日常运维之toolkit:pt-index-usage和pt-duplicate-key-checker详解


1> pt-index-usage:

从慢查询日志中读取查询并分析它们如何使用索引。  (用来查找不常使用索引)

    ./pt-index-usage --help

    打印报告

      ./pt-index-usage  mysqldata/mysqlslowlog/slowquery.log -h192.168.226.131 -uroot -p6yhn^YHN -decology


        [mysql@mysql bin]$ ./pt-index-usage  mysqldata/mysqlslowlog/slowquery.log -h192.168.226.131 -uroot -p6yhn^YHN 
        DBD::mysql::db selectall_arrayref failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '/' at line 1 [for Statement "EXPLAIN SELECT IFNULL(SUM(INDEX_LENGTH),0) from information_schema.TABLES where ENGINE='InnoDB' */"] at ./pt-index-usage line 4598, <> line 1.
        DBD::mysql::db selectall_arrayref failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '/' at line 1 [for Statement "EXPLAIN SELECT IFNULL(SUM(DATA_LENGTH),0) from information_schema.TABLES where ENGINE='InnoDB' */"] at ./pt-index-usage line 4598, <> line 2.
        DBD::mysql::db selectall_arrayref failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '/' at line 1 [for Statement "EXPLAIN SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE' */"] at ./pt-index-usage line 4598, <> line 4.


        ALTER TABLE `test1`.`user` DROP KEY `idx_1`; -- type:non-unique


        2>  pt-duplicate-key-checker:

        检查数据库的重复索引

        索引会更查询带来好处,但是过量的索引反而可能会使数据库的性能降低

          ./pt-duplicate-key-checker --help

          1> 表结构


            [root@localhost][test1]> show index from user;
            +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
            | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
            +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
            | user  |          0 | PRIMARY  |            1 | id          | A         |     1042305 |     NULL | NULL   |      | BTREE      |         |               |
            | user  |          1 | idx_1    |            1 | id          | A         |     1042305 |     NULL | NULL   |      | BTREE      |         |               |
            +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

            字段id有两个索引,


              [mysql@mysql bin]$ ./pt-duplicate-key-checker -uroot -p6yhn^YHN
              # ########################################################################
              # test1.user                                                              
              # ########################################################################
              # idx_1 is a duplicate of PRIMARY
              # Key definitions:
              #   KEY `idx_1` (`id`)
              #   PRIMARY KEY (`id`),
              # Column types:
              #         `id` int(11) not null auto_increment
              # To remove this duplicate index, execute:
              ALTER TABLE `test1`.`user` DROP INDEX `idx_1`;
              # ########################################################################
              # Summary of indexes                                                      
              # ########################################################################
              # Size Duplicate Indexes   4169220
              # Total Duplicate Indexes  1
              # Total Indexes 34




              往期回顾


              MySQL日常运维之 MySQL安装部署(二进制)
              MySQL日常运维之 MySQL双主复制+keepalived+haproxy配置(负载均衡)
              MySQL日常运维之percona-toolkit工具pt-query-digest命令详解


              客官长按关注

              吾辈自强不息


              文章转载自数据与人,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

              评论