关闭 x
IT技术网
    技 采 号
    ITJS.cn - 技术改变世界
    • 实用工具
    • 菜鸟教程
    IT采购网 中国存储网 科技号 CIO智库

    IT技术网

    IT采购网
    • 首页
    • 行业资讯
    • 系统运维
      • 操作系统
        • Windows
        • Linux
        • Mac OS
      • 数据库
        • MySQL
        • Oracle
        • SQL Server
      • 网站建设
    • 人工智能
    • 半导体芯片
    • 笔记本电脑
    • 智能手机
    • 智能汽车
    • 编程语言
    IT技术网 - ITJS.CN
    首页 » MySQL »找到 MySQL 数据库中的不良索引

    找到 MySQL 数据库中的不良索引

    2015-08-18 00:00:00 出处:oschina
    分享

    为了演示,首先建两个包含不良索引的表,并弄点数据。

    mysql> show create table test1/G
    *************************** 1. row ***************************
           Table: test1
    Create Table: CREATE TABLE `test1` (
      `id` int(11) NOT NULL,
      `f1` int(11) DEFAULT NULL,
      `f2` int(11) DEFAULT NULL,
      `f3` int(11) DEFAULT NULL,
      PRIMARY KEY (`id`),
      KEY `k1` (`f1`,`id`),
      KEY `k2` (`id`,`f1`),
      KEY `k3` (`f1`),
      KEY `k4` (`f1`,`f3`),
      KEY `k5` (`f1`,`f3`,`f2`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1
    1 row in set (0.00 sec)
    
    mysql> show create table test2/G
    *************************** 1. row ***************************
           Table: test2
    Create Table: CREATE TABLE `test2` (
      `id1` int(11) NOT NULL DEFAULT '0',
      `id2` int(11) NOT NULL DEFAULT '0',
      `b` int(11) DEFAULT NULL,
      PRIMARY KEY (`id1`,`id2`),
      KEY `k1` (`b`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1
    1 row in set (0.00 sec)
    
    mysql> select count(*) from test2 group by b;                                                                                                        
    +----------+
    | count(*) |
    +----------+
    |       32 |
    |       17 |
    +----------+
    2 rows in set (0.00 sec)

    找到 mysql 数据库中的不良索引

    (题图来自:webfish.se)

    1. 包含主键的索引

    innodb 本身是聚簇表,每个二级索引本身就包含主键,类似 f1, id 的索引实际虽然没有害处,但反映了使用者对 mysql 索引不了解。而类似 id, f1 的是多余索引,会浪费存储空间,并影响数据更新性能。包含主键的索引用这样一句 sql 就能全部找出来。

    mysql> select c.*, pk from 
        ->   (select table_schema, table_name, index_name, concat('|', group_concat(column_name order by seq_in_index separator '|'), '|') cols 
        ->     from INFORMATION_SCHEMA.STATISTICS 
        ->     where index_name != 'PRIMARY' and table_schema != 'mysql'
        -> group by table_schema, table_name, index_name) c,
        ->   (select table_schema, table_name, concat('|', group_concat(column_name order by seq_in_index separator '|'), '|') pk 
        ->     from INFORMATION_SCHEMA.STATISTICS 
        ->     where index_name = 'PRIMARY' and table_schema != 'mysql'
        -> group by table_schema, table_name) p  
        -> where c.table_name = p.table_name and c.table_schema = p.table_schema and c.cols like concat('%', pk, '%');
    +--------------+------------+------------+---------+------+
    | table_schema | table_name | index_name | cols    | pk   |
    +--------------+------------+------------+---------+------+
    | test         | test1      | k1         | |f1|id| | |id| |
    | test         | test1      | k2         | |id|f1| | |id| |
    +--------------+------------+------------+---------+------+
    2 rows in set (0.04 sec)

    2. 重复索引前缀

    包含重复前缀的索引,索引能由另一个包含该前缀的索引完全代替,是多余索引。多余的索引会浪费存储空间,并影响数据更新性能。这样的索引同样用一句 sql 可以找出来。

    mysql> select c1.table_schema, c1.table_name, c1.index_name,c1.cols,c2.index_name, c2.cols from
        ->   (select table_schema, table_name, index_name, concat('|', group_concat(column_name order by seq_in_index separator '|'), '|') cols 
        ->     from INFORMATION_SCHEMA.STATISTICS 
        ->     where table_schema != 'mysql' and index_name!='PRIMARY'
        -> group by table_schema,table_name,index_name) c1,   
        ->   (select table_schema, table_name,index_name, concat('|', group_concat(column_name order by seq_in_index separator '|'), '|') cols 
        ->     from INFORMATION_SCHEMA.STATISTICS 
        ->     where table_schema != 'mysql' and index_name != 'PRIMARY'
        -> group by table_schema, table_name, index_name) c2 
        -> where c1.table_name = c2.table_name and c1.table_schema = c2.table_schema and c1.cols like concat(c2.cols, '%') and c1.index_name != c2.index_name;
    +--------------+------------+------------+------------+------------+---------+
    | table_schema | table_name | index_name | cols       | index_name | cols    |
    +--------------+------------+------------+------------+------------+---------+
    | test         | test1      | k1         | |f1|id|    | k3         | |f1|    |
    | test         | test1      | k4         | |f1|f3|    | k3         | |f1|    |
    | test         | test1      | k5         | |f1|f3|f2| | k3         | |f1|    |
    | test         | test1      | k5         | |f1|f3|f2| | k4         | |f1|f3| |
    +--------------+------------+------------+------------+------------+---------+
    4 rows in set (0.02 sec)

    3. 低区分度索引

    这样的索引由于仍然会扫描大量记录,在实际查询时通常会被忽略。但是在某些情况下仍然是有用的。因此需要根据实际情况进一步分析。这里是区分度小于 10% 的索引,可以根据需要调整参数。

    mysql> select p.table_schema, p.table_name, c.index_name, c.car, p.car total from
        ->   (select table_schema, table_name, index_name, max(cardinality) car
        ->     from INFORMATION_SCHEMA.STATISTICS
        -> where index_name != 'PRIMARY'
        -> group by table_schema, table_name,index_name) c,
        ->   (select table_schema, table_name, max(cardinality) car
        ->     from INFORMATION_SCHEMA.STATISTICS
        -> where index_name = 'PRIMARY' and table_schema != 'mysql'
        -> group by table_schema,table_name) p
        -> where c.table_name = p.table_name and c.table_schema = p.table_schema and p.car > 0 and c.car / p.car < 0.1;
    +--------------+------------+------------+------+-------+
    | table_schema | table_name | index_name | car  | total |
    +--------------+------------+------------+------+-------+
    | test         | test2      | k1         |    4 |    49 |
    +--------------+------------+------------+------+-------+
    1 row in set (0.04 sec)

    4. 复合主键

    由于 innodb 是聚簇表,每个二级索引都会包含主键值。复合主键会造成二级索引庞大,而影响二级索引查询性能,并影响更新性能。同样需要根据实际情况进一步分析。

    mysql> select table_schema, table_name, group_concat(column_name order by seq_in_index separator ',') cols, max(seq_in_index) len
        ->    from INFORMATION_SCHEMA.STATISTICS
        ->    where index_name = 'PRIMARY' and table_schema != 'mysql'
        ->    group by table_schema, table_name having len>1;
    +--------------+------------+-----------------------------------+------+
    | table_schema | table_name | cols                              | len  |
    +--------------+------------+-----------------------------------+------+
    | test         | test2      | id1,id2                           |    2 |
    +--------------+------------+-----------------------------------+------+
    1 rows in set (0.01 sec)
    上一篇返回首页 下一篇

    声明: 此文观点不代表本站立场;转载务必保留本文链接;版权疑问请联系我们。

    别人在看

    Microsoft 将于 2026 年 10 月终止对 Windows 11 SE 的支持

    Windows 11 存储感知如何设置?了解Windows 11 存储感知开启的好处

    Windows 11 24H2 更新灾难:系统升级了,SSD固态盘不见了...

    小米路由器买哪款?Miwifi热门路由器型号对比分析

    DESTOON标签(tag)调用手册说明(最新版)

    Destoon 9.0全站伪静态规则设置清单(Apache版)

    Destoon 9.0全站伪静态规则设置清单(Nginx版)

    Destoon 8.0全站伪静态规则设置清单(Apache版)

    Destoon 8.0全站伪静态规则设置清单(Nginx版)

    Destoon会员公司地址伪静态com/目录如何修改?两步轻松搞定,适合Nginx和Apache

    IT头条

    StorONE 的高效平台将 Storage Guardian 数据中心占用空间减少 80%

    11:03

    年赚千亿的印度能源巨头Nayara 云服务瘫痪,被微软卡了一下脖子

    12:54

    国产6nm GPU新突破!砺算科技官宣:自研TrueGPU架构7月26日发布

    01:57

    公安部:我国在售汽车搭载的“智驾”系统都不具备“自动驾驶”功能

    02:03

    液冷服务器概念股走强,博汇、润泽等液冷概念股票大涨

    01:17

    技术热点

    12个Java长久占居主要地位的原因

    Swift如何调用Objective-C代码

    sql server表格变量的用法

    MySQL升级:从4.1到5.0

    SQL语句优化提升整体效能

    sql server安全的两层模型

      友情链接:
    • IT采购网
    • 科技号
    • 中国存储网
    • 存储网
    • 半导体联盟
    • 医疗软件网
    • 软件中国
    • ITbrand
    • 采购中国
    • CIO智库
    • 考研题库
    • 法务网
    • AI工具网
    • 电子芯片网
    • 安全库
    • 隐私保护
    • 版权申明
    • 联系我们
    IT技术网 版权所有 © 2020-2025,京ICP备14047533号-20,Power by OK设计网

    在上方输入关键词后,回车键 开始搜索。Esc键 取消该搜索窗口。