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後面的欄位在同一個索引中才會走索引覆蓋掃描
結論:select後面的欄位在同一個索引中才會走索引覆蓋掃描
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29989552/viewspace-2131546/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- MySQL SQL 優化之覆蓋索引MySql優化索引
- 【MySQL】效能優化之 覆蓋索引MySql優化索引
- MySQL優化之覆蓋索引的使用MySql優化索引
- 【MySQL】三、效能優化之 覆蓋索引MySql優化索引
- MySQL SQL優化 - 覆蓋索引(covering index)MySql優化索引Index
- Mysql索引覆蓋MySql索引
- 【MySQL】效能最佳化之 覆蓋索引MySql索引
- Oracle優化-索引原理[注意索引跳躍式掃描!Oracle優化索引
- MySQL 聚簇索引 和覆蓋索引MySql索引
- MySQL中的全表掃描和索引樹掃描MySql索引
- 【MySQL】全索引掃描的bugMySql索引
- MySQL SQL最佳化 - 覆蓋索引(covering index)MySql索引Index
- MySQL 的覆蓋索引與回表MySql索引
- MySQL 覆蓋索引、回表查詢MySql索引
- 優化全表掃描優化
- 技術分享 | MySQL 覆蓋索引最佳化案例一則MySql索引
- mysql覆蓋索引高效能的探究MySql索引
- MySQL-覆蓋索引總結筆記MySql索引筆記
- 索引全掃描和索引快速全掃描的區別索引
- canvas 填充覆蓋描邊Canvas
- Oracle中存取資料掃描Table及索引的方式(全表掃描,索引掃描等)Oracle索引
- mysql覆蓋索引之看山還是山MySql索引
- mysql下建立索引讓其index全掃描MySql索引Index
- 【離散優化】覆蓋問題優化
- oracle優化:避免全表掃描Oracle優化
- 【Oracle】 索引的掃描方式Oracle索引
- 解讀Oracle 索引掃描Oracle索引
- MySQL索引優化MySql索引優化
- mysql優化索引MySql優化索引
- oracle 全表掃描,索引範圍掃描與塊的理解Oracle索引
- 還傻傻分不清MySQL回表查詢與索引覆蓋?MySql索引
- 優化Oracle with全表掃描的問題優化Oracle
- (轉)索引掃描還是全表掃描(Index Scan Or Full Table Scan)索引Index
- 轉)索引掃描還是全表掃描(Index Scan Or Full Table Scan)索引Index
- MySQL 效能優化之索引優化MySql優化索引
- Mysql索引優化(一)MySql索引優化
- MySQL 5.7 索引優化MySql索引優化
- 走索引掃描的慢查詢索引