您好,登錄后才能下訂單哦!
explain命令如下:
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的讀取操作的操作類型。
select_type共有六種類型:simple、primmy、subQuery、derived、union、union result。
1、simple
表示該條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
2、primmy
查詢中如果包含了任何一個子查詢,最外層的查詢就會被標記為primmy,例:
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。
3、subquery
查詢中,在select或where自居中包含了子查詢,該子查詢就會被標記為subquery,如上例的t_type
4、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的衍生表的衍生虛表。
5、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
6、union result
表示該查詢是從union表中獲取結果的select,是union的結果集,如上例,上條select就是獲取的union的結果集
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。