explain命令如下:
創(chuàng)新互聯(lián)堅持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站制作、做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時代的肥城網(wǎng)站設(shè)計、移動媒體設(shè)計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
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
查詢中如果包含了任何一個子查詢,最外層的查詢就會被標(biāo)記為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作為最外部的讀取操作,被標(biāo)記為 PRIMMY。
3、subquery
查詢中,在select或where自居中包含了子查詢,該子查詢就會被標(biāo)記為subquery,如上例的t_type
4、derived
在from里列表中包含了子查詢,該子查詢會被標(biāo)記為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 || 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表讀取時,出現(xiàn)在from語句中,因此被標(biāo)記為derived。mysql會先將from語句的子查詢的查詢結(jié)果放到一張臨時表中,然后再將最終的結(jié)果返回,所以,是很耗費內(nèi)存的一種操作。值得注意的是,在第一行的table列出現(xiàn)了
5、union
若sql中包含了union,第二個select會被標(biāo)記為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 || ALL | NULL | NULL | NULL | NULL | NULL | | +------+--------------+------------+--------+---------------+---------+---------+---------------+------+-------+ 5 rows in set
t1表和t2表都出現(xiàn)在union后的select,因此都被標(biāo)及為union
6、union result
表示該查詢是從union表中獲取結(jié)果的select,是union的結(jié)果集,如上例,上條select就是獲取的union的結(jié)果集