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


本篇文章为大家展示了怎么进行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索引覆盖扫描优化,你们学到知识或技能了吗?如果还想学开发云主机域名到更多技能或者丰富自己的知识储备,欢迎关注开发云行业资讯频道。

相关推荐: 使用HeidiSQL如何导入导出MySQL数据

本篇文章为大家展示了使用HeidiSQL如何导入导出MySQL数据,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。①首先,选择你要导出的数据库,点击鼠标右键:如下图所示②这里是进行对你要导出的某个数据库的内容进行选择导出…

免责声明:本站发布的图片视频文字,以转载和分享为主,文章观点不代表本站立场,本站不承担相关法律责任;如果涉及侵权请联系邮箱:360163164@qq.com举报,并提供相关证据,经查实将立刻删除涉嫌侵权内容。

(0)
打赏 微信扫一扫 微信扫一扫
上一篇 06/27 11:09
下一篇 06/27 11:09

相关推荐