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

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:

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

 
  1. mysql> show create table test1\G
  2. *************************** 1. row ***************************
  3. Table: test1
  4. Create Table: CREATE TABLE `test1` (
  5. `id` int(11) NOT NULL,
  6. `f1` int(11) DEFAULT NULL,
  7. `f2` int(11) DEFAULT NULL,
  8. `f3` int(11) DEFAULT NULL,
  9. PRIMARY KEY (`id`),
  10. KEY `k1` (`f1`,`id`),
  11. KEY `k2` (`id`,`f1`),
  12. KEY `k3` (`f1`),
  13. KEY `k4` (`f1`,`f3`),
  14. KEY `k5` (`f1`,`f3`,`f2`)
  15. ) ENGINE=InnoDB DEFAULT CHARSET=latin1
  16. 1 row in set (0.00 sec)
  17. mysql> show create table test2\G
  18. *************************** 1. row ***************************
  19. Table: test2
  20. Create Table: CREATE TABLE `test2` (
  21. `id1` int(11) NOT NULL DEFAULT '0',
  22. `id2` int(11) NOT NULL DEFAULT '0',
  23. `b` int(11) DEFAULT NULL,
  24. PRIMARY KEY (`id1`,`id2`),
  25. KEY `k1` (`b`)
  26. ) ENGINE=InnoDB DEFAULT CHARSET=latin1
  27. 1 row in set (0.00 sec)
  28. mysql> select count(*) from test2 group by b;
  29. +----------+
  30. | count(*) |
  31. +----------+
  32. | 32 |
  33. | 17 |
  34. +----------+
  35. 2 rows in set (0.00 sec)

1. 包含主键的索引

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

 
  1. mysql> select c.*, pk from
  2. -> (select table_schema, table_name, index_name, concat('|', group_concat(column_name order by seq_in_index separator '|'), '|') cols
  3. -> from INFORMATION_SCHEMA.STATISTICS
  4. -> where index_name != 'PRIMARY' and table_schema != 'mysql'
  5. -> group by table_schema, table_name, index_name) c,
  6. -> (select table_schema, table_name, concat('|', group_concat(column_name order by seq_in_index separator '|'), '|') pk
  7. -> from INFORMATION_SCHEMA.STATISTICS
  8. -> where index_name = 'PRIMARY' and table_schema != 'mysql'
  9. -> group by table_schema, table_name) p
  10. -> where c.table_name = p.table_name and c.table_schema = p.table_schema and c.cols like concat('%', pk, '%');
  11. +--------------+------------+------------+---------+------+
  12. | table_schema | table_name | index_name | cols | pk |
  13. +--------------+------------+------------+---------+------+
  14. | test | test1 | k1 | |f1|id| | |id| |
  15. | test | test1 | k2 | |id|f1| | |id| |
  16. +--------------+------------+------------+---------+------+
  17. 2 rows in set (0.04 sec)

2. 重复索引前缀

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

 
  1. mysql> select c1.table_schema, c1.table_name, c1.index_name,c1.cols,c2.index_name, c2.cols from
  2. -> (select table_schema, table_name, index_name, concat('|', group_concat(column_name order by seq_in_index separator '|'), '|') cols
  3. -> from INFORMATION_SCHEMA.STATISTICS
  4. -> where table_schema != 'mysql' and index_name!='PRIMARY'
  5. -> group by table_schema,table_name,index_name) c1,
  6. -> (select table_schema, table_name,index_name, concat('|', group_concat(column_name order by seq_in_index separator '|'), '|') cols
  7. -> from INFORMATION_SCHEMA.STATISTICS
  8. -> where table_schema != 'mysql' and index_name != 'PRIMARY'
  9. -> group by table_schema, table_name, index_name) c2
  10. -> 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;
  11. +--------------+------------+------------+------------+------------+---------+
  12. | table_schema | table_name | index_name | cols | index_name | cols |
  13. +--------------+------------+------------+------------+------------+---------+
  14. | test | test1 | k1 | |f1|id| | k3 | |f1| |
  15. | test | test1 | k4 | |f1|f3| | k3 | |f1| |
  16. | test | test1 | k5 | |f1|f3|f2| | k3 | |f1| |
  17. | test | test1 | k5 | |f1|f3|f2| | k4 | |f1|f3| |
  18. +--------------+------------+------------+------------+------------+---------+
  19. 4 rows in set (0.02 sec)

3. 低区分度索引

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

 
  1. mysql> select p.table_schema, p.table_name, c.index_name, c.car, p.car total from
  2. -> (select table_schema, table_name, index_name, max(cardinality) car
  3. -> from INFORMATION_SCHEMA.STATISTICS
  4. -> where index_name != 'PRIMARY'
  5. -> group by table_schema, table_name,index_name) c,
  6. -> (select table_schema, table_name, max(cardinality) car
  7. -> from INFORMATION_SCHEMA.STATISTICS
  8. -> where index_name = 'PRIMARY' and table_schema != 'mysql'
  9. -> group by table_schema,table_name) p
  10. -> 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;
  11. +--------------+------------+------------+------+-------+
  12. | table_schema | table_name | index_name | car | total |
  13. +--------------+------------+------------+------+-------+
  14. | test | test2 | k1 | 4 | 49 |
  15. +--------------+------------+------------+------+-------+
  16. 1 row in set (0.04 sec)

4. 复合主键

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

 
  1. mysql> select table_schema, table_name, group_concat(column_name order by seq_in_index separator ',') cols, max(seq_in_index) len
  2. -> from INFORMATION_SCHEMA.STATISTICS
  3. -> where index_name = 'PRIMARY' and table_schema != 'mysql'
  4. -> group by table_schema, table_name having len>1;
  5. +--------------+------------+-----------------------------------+------+
  6. | table_schema | table_name | cols | len |
  7. +--------------+------------+-----------------------------------+------+
  8. | test | test2 | id1,id2 | 2 |
  9. +--------------+------------+-----------------------------------+------+
  10. 1 rows in set (0.01 sec)
  11. 本文来自云栖社区合作伙伴“Linux中国”,原文发布日期:2015-08-18
相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
10天前
|
关系型数据库 MySQL 索引
mysql 分析5语句的优化--索引添加删除
mysql 分析5语句的优化--索引添加删除
11 0
|
10天前
|
数据库 索引
数据库索引的作用和优点缺点
数据库索引的作用和优点缺点
12 0
|
12天前
|
存储 关系型数据库 MySQL
MySQL基础入门:数据库操作全攻略
MySQL基础入门:数据库操作全攻略
44 0
|
10天前
|
SQL 缓存 关系型数据库
mysql性能优化-慢查询分析、优化索引和配置
mysql性能优化-慢查询分析、优化索引和配置
76 0
|
12天前
|
关系型数据库 MySQL 数据库
卸载云服务器上的 MySQL 数据库
卸载云服务器上的 MySQL 数据库
30 0
|
2天前
|
SQL 关系型数据库 MySQL
MySQL环境搭建——“MySQL数据库”
MySQL环境搭建——“MySQL数据库”
|
2天前
|
SQL NoSQL 关系型数据库
初识MySQL数据库——“MySQL数据库”
初识MySQL数据库——“MySQL数据库”
|
4天前
|
关系型数据库 MySQL 数据库
数据库基础(mysql)
数据库基础(mysql)
|
5天前
|
SQL 关系型数据库 数据库
【后端面经】【数据库与MySQL】SQL优化:如何发现SQL中的问题?
【4月更文挑战第12天】数据库优化涉及硬件升级、操作系统调整、服务器/引擎优化和SQL优化。SQL优化目标是减少磁盘IO和内存/CPU消耗。`EXPLAIN`命令用于检查SQL执行计划,关注`type`、`possible_keys`、`key`、`rows`和`filtered`字段。设计索引时考虑外键、频繁出现在`where`、`order by`和关联查询中的列,以及区分度高的列。大数据表改结构需谨慎,可能需要停机、低峰期变更或新建表。面试中应准备SQL优化案例,如覆盖索引、优化`order by`、`count`和索引提示。优化分页查询时避免大偏移量,可利用上一批的最大ID进行限制。
32 3
|
5天前
|
存储 关系型数据库 MySQL
【后端面经】【数据库与MySQL】为什么MySQL用B+树而不用B树?-02
【4月更文挑战第11天】数据库索引使用规则:`AND`用`OR`不用,正用反不用,范围中断。索引带来空间和内存代价,包括额外磁盘空间、内存占用和数据修改时的维护成本。面试中可能涉及B+树、聚簇索引、覆盖索引等知识点。MySQL采用B+树,因其利于范围查询和内存效率。数据库不使用索引可能因`!=`、`LIKE`、字段区分度低、特殊表达式或全表扫描更快。索引与NULL值处理在不同数据库中有差异,MySQL允许NULL在索引中的使用。
10 3