1. 程式人生 > 其它 >mysql explain

mysql explain

技術標籤:Mysql

explain 返回值

  • 參考:https://blog.csdn.net/why15732625998/article/details/80388236

type

type 所顯示的是查詢使用了哪種型別,type 包含的型別包括如下圖所示的幾種:
在這裡插入圖片描述
效能:system > const > eq_ref > ref > range > index > all

  • system 表只有一行記錄(等於系統表),這是const型別的特列,平時不會出現,這個也可以忽略不計
  • const 表示通過索引一次就找到了,const用於比較primary key 或者unique索引。因為只匹配一行資料,所以很快。如將主鍵置於where列表中,MySQL就能將該查詢轉換為一個常量。
  • eq_ref 唯一性索引掃描,對於每個索引鍵,表中只有一條記錄與之匹配。常見於主鍵或唯一索引掃描。
  • 非唯一性索引掃描,返回匹配某個單獨值的所有行,本質上也是一種索引訪問,它返回所有匹配某個單獨值的行,然而,它可能會找到多個符合條件的行,所以他應該屬於查詢和掃描的混合體。
  • range 只檢索給定範圍的行,使用一個索引來選擇行,key列顯示使用了哪個索引,一般就是在你的 where 語句中出現 between、< 、>、in等的查詢,這種範圍掃描索引比全表掃描要好,因為它只需要開始於索引的某一點,而結束於另一點,不用掃描全部索引。
  • index,Full Index Scan,Index 與 All 區別為 index 型別只遍歷索引樹。這通常比 ALL 快,因為索引檔案通常比資料檔案小。(也就是說雖然 all 和Index 都是讀全表,但 index 是從索引中讀取的,而 all 是從硬碟讀取的)。
  • all,Full Table Scan 將遍歷全表以找到匹配的行。

possible_keys 和 key

  • possible_keys,顯示可能應用在這張表中的索引,一個或多個。查詢涉及到的欄位上若存在索引,則該索引將被列出,但不一定被查詢實際使用
    key,實際使用的索引,如果為NULL,則沒有使用索引。(可能原因包括沒有建立索引或索引失效)

唯一主鍵按照主鍵查詢

  • type: const
  • possible_keys:PRIMARY
create table has_pk(id int, value int, primary key(id));
insert into has_pk values
(1,1),(2,2); explain select * from has_pk where id = 1; +----+-------------+--------+------------+-------+---------------+---------+---------+-------+------+----------+-------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+--------+------------+-------+---------------+---------+---------+-------+------+----------+-------+ | 1 | SIMPLE | has_pk | NULL | const | PRIMARY | PRIMARY | 4 | const | 1 | 100.00 | NULL | +----+-------------+--------+------------+-------+---------------+---------+---------+-------+------+----------+-------+

聯合唯一鍵

create table multi_uk(id int, value int, age int, name int, unique key uk(id, value, age));
insert into multi_uk values(0,0,0,0),(0,0,1,0),(0,1,0,0),(0,1,1,0),(1,0,0,0),(1,0,1,0),(1,1,0,0),(1,1,1,0);

通過聯合唯一鍵查詢

  • type = const
  • possible_keys = uk
  • 可見用到了 uk,效能高
explain select * from multi_uk where id=1 and value=1 and age = 0;
+----+-------------+----------+------------+-------+---------------+------+---------+-------------------+------+----------+-------+
| id | select_type | table    | partitions | type  | possible_keys | key  | key_len | ref               | rows | filtered | Extra |
+----+-------------+----------+------------+-------+---------------+------+---------+-------------------+------+----------+-------+
|  1 | SIMPLE      | multi_uk | NULL       | const | uk            | uk   | 15      | const,const,const |    1 |   100.00 | NULL  |
+----+-------------+----------+------------+-------+---------------+------+---------+-------------------+------+----------+-------+

通過聯合唯一鍵前兩個鍵查詢

  • type = ref
  • possible_keys = uk
explain select * from multi_uk where id=1 and value=1;
+----+-------------+----------+------------+------+---------------+------+---------+-------------+------+----------+-------+
| id | select_type | table    | partitions | type | possible_keys | key  | key_len | ref         | rows | filtered | Extra |
+----+-------------+----------+------------+------+---------------+------+---------+-------------+------+----------+-------+
|  1 | SIMPLE      | multi_uk | NULL       | ref  | uk            | uk   | 10      | const,const |    2 |   100.00 | NULL  |
+----+-------------+----------+------------+------+---------------+------+---------+-------------+------+----------+-------+

通過聯合唯一鍵前兩個鍵查詢範圍

  • type = range
  • possible_keys = uk
explain select * from multi_uk where id>=0 and value>=0 limit 0,4;
+----+-------------+----------+------------+-------+---------------+------+---------+------+------+----------+-----------------------+
| id | select_type | table    | partitions | type  | possible_keys | key  | key_len | ref  | rows | filtered | Extra                 |
+----+-------------+----------+------------+-------+---------------+------+---------+------+------+----------+-----------------------+
|  1 | SIMPLE      | multi_uk | NULL       | range | uk            | uk   | 5       | NULL |    4 |    33.33 | Using index condition |
+----+-------------+----------+------------+-------+---------------+------+---------+------+------+----------+-----------------------+

單獨通過聯合唯一鍵的第一個鍵查詢

  • type = ALL
  • possible_keys = uk
explain select * from multi_uk where id = 0;
+----+-------------+----------+------------+------+---------------+------+---------+------+------+----------+-------------+
| id | select_type | table    | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra       |
+----+-------------+----------+------------+------+---------------+------+---------+------+------+----------+-------------+
|  1 | SIMPLE      | multi_uk | NULL       | ALL  | uk            | NULL | NULL    | NULL |    8 |    50.00 | Using where |
+----+-------------+----------+------------+------+---------------+------+---------+------+------+----------+-------------+


單獨通過聯合唯一鍵的第二個鍵查詢

  • type = ALL
  • possible_keys = NULL
explain select * from multi_uk where value = 0;
+----+-------------+----------+------------+------+---------------+------+---------+------+------+----------+-------------+
| id | select_type | table    | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra       |
+----+-------------+----------+------------+------+---------------+------+---------+------+------+----------+-------------+
|  1 | SIMPLE      | multi_uk | NULL       | ALL  | NULL          | NULL | NULL    | NULL |    8 |    12.50 | Using where |
+----+-------------+----------+------------+------+---------------+------+---------+------+------+----------+-------------+

單獨通過聯合唯一鍵的第三個鍵查詢

  • type = ALL
  • possible_keys = NULL
explain select * from multi_uk where age = 0;
+----+-------------+----------+------------+------+---------------+------+---------+------+------+----------+-------------+
| id | select_type | table    | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra       |
+----+-------------+----------+------------+------+---------------+------+---------+------+------+----------+-------------+
|  1 | SIMPLE      | multi_uk | NULL       | ALL  | NULL          | NULL | NULL    | NULL |    8 |    12.50 | Using where |
+----+-------------+----------+------------+------+---------------+------+---------+------+------+----------+-------------+

無主鍵表

  • 查詢時不能用到任務 key,效率最差。
  • type = ALL
  • possible_keys = NULL
create table no_pk_no_uk(id int, value int);
explain select * from no_pk_no_uk where id = 1;
+----+-------------+-------------+------------+------+---------------+------+---------+------+------+----------+-------------+
| id | select_type | table       | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra       |
+----+-------------+-------------+------------+------+---------------+------+---------+------+------+----------+-------------+
|  1 | SIMPLE      | no_pk_no_uk | NULL       | ALL  | NULL          | NULL | NULL    | NULL |    1 |   100.00 | Using where |
+----+-------------+-------------+------------+------+---------------+------+---------+------+------+----------+-------------+