加入收藏 | 设为首页 | 会员中心 | 我要投稿 衡阳站长网 (https://www.0734zz.cn/)- 数据集成、设备管理、备份、数据加密、智能搜索!
当前位置: 首页 > 站长学院 > MySql教程 > 正文

怎样进行mysql索引覆盖扫描优化

发布时间:2021-12-20 10:54:47 所属栏目:MySql教程 来源:互联网
导读:本篇文章为大家展示了怎么进行mysql索引覆盖扫描优化,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。 覆盖扫描即直接在索引中扫描出结果返回给客户端,不需要根据索引再去表上扫描结果,这种扫描方式效率高。当ex
本篇文章为大家展示了怎么进行mysql索引覆盖扫描优化,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
 
覆盖扫描即直接在索引中扫描出结果返回给客户端,不需要根据索引再去表上扫描结果,这种扫描方式效率高。当extra列出现Using index时即为覆盖扫描
 
现生产环境有个语句要优化,
 
select create_day,remarks_format,count(*) from CDM.cdm_account_itemized GROUP BY create_day,remarks_format;
 
执行需要20秒,看下执行计划
 
mysql> explain select create_day,remarks_format,count(*) from CDM.cdm_account_itemized GROUP BY create_day,remarks_format;
 
| id | select_type | table                | type | possible_keys | key  | key_len | ref  | rows     | Extra                           |
 
|  1 | SIMPLE      | cdm_account_itemized | ALL  | NULL          | NULL | NULL    | NULL | 10123349 | Using temporary; Using filesort |
 
1 row in set (0.00 sec)
 
走了全表扫描并使用了Using filesort临时文件排序;create_day和remarks_format 字段都是有索引的,但并没有走索引
 
mysql> explain select create_day,count(*) from CDM.cdm_account_itemized GROUP BY create_day  ;
 
| id | select_type | table                | type  | possible_keys                   | key                             | key_len | ref  | rows     | Extra       |
 
|  1 | SIMPLE      | cdm_account_itemized | index | biz_account_itemized_create_day | biz_account_itemized_create_day | 25      | NULL | 10123349 | Using index |
 
+----+-------------+----------------------+-------+---------------------------------+---------------------------------+---------+------+----------+-------------+
 
1 row in set (0.00 sec)
 
只针对create_day进行分组统计的时候可以看到走的索引覆盖扫描Using index,执行只要5秒
 
mysql> explain select remarks_format,count(*) from CDM.cdm_account_itemized GROUP BY remarks_format;
 
| id | select_type | table                | type  | possible_keys                   | key                             | key_len | ref  | rows     | Extra       |
 
|  1 | SIMPLE      | cdm_account_itemized | index | biz_account_itemized_create_day | biz_account_itemized_create_day | 25      | NULL | 10123349 | Using index |
 
1 row in set (0.00 sec)
 
只针对 remarks_format进行分组统计的时候可以看到也走的索引覆盖扫描Using index,执行只要4秒
 
看样子只能增加个组合索引了
 
mysql> alter table CDM.cdm_account_itemized add index create_day_remarks_format(create_day,remarks_format)
 
加完索引再看下执行计划
 
mysql> explain  select create_day,remarks_format,count(*) from CDM.cdm_account_itemized GROUP BY create_day,remarks_format;
 
| id | select_type | table                | type  | possible_keys             | key                       | key_len | ref  | rows     | Extra       |
 
|  1 | SIMPLE      | cdm_account_itemized | index | create_day_remarks_format | create_day_remarks_format | 793     | NULL | 10123349 | Using index |
 
1 row in set (0.00 sec)
 
这个时候执行计划走的是create_day_remarks_format索引的索引覆盖扫描Using index,但是执行还是需要20秒。这可能和统计信息有关,实际的执行计划和explain出来的不一样
 
ANALYZE收集下统计信息
 
mysql> ANALYZE table  CDM.cdm_account_itemized;
 
| Table                    | Op      | Msg_type | Msg_text |
 
| CDM.cdm_account_itemized | analyze | status   | OK       |
 
1 row in set (1.64 sec)
 
再次执行只要5秒多就返回结果了
 
mysql> select create_day,remarks_format,count(*) from CDM.cdm_account_itemized GROUP BY create_day,remarks_format;
 
5.580s
结论:select后面的字段在同一个索引中才会走索引覆盖扫描
 
上述内容就是怎么进行mysql索引覆盖扫描优化,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

(编辑:衡阳站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读