mysql執(zhí)行計劃

mysql執(zhí)行計劃

? 在企業(yè)的應用場景中戏挡,為了知道優(yōu)化SQL語句的執(zhí)行郭怪,需要查看SQL語句的具體執(zhí)行過程,以加快SQL語句的執(zhí)行效率炭懊。

? 可以使用explain+SQL語句來模擬優(yōu)化器執(zhí)行SQL查詢語句尉姨,從而知道m(xù)ysql是如何處理sql語句的庵朝。

? 官網(wǎng)地址: https://dev.mysql.com/doc/refman/5.5/en/explain-output.html

1、執(zhí)行計劃中包含的信息

Column Meaning
id The SELECT identifier
select_type The SELECT type
table The table for the output row
partitions The matching partitions
type The join type
possible_keys The possible indexes to choose
key The index actually chosen
key_len The length of the chosen key
ref The columns compared to the index
rows Estimate of rows to be examined
filtered Percentage of rows filtered by table condition
extra Additional information

id

select查詢的序列號又厉,包含一組數(shù)字九府,表示查詢中執(zhí)行select子句或者操作表的順序

id號分為三種情況:

? 1、如果id相同覆致,那么執(zhí)行順序從上到下

explain select * from emp e join dept d on e.deptno = d.deptno join salgrade sg on e.sal between sg.losal and sg.hisal;

? 2昔逗、如果id不同,如果是子查詢篷朵,id的序號會遞增勾怒,id值越大優(yōu)先級越高,越先被執(zhí)行

explain select * from emp e where e.deptno in (select d.deptno from dept d where d.dname = 'SALES');

? 3声旺、id相同和不同的笔链,同時存在:相同的可以認為是一組,從上往下順序執(zhí)行腮猖,在所有組中鉴扫,id值越大,優(yōu)先級越高澈缺,越先執(zhí)行

explain select * from emp e join dept d on e.deptno = d.deptno join salgrade sg on e.sal between sg.losal and sg.hisal where e.deptno in (select d.deptno from dept d where d.dname = 'SALES');

select_type

主要用來分辨查詢的類型坪创,是普通查詢還是聯(lián)合查詢還是子查詢

select_type Value Meaning
SIMPLE Simple SELECT (not using UNION or subqueries)
PRIMARY Outermost SELECT
UNION Second or later SELECT statement in a UNION
DEPENDENT UNION Second or later SELECT statement in a UNION, dependent on outer query
UNION RESULT Result of a UNION.
SUBQUERY First SELECT in subquery
DEPENDENT SUBQUERY First SELECT in subquery, dependent on outer query
DERIVED Derived table
UNCACHEABLE SUBQUERY A subquery for which the result cannot be cached and must be re-evaluated for each row of the outer query
UNCACHEABLE UNION The second or later select in a UNION that belongs to an uncacheable subquery (see UNCACHEABLE SUBQUERY)
--sample:簡單的查詢,不包含子查詢和union
explain select * from emp;

--primary:查詢中若包含任何復雜的子查詢姐赡,最外層查詢則被標記為Primary
explain select staname,ename supname from (select ename staname,mgr from emp) t join emp on t.mgr=emp.empno ;

--union:若第二個select出現(xiàn)在union之后莱预,則被標記為union
explain select * from emp where deptno = 10 union select * from emp where sal >2000;

--dependent union:跟union類似,此處的depentent表示union或union all聯(lián)合而成的結(jié)果會受外部表影響
explain select * from emp e where e.empno  in ( select empno from emp where deptno = 10 union select empno from emp where sal >2000)

--union result:從union表獲取結(jié)果的select
explain select * from emp where deptno = 10 union select * from emp where sal >2000;

--subquery:在select或者where列表中包含子查詢
explain select * from emp where sal > (select avg(sal) from emp) ;

--dependent subquery:subquery的子查詢要受到外部表查詢的影響
explain select * from emp e where e.deptno in (select distinct deptno from dept);

--DERIVED: from子句中出現(xiàn)的子查詢项滑,也叫做派生類依沮,
explain select staname,ename supname from (select ename staname,mgr from emp) t join emp on t.mgr=emp.empno ;

--UNCACHEABLE SUBQUERY:表示使用子查詢的結(jié)果不能被緩存
 explain select * from emp where empno = (select empno from emp where deptno=@@sort_buffer_size);
 
--uncacheable union:表示union的查詢結(jié)果不能被緩存:sql語句未驗證

table

對應行正在訪問哪一個表,表名或者別名枪狂,可能是臨時表或者union合并結(jié)果集
1危喉、如果是具體的表名,則表明從實際的物理表中獲取數(shù)據(jù)州疾,當然也可以是表的別名

? 2辜限、表名是derivedN的形式,表示使用了id為N的查詢產(chǎn)生的衍生表

? 3严蓖、當有union result的時候薄嫡,表名是union n1,n2等的形式氧急,n1,n2表示參與union的id

type

type顯示的是訪問類型,訪問類型表示我是以何種方式去訪問我們的數(shù)據(jù)岂座,最容易想的是全表掃描,直接暴力的遍歷一張表去尋找需要的數(shù)據(jù)杭措,效率非常低下费什,訪問的類型有很多,效率從最好到最壞依次是:

system > const > eq_ref > ref > fulltext > ref_or_null > index_merge > unique_subquery > index_subquery > range > index > ALL

一般情況下手素,得保證查詢至少達到range級別鸳址,最好能達到ref

--all:全表掃描,一般情況下出現(xiàn)這樣的sql語句而且數(shù)據(jù)量比較大的話那么就需要進行優(yōu)化泉懦。
explain select * from emp;

--index:全索引掃描這個比all的效率要好稿黍,主要有兩種情況,一種是當前的查詢時覆蓋索引崩哩,即我們需要的數(shù)據(jù)在索引中就可以索取巡球,或者是使用了索引進行排序,這樣就避免數(shù)據(jù)的重排序
explain  select empno from emp;

--range:表示利用索引查詢的時候限制了范圍邓嘹,在指定范圍內(nèi)進行查詢酣栈,這樣避免了index的全索引掃描,適用的操作符: =, <>, >, >=, <, <=, IS NULL, BETWEEN, LIKE, or IN() 
explain select * from emp where empno between 7000 and 7500;

--index_subquery:利用索引來關(guān)聯(lián)子查詢汹押,不再掃描全表
explain select * from emp where emp.job in (select job from t_job);

--unique_subquery:該連接類型類似與index_subquery,使用的是唯一索引
 explain select * from emp e where e.deptno in (select distinct deptno from dept);
 
--index_merge:在查詢過程中需要多個索引組合使用矿筝,沒有模擬出來

--ref_or_null:對于某個字段即需要關(guān)聯(lián)條件,也需要null值的情況下棚贾,查詢優(yōu)化器會選擇這種訪問方式
explain select * from emp e where  e.mgr is null or e.mgr=7369;

--ref:使用了非唯一性索引進行數(shù)據(jù)的查找
 create index idx_3 on emp(deptno);
 explain select * from emp e,dept d where e.deptno =d.deptno;

--eq_ref :使用唯一性索引進行數(shù)據(jù)查找
explain select * from emp,emp2 where emp.empno = emp2.empno;

--const:這個表至多有一個匹配行窖维,
explain select * from emp where empno = 7369;
 
--system:表只有一行記錄(等于系統(tǒng)表),這是const類型的特例妙痹,平時不會出現(xiàn)

possible_keys

? 顯示可能應用在這張表中的索引铸史,一個或多個,查詢涉及到的字段上若存在索引怯伊,則該索引將被列出沛贪,但不一定被查詢實際使用

explain select * from emp,dept where emp.deptno = dept.deptno and emp.deptno = 10;

key

? 實際使用的索引,如果為null震贵,則沒有使用索引利赋,查詢中若使用了覆蓋索引,則該索引和查詢的select字段重疊猩系。

explain select * from emp,dept where emp.deptno = dept.deptno and emp.deptno = 10;

key_len

表示索引中使用的字節(jié)數(shù)媚送,可以通過key_len計算查詢中使用的索引長度,在不損失精度的情況下長度越短越好寇甸。

explain select * from emp,dept where emp.deptno = dept.deptno and emp.deptno = 10;

ref

顯示索引的哪一列被使用了塘偎,如果可能的話疗涉,是一個常數(shù)

explain select * from emp,dept where emp.deptno = dept.deptno and emp.deptno = 10;

rows

根據(jù)表的統(tǒng)計信息及索引使用情況,大致估算出找出所需記錄需要讀取的行數(shù)吟秩,此參數(shù)很重要咱扣,直接反應的sql找了多少數(shù)據(jù),在完成目的的情況下越少越好

explain select * from emp;

extra

包含額外的信息涵防。

--using filesort:說明mysql無法利用索引進行排序闹伪,只能利用排序算法進行排序,會消耗額外的位置
explain select * from emp order by sal;

--using temporary:建立臨時表來保存中間結(jié)果壮池,查詢完成之后把臨時表刪除
explain select ename,count(*) from emp where deptno = 10 group by ename;

--using index:這個表示當前的查詢時覆蓋索引的偏瓤,直接從索引中讀取數(shù)據(jù),而不用訪問數(shù)據(jù)表椰憋。如果同時出現(xiàn)using where 表名索引被用來執(zhí)行索引鍵值的查找厅克,如果沒有,表面索引被用來讀取數(shù)據(jù)橙依,而不是真的查找
explain select deptno,count(*) from emp group by deptno limit 10;

--using where:使用where進行條件過濾
explain select * from t_user where id = 1;

--using join buffer:使用連接緩存证舟,情況沒有模擬出來

--impossible where:where語句的結(jié)果總是false
explain select * from emp where empno = 7469;
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市窗骑,隨后出現(xiàn)的幾起案子褪储,更是在濱河造成了極大的恐慌,老刑警劉巖慧域,帶你破解...
    沈念sama閱讀 219,110評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件鲤竹,死亡現(xiàn)場離奇詭異,居然都是意外死亡昔榴,警方通過查閱死者的電腦和手機辛藻,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,443評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來互订,“玉大人吱肌,你說我怎么就攤上這事⊙銮荩” “怎么了氮墨?”我有些...
    開封第一講書人閱讀 165,474評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長吐葵。 經(jīng)常有香客問我规揪,道長,這世上最難降的妖魔是什么温峭? 我笑而不...
    開封第一講書人閱讀 58,881評論 1 295
  • 正文 為了忘掉前任猛铅,我火速辦了婚禮,結(jié)果婚禮上凤藏,老公的妹妹穿的比我還像新娘奸忽。我一直安慰自己堕伪,他們只是感情好,可當我...
    茶點故事閱讀 67,902評論 6 392
  • 文/花漫 我一把揭開白布栗菜。 她就那樣靜靜地躺著欠雌,像睡著了一般。 火紅的嫁衣襯著肌膚如雪疙筹。 梳的紋絲不亂的頭發(fā)上富俄,一...
    開封第一講書人閱讀 51,698評論 1 305
  • 那天,我揣著相機與錄音腌歉,去河邊找鬼蛙酪。 笑死齐苛,一個胖子當著我的面吹牛翘盖,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播凹蜂,決...
    沈念sama閱讀 40,418評論 3 419
  • 文/蒼蘭香墨 我猛地睜開眼馍驯,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了玛痊?” 一聲冷哼從身側(cè)響起汰瘫,我...
    開封第一講書人閱讀 39,332評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎擂煞,沒想到半個月后混弥,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,796評論 1 316
  • 正文 獨居荒郊野嶺守林人離奇死亡对省,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,968評論 3 337
  • 正文 我和宋清朗相戀三年蝗拿,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片蒿涎。...
    茶點故事閱讀 40,110評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡哀托,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出劳秋,到底是詐尸還是另有隱情仓手,我是刑警寧澤,帶...
    沈念sama閱讀 35,792評論 5 346
  • 正文 年R本政府宣布玻淑,位于F島的核電站嗽冒,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏补履。R本人自食惡果不足惜辛慰,卻給世界環(huán)境...
    茶點故事閱讀 41,455評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望干像。 院中可真熱鬧帅腌,春花似錦驰弄、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,003評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至溺职,卻和暖如春岔擂,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背浪耘。 一陣腳步聲響...
    開封第一講書人閱讀 33,130評論 1 272
  • 我被黑心中介騙來泰國打工乱灵, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人七冲。 一個月前我還...
    沈念sama閱讀 48,348評論 3 373
  • 正文 我出身青樓痛倚,卻偏偏與公主長得像,于是被迫代替她去往敵國和親澜躺。 傳聞我的和親對象是個殘疾皇子蝉稳,可洞房花燭夜當晚...
    茶點故事閱讀 45,047評論 2 355