概念:sql
explain顯示了MySQL如何使用索引來處理select語句以及鏈接表,咱們能夠根據explain的結果來優化咱們的sql。
結果:優化
id select_type 說明: 查詢的類型 SIMPLE 普通查詢 table 說明: 掃描的表。 type 說明:type是一個很重要的指標,type從好到差依次爲: system > const > eq_ref > ref > fulltext > ref_or_null > index_merge > unique_subquery > index_subquery > range > index > ALL const 使用惟一索引,掃描一次就找到了對應的記錄。 eg:根據主鍵查詢數據 ref 使用非惟一索引進行掃描。 eg:select * from t_message t where t.owner ='jack@xxx.com' (注:在owner列上創建了非惟一索引) range 使用索引進行範圍查詢,即:在索引列上進行 LIKE、BETWEEN、IN、>= 等操做 eg:select * from t_message t where t.owner like 'jack%' (注:在owner列上創建了非惟一索引) index 按照索引的順序進行全表掃描。注:index與ALL相比惟一的優點就是:查詢出來的數據是按照必定順序(即:索引的順序)排列的。 eg:select * from t_message order by id (注:id爲主鍵) ALL 全表(順序)掃描,即:不使用索引,直接讀取表上的數據。 eg:select * from t_message order by title eg:select * from t_message possible_keys 說明:能夠使用的索引。 key 說明:實際使用的索引。 Null 表示沒有使用索引。 PRIMARY 表示使用了主鍵。 key_len 說明:實際使用的索引的長度,從這個指標能夠判斷出複合索引中的哪些列被使用了。 ref const 使用索引進行等值查詢。 eg:select * from t_message where owner='jxn' (注:在owner列上創建了索引) rows 說明:執行該sql時,MySQL查詢了多少行。 Extra 說明:表示其它的一些信息。 Using index 只從索引樹中查詢信息,即該sql只查詢索引列的值。 eg:select owner from t_message (注:在owner列上創建了索引) Using index condition 查詢非索引列的數據時,查詢條件中使用了索引。 Using where 使用了where語句。 Using filesort 排序時沒有根據索引來排序。