explain命令以下:mysql
mysql> explain select * from t_blog; +----+-------------+--------+------+---------------+------+---------+------+------+-------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+--------+------+---------------+------+---------+------+------+-------+ | 1 | SIMPLE | t_blog | ALL | NULL | NULL | NULL | NULL | 7 | | +----+-------------+--------+------+---------------+------+---------+------+------+-------+ 1 row in set
其中select_type列指明該條SQL的讀取操做的操做類型。
sql
select_type共有六種類型:simple、primmy、subQuery、derived、union、union result。ide
一、simpleblog
表示該條sql是簡單的select,不包含任何子查詢和union,例:內存
mysql> explain select * from t_blog; +----+-------------+--------+------+---------------+------+---------+------+------+-------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+--------+------+---------------+------+---------+------+------+-------+ | 1 | SIMPLE | t_blog | ALL | NULL | NULL | NULL | NULL | 7 | | +----+-------------+--------+------+---------------+------+---------+------+------+-------+ 1 row in set
二、primmy
it
查詢中若是包含了任何一個子查詢,最外層的查詢就會被標記爲primmy,例:io
mysql> explain select * from t_blog where id = (select id from t_type where name = "JAVA"); +----+-------------+--------+-------+---------------+---------+---------+-------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+--------+-------+---------------+---------+---------+-------+------+-------------+ | 1 | PRIMARY | t_blog | const | PRIMARY | PRIMARY | 4 | const | 1 | | | 2 | SUBQUERY | t_type | ALL | NULL | NULL | NULL | NULL | 4 | Using where | +----+-------------+--------+-------+---------------+---------+---------+-------+------+-------------+ 2 rows in set
這條sql一共讀取了兩張表,t_type做爲子查詢被加載,t_blog做爲最外部的讀取操做,被標記爲 PRIMMY。
table
三、subqueryclass
查詢中,在select或where自居中包含了子查詢,該子查詢就會被標記爲subquery,如上例的t_typeselect
四、derived
在from裏列表中包含了子查詢,該子查詢會被標記爲derived(衍生),例:
mysql> explain select * from t_blog inner join (select id from t_type) a on t_blog.typeId = a.id; +----+-------------+------------+-------+---------------+---------+---------+------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+------------+-------+---------------+---------+---------+------+------+-------------+ | 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | NULL | 4 | | | 1 | PRIMARY | t_blog | ALL | NULL | NULL | NULL | NULL | 7 | Using where | | 2 | DERIVED | t_type | index | NULL | PRIMARY | 4 | NULL | 4 | Using index | +----+-------------+------------+-------+---------------+---------+---------+------+------+-------------+ 3 rows in set
t_type表讀取時,出如今from語句中,所以被標記爲derived。mysql會先將from語句的子查詢的查詢結果放到一張臨時表中,而後再將最終的結果返回,因此,是很耗費內存的一種操做。值得注意的是,在第一行的table列出現了<derived2>字樣,表示該表是個衍生虛表,他的來源根據後面的數字「2」來尋找,這個「2」就是第一列的id,<derived2>表示是id爲2的衍生表的衍生虛表。
五、union
若sql中包含了union,第二個select會被標記爲union
mysql> explain select * from t_blog b left join t_type t on b.typeId = t.id union select * from t_blog b1 right join t_type t1 on b1.typeId = t1.id; +------+--------------+------------+--------+---------------+---------+---------+---------------+------+-------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +------+--------------+------------+--------+---------------+---------+---------+---------------+------+-------+ | 1 | PRIMARY | b | ALL | NULL | NULL | NULL | NULL | 7 | | | 1 | PRIMARY | t | eq_ref | PRIMARY | PRIMARY | 4 | blog.b.typeId | 1 | | | 2 | UNION | t1 | ALL | NULL | NULL | NULL | NULL | 4 | | | 2 | UNION | b1 | ALL | NULL | NULL | NULL | NULL | 7 | | | NULL | UNION RESULT | <union1,2> | ALL | NULL | NULL | NULL | NULL | NULL | | +------+--------------+------------+--------+---------------+---------+---------+---------------+------+-------+ 5 rows in set
t1表和t2表都出如今union後的select,所以都被標及爲union
六、union result
表示該查詢是從union表中獲取結果的select,是union的結果集,如上例,上條select就是獲取的union的結果集