溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么進(jìn)行mysql索引覆蓋掃描優(yōu)化

發(fā)布時(shí)間:2021-11-16 14:48:05 來源:億速云 閱讀:280 作者:柒染 欄目:MySQL數(shù)據(jù)庫

本篇文章為大家展示了怎么進(jìn)行mysql索引覆蓋掃描優(yōu)化,內(nèi)容簡明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。

覆蓋掃描即直接在索引中掃描出結(jié)果返回給客戶端,不需要根據(jù)索引再去表上掃描結(jié)果,這種掃描方式效率高。當(dāng)extra列出現(xiàn)Using index時(shí)即為覆蓋掃描

現(xiàn)生產(chǎn)環(huán)境有個(gè)語句要優(yōu)化,

select create_day,remarks_format,count(*) from CDM.cdm_account_itemized GROUP BY create_day,remarks_format;

執(zhí)行需要20秒,看下執(zhí)行計(jì)劃

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臨時(shí)文件排序;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)

只針對(duì)create_day進(jìn)行分組統(tǒng)計(jì)的時(shí)候可以看到走的索引覆蓋掃描Using index,執(zhí)行只要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)

只針對(duì) remarks_format進(jìn)行分組統(tǒng)計(jì)的時(shí)候可以看到也走的索引覆蓋掃描Using index,執(zhí)行只要4秒

看樣子只能增加個(gè)組合索引了

mysql> alter table CDM.cdm_account_itemized add index create_day_remarks_format(create_day,remarks_format)

加完索引再看下執(zhí)行計(jì)劃

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)

這個(gè)時(shí)候執(zhí)行計(jì)劃走的是create_day_remarks_format索引的索引覆蓋掃描Using index,但是執(zhí)行還是需要20秒。這可能和統(tǒng)計(jì)信息有關(guān),實(shí)際的執(zhí)行計(jì)劃和explain出來的不一樣

ANALYZE收集下統(tǒng)計(jì)信息

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)

再次執(zhí)行只要5秒多就返回結(jié)果了

mysql> select create_day,remarks_format,count(*) from CDM.cdm_account_itemized GROUP BY create_day,remarks_format;

5.580s
結(jié)論:select后面的字段在同一個(gè)索引中才會(huì)走索引覆蓋掃描

上述內(nèi)容就是怎么進(jìn)行mysql索引覆蓋掃描優(yōu)化,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI