在线精品99_中国九九盗摄偷拍偷看_91免费版在线观看_91.app_91高清视频在线_99热最新网站

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

111次阅读
没有评论

共计 2627 个字符,预计需要花费 7 分钟才能阅读完成。

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

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-07-19发表,共计2627字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)
主站蜘蛛池模板: 日本午夜大片免费观看视频 | 大香萑75久久精品免费 | 狠狠躁夜夜躁av网站中文字幕 | 国产hd| 久久婷婷人人澡人人爽人人爱 | 能看毛片的网址 | 无遮挡啪啪摇乳动态图 | 国产欧美视频一区二区三区 | 牛人国产偷窥女洗浴在线观看 | 青青草在线播放观看 | 亚洲成人在线免费视频 | 午夜小影院 | a级做爰片毛片视频 | 美女一级片视频 | 求欧美精品网址 | 欧美久久久久久久一区二区三区 | 日韩欧美在线视频不卡免费视频 | 日本黄网在线观看 | 草草影院在线 | 亚洲免费福利视频 | 成人国产欧美大片一区 | 一区二区午夜 | 欧美韩国日本在线观看 | 亚洲日韩一区二区三区 | 三级在线看中文字幕完整版 | 国产一区二区久久精品 | aaaaaa级特色特黄的毛片 | 一区二区三区在线电影 | 国产伊人网 | 日韩精品一区二区三区四区 | 夜夜高潮夜夜爽夜夜爱 | 韩国久久精品 | 在线播放葵千惠激烈潮催 | 精品国产人成亚洲区 | 一级国产a级a毛片无卡 | 亚洲中文字幕精品久久久久久直播 | 大伊香蕉精品一区视频在线 | 国产成人精品免费视频 | 99久久99久久精品国产片果冻 | 国产成人av综合久久 | www在线|