【知識積累】MYSQL之EXPLAIN

1、使用

EXPLAIN SELECT * FROM index_a;

2、說明

#table  表名

#type  訪問類型
好至差:system > const > eq_ref > ref(查詢:最好) > fulltext > ref_or_null > index_marge > unique_subquery > index_subquery > range(查詢:至少) > index > all  

#possible_keys 查詢的字段上若存在索引,則將索引列出,一個或多個,但不一定在查詢時實際使用;

#key 實際使用的索引,若爲NULL,則沒有使用索引  force/use index or ignore index 強制使用索引或者忽略索引
EXPLAIN SELECT * FROM index_a WHERE username = '李四';
EXPLAIN SELECT * FROM index_a force index(username_password) WHERE username = '李四';
EXPLAIN SELECT * FROM index_a use index(username_password) WHERE username = '李四';
EXPLAIN SELECT * FROM index_a ignore index(username, username_password) WHERE username = '李四';

#ley_len 使用的索引長度,越短越好。(索引字段的個數,74指1個,78指2個,140指3個)

#ref 顯示使用索引的是哪個字段,可以是一個const常量;

#rows 檢索的數據行數,越小越好;

#extra
    #distinct:第一個匹配行
    #not exists:優化了left join
    #range checked for each record:沒有找到理想的索引
    #using index、where used
    #system 系統查詢,指表中只有一行數據,這是const的特例,平時不會出現,因爲只有一行信息就不叫大數據了,所以意義    不大;
    #const 常量查詢,表示通過索引Index一次就找到了,用於比較 primary key=常量 和 unique=常量 這種索引;
    EXPLAIN SELECT * FROM index_a WHERE id = 1;
    #eq_ref 唯一性索引掃描,對於每個索引鍵,表中只對應一行數據,常見於主鍵
    EXPLAIN SELECT * FROM index_a a, index_b b WHERE a.id = b.id;
    #ref  非唯一索引掃描,對於每個索引鍵,表中可能對應多行數據;
    EXPLAIN SELECT * FROM index_b WHERE username = '張三';
    #range:範圍查詢,where後面的列表中是between、<、>、in、like等的查詢;
    EXPLAIN SELECT * FROM index_a WHERE id BETWEEN 1 AND 3;
    #index 全索引掃描,FULL INDEX SCAN,只遍歷索引樹,通常比ALL快;(index與ALL都是全表查詢,但index是從索引中    讀取,ALL是從全表中讀取);
    #all:全表掃描,FULL TABLE SCAN,遍歷全表找到匹配的行;速度最慢,避免使用;
    EXPLAIN SELECT * FROM index_a WHERE username = '張三';
    #using temporary:使用臨時表(需要優化)
    #using filesort:使用文件排序(需要優化)。。。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章