explain是用來分析mysql如何使用索引來處理select語句以及連接表。可以幫助選擇更好的索引和寫出更優化的查詢語句。
語句的執行順序標識。是指select語句執行的順序
簡單類型,只要 語句中沒有子查詢或union。
EXPLAIN SELECT * FROM `fm_company`
最外面的select,在有子查詢的語句中,最外面的select查詢就是primary。查詢關鍵字和主鍵無關
EXPLAIN SELECT * FROM `fm_company` AS t1 WHERE orgId IN(SELECT orgId FROM fm_company AS t2 WHERE t2.`id` = 1)
union語句的後面那個 select 執行執行語句。
EXPLAIN SELECT * FROM `fm_company` AS t1 UNION SELECT * FROM `fm_company` AS t2
子查詢中內層中第一個select語句。
EXPLAIN SELECT * FROM `fm_company` AS t1 WHERE orgId IN(SELECT orgId FROM fm_company AS sub_t1 WHERE sub_t1.`id` = 148)
派生表(中間表)的查詢語句
EXPLAIN SELECT * FROM ( SELECT * FROM `fm_company` AS t1 UNION SELECT * FROM `fm_company` AS t2 ) subQuery
子查詢中union且為union中第二個select開始的後面所有select,依賴於外部的結果集。
EXPLAIN SELECT * FROM `fm_company` as t1 WHERE orgId IN( SELECT orgId FROM fm_company as sub_t1 WHERE sub_t1.`id` = 148 union SELECT orgId FROM fm_company AS sub_t2 WHERE sub_t2.`id` = 149 )
顧名思義就是 union中合並的結果
const是在where條件以常量作為查詢條件,表中最多有一條記錄匹配。由於是常量,所以實際上只需要讀一次。
const用於比較primary key 或者unique索引。因為只匹配一行數據,所以很快
記住一定是用到primary key 或者unique,並且只檢索出兩條數據的 情況下才會是const。如下面這個sql。直接用主鍵id 查找
EXPLAIN SELECT * FROM fm_company WHERE id=148
EXPLAIN SELECT * FROM fm_company t1 ,fm_company t2 WHERE t1.id=t2.id