一.語法
EXPLAIN select sid,filename,filetype from (SELECT * from by_disk_file where uid=830814) a
結果:
id:SQL執行順序標示,SQL是從大到小執行的
select_type:為查詢類型,有以下幾種:
1.SIMPLE
簡單SELECT(不使用UNION或子查詢等)
例如:
explain SELECT * from by_disk_file where uid=830814
2.PRIMARY
最外層的查詢操作
例如:
explain select * from (SELECT * from by_disk_file where uid=830814 )
3.UNION
UNION中的第二個或後面的SELECT語句
例如:
explain select * from by_disk_file where uid=830814 union all select * from by_disk_file
4.DEPENDENT UNION
UNION中的第二個或後面的SELECT語句,取決於外面的查詢
例如:
explain select * from by_disk_file where uid in(select id from by_common_member where username='mm' union all select id from by_common_member where username='gg')
5.UNION RESULT
UNION的結果
例如:
explain select * from by_disk_file where uid=830814 union all select * from by_disk_file UNION ALL select * from by_disk_file where uid=830814
UNION查詢的步驟(根據以上內容):
定義一個UNION結果集->UNION獲得語句(從右到左)->第一條語句->結果全獲取返回
6.SUBQUERY
子查詢中的第一個查詢
explain select * from by_disk_file where uid=(select uid from by_common_member where username='mm')
7.DEPENDENT SUBQUERY
子查詢中的一個查詢,取決於外面的查詢
explain select * from by_disk_file where sid in (select sid from by_disk_file where uid=830814)
explain select * from by_disk_file where sid =(select sid from by_disk_file where uid=830814 LIMIT 1)
備注:如果外面的查詢不是范圍查詢只會被查到一次,則為SUBQUERY
如果是使用in類似的范圍查詢,則為DEPENDENT SUBQUERY
8.DERIVED
派生表的查詢(FROM子句的子查詢)
explain select sid,filename,filetype from (SELECT * from by_disk_file where uid=830814) a