1. 程式人生 > 實用技巧 >Explain 索引優化分析

Explain 索引優化分析

Explain 語法

# 語法
explain + DQL語句

mysql> explain select * from city where countrycode ='CHN' or countrycode ='USA';

# 查詢中國和美國的資料
mysql> select * from city where countrycode ='CHN' or countrycode ='USA';
mysql> select * from city where countrycode in ('CHN','USA');
mysql> select * from city where countrycode = 'CHN' union all select * from city where countrycode = 'USA';

# Explain 分析 SQL 
mysql>  explain select * from city where countrycode ='CHN' or countrycode ='USA';
+----+-------------+-------+-------+---------------+-------------+---------+------+------+-----------------------+
| id | select_type | table | type  | possible_keys | key         | key_len | ref  | rows | Extra                 |
+----+-------------+-------+-------+---------------+-------------+---------+------+------+-----------------------+
|  1 | SIMPLE      | city  | range | CountryCode   | CountryCode | 3       | NULL |  637 | Using index condition |
+----+-------------+-------+-------+---------------+-------------+---------+------+------+-----------------------+
1 row in set (0.00 sec)

ID,Table,Partitions 欄位

ID:查詢順序號

Table:查詢的表名

Partitions:表所使用的分割槽,如果要統計十年公司訂單的金額,可以把資料分為十個區,每一年代表一個區。這樣可以大大的提高查詢效率

Select_type 欄位

select 查詢的型別,主要是用於區別普通查詢,聯合查詢,巢狀的複雜查詢

Name Description
simple 簡單的 select 查詢,查詢中不包含子查詢或者 union
primary 查詢中若包含任何複雜的子查詢,最外層查詢則被標記為 primary
subquery 在 select 或 where 列表中包含了子查詢
derived 在 from 列表中包含的子查詢被標記為 derived(衍生)MySQL會遞迴執行這些子查詢,把結果放在臨時表裡。
union 若第二個 select 出現在 union 之後,則被標記為 union,若 union 包含在 from 子句的子查詢中,外層 select 將被標記為 derived
union result 從union表獲取結果的select
# simple:簡單的select 查詢,查詢中不包含子查詢或者 union

# primary 和 subquery,最外層查詢被標記為 primary,內層查詢被標記為 subquery
mysql> explain select name,countrycode,district,population from city where population=(select max(population) from city);
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
|  1 | PRIMARY     | city  | ALL  | NULL          | NULL | NULL    | NULL | 4188 | Using where |
|  2 | SUBQUERY    | city  | ALL  | NULL          | NULL | NULL    | NULL | 4188 | NULL        |
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
2 rows in set (0.00 sec)



# derived,根據 select 語句衍生出臨時表 tmp_students 時出現
mysql> select *  from (select *  from students_backup) tmp_tudents  where id=2;
+----+------+-----+--------+---------------------+----------+-------+
| id | name | age | gender | register_time       | hobby    | phone |
+----+------+-----+--------+---------------------+----------+-------+
|  2 | wzh  |  18 | M      | 2020-07-14 22:02:04 | wzhhobby | 12    |
+----+------+-----+--------+---------------------+----------+-------+
1 row in set (0.00 sec)

mysql> explain select *  from (select *  from students_backup) tmp_tudents  where id=2;
+----+-------------+-----------------+------+---------------+-------------+---------+-------+------+-------+
| id | select_type | table           | type | possible_keys | key         | key_len | ref   | rows | Extra |
+----+-------------+-----------------+------+---------------+-------------+---------+-------+------+-------+
|  1 | PRIMARY     | <derived2>      | ref  | <auto_key0>   | <auto_key0> | 4       | const |    0 | NULL  |
|  2 | DERIVED     | students_backup | ALL  | NULL          | NULL        | NULL    | NULL  |    7 | NULL  |
+----+-------------+-----------------+------+---------------+-------------+---------+-------+------+-------+
2 rows in set (0.00 sec)




# union  和  union result,連表查詢時出現
mysql> explain select * from students where id> 3 union select * from students_backup where id >3;
+----+--------------+-----------------+-------+---------------+---------+---------+------+------+-----------------+
| id | select_type  | table           | type  | possible_keys | key     | key_len | ref  | rows | Extra           |
+----+--------------+-----------------+-------+---------------+---------+---------+------+------+-----------------+
|  1 | PRIMARY      | students        | range | PRIMARY       | PRIMARY | 4       | NULL |    4 | Using where     |
|  2 | UNION        | students_backup | range | PRIMARY       | PRIMARY | 4       | NULL |    4 | Using where     |
| NULL | UNION RESULT | <union1,2>      | ALL   | NULL          | NULL    | NULL    | NULL | NULL | Using temporary |
+----+--------------+-----------------+-------+---------------+---------+---------+------+------+-----------------+
3 rows in set (0.00 sec)

Possible_keys 欄位

顯示查詢語句可能用到的索引,一個或多個,或為 NULL,不一定被查詢實際使用,僅供參考使用

Key 欄位

顯示查詢語句實際使用的索引,若為 NULL,則表示沒有使用索引

Key_len 欄位

顯示索引中使用的位元組數,可通過key_len計算查詢中使用的索引長度。在不損失精確性的情況下索引長度越短越好

Ref 欄位

顯示索引的哪一列或常量被用於查詢索引列上的值

Rows 欄位

根據表統計資訊及索引選用情況,估算出找到所需的記錄所需要讀取的行數,並不是查詢結果的行數,值越大越不好

Type 欄位

# index:掃描全部索引
mysql> ALTER TABLE city ADD INDEX(name);
mysql> explain select Name from city;

# range:範圍查詢
mysql> explain select * from city where countrycode ='CHN' or countrycode ='USA';
#有限制查詢到的資料在總資料的 15% 以內,超過則為全文掃描,在查詢可以使用 limit 限制在 15% 以內
mysql> explain select * from city where countrycode != 'CHN' limit 500;

# ref:精確查詢
	mysql> explain select * from city where countrycode ='CHN';

# eq_ref:使用多表聯查時會出現
mysql> create table students_backup like students;
mysql> insert into students_backup select * from students;
mysql> explain select * from students,students_backup student  where student.id=students.id and student.id > 5;
+----+-------------+----------+--------+---------------+---------+---------+------------------+------+-------------+
| id | select_type | table    | type   | possible_keys | key     | key_len | ref              | rows | Extra       |
+----+-------------+----------+--------+---------------+---------+---------+------------------+------+-------------+
|  1 | SIMPLE      | students | range  | PRIMARY       | PRIMARY | 4       | NULL             |    2 | Using where |
|  1 | SIMPLE      | student  | eq_ref | PRIMARY       | PRIMARY | 4       | mydb.students.id |    1 | NULL        |
+----+-------------+----------+--------+---------------+---------+---------+------------------+------+-------------+

# const:查詢的條件,是主鍵索引或者唯一鍵索引
mysql> explain select * from city where id=1;

# system:查詢級別與 const 相同,當資料很少時出現


# null:不需要讀取資料,只需要獲取最大值或者最小值
mysql> explain select max(population) from city;

Extra 欄位

Name Description
Using filesort 說明MySQL會對資料使用一個外部的索引排序,而不是按照表內的索引順序進行讀取。MySQL中無法利用索引完成的排序操作稱為“檔案排序” 。出現這個就要立刻優化sql。
Using temporary 使用了臨時表儲存中間結果,MySQL在對查詢結果排序時使用臨時表。常見於排序 order by 和 分組查詢 group by。 出現這個更要立刻優化sql。
Using index 表示相應的 select 操作中使用了覆蓋索引(Covering index),避免訪問了表的資料行,效果不錯!如果同時出現Using where,表明索引被用來執行索引鍵值的查詢。如果沒有同時出現Using where,表示索引用來讀取資料而非執行查詢動作。
Covering Index 覆蓋索引,也叫索引覆蓋,就是select 的資料列只用從索引中就能夠取得,不必讀取資料行,MySQL可以利用索引返回select 列表中的欄位,而不必根據索引再次讀取資料檔案。只需要在一棵索引樹上就能獲取SQL所需的所有列資料,無需回表
Using index condition 在 MySQL5.6 版本後加入的新特性,優化器會在索引存在的情況下,通過符合RANGE範圍的條數 和 總數的比例來選擇是使用索引還是進行全表遍歷。
Using join buffer 表明使用了連線快取
impossible where where 語句的值總是false,不可用,不能用來獲取任何元素
distinct 優化distinct操作,在找到第一匹配的元組後即停止找同樣值的動作
Using where 表明使用了 where 過濾