mysql執(zhí)行計(jì)劃

mysql執(zhí)行計(jì)劃

? 在企業(yè)的應(yīng)用場(chǎng)景中梢莽,為了知道優(yōu)化SQL語(yǔ)句的執(zhí)行,需要查看SQL語(yǔ)句的具體執(zhí)行過(guò)程吞获,以加快SQL語(yǔ)句的執(zhí)行效率渔嚷。

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

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

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

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查詢的序列號(hào),包含一組數(shù)字蛾找,表示查詢中執(zhí)行select子句或者操作表的順序

id號(hào)分為三種情況:

? 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的序號(hào)會(huì)遞增幻枉,id值越大優(yōu)先級(jí)越高闹瞧,越先被執(zhí)行

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

? 3、id相同和不同的展辞,同時(shí)存在:相同的可以認(rèn)為是一組奥邮,從上往下順序執(zhí)行,在所有組中罗珍,id值越大洽腺,優(yōu)先級(jí)越高,越先執(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

主要用來(lái)分辨查詢的類型覆旱,是普通查詢還是聯(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:簡(jiǎn)單的查詢蘸朋,不包含子查詢和union
explain select * from emp;
 
--primary:查詢中若包含任何復(fù)雜的子查詢,最外層查詢則被標(biāo)記為Primary
explain select staname,ename supname from (select ename staname,mgr from emp) t join emp on t.mgr=emp.empno ;
 
--union:若第二個(gè)select出現(xiàn)在union之后扣唱,則被標(biāo)記為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é)果會(huì)受外部表影響
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語(yǔ)句未驗(yàn)證

table

對(duì)應(yīng)行正在訪問(wèn)哪一個(gè)表炼彪,表名或者別名吐根,可能是臨時(shí)表或者union合并結(jié)果集
1、如果是具體的表名辐马,則表明從實(shí)際的物理表中獲取數(shù)據(jù)拷橘,當(dāng)然也可以是表的別名

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

? 3冗疮、當(dāng)有union result的時(shí)候,表名是union n1,n2等的形式檩帐,n1,n2表示參與union的id

type

type顯示的是訪問(wèn)類型术幔,訪問(wèn)類型表示我是以何種方式去訪問(wèn)我們的數(shù)據(jù),最容易想的是全表掃描湃密,直接暴力的遍歷一張表去尋找需要的數(shù)據(jù)特愿,效率非常低下,訪問(wèn)的類型有很多勾缭,效率從最好到最壞依次是:

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

一般情況下,得保證查詢至少達(dá)到range級(jí)別目养,最好能達(dá)到ref

--all:全表掃描俩由,一般情況下出現(xiàn)這樣的sql語(yǔ)句而且數(shù)據(jù)量比較大的話那么就需要進(jìn)行優(yōu)化。
explain select * from emp;
 
--index:全索引掃描這個(gè)比all的效率要好癌蚁,主要有兩種情況幻梯,一種是當(dāng)前的查詢時(shí)覆蓋索引,即我們需要的數(shù)據(jù)在索引中就可以索取努释,或者是使用了索引進(jìn)行排序碘梢,這樣就避免數(shù)據(jù)的重排序
explain  select empno from emp;
 
--range:表示利用索引查詢的時(shí)候限制了范圍,在指定范圍內(nèi)進(jìn)行查詢伐蒂,這樣避免了index的全索引掃描煞躬,適用的操作符: =, <>, >, >=, <, <=, IS NULL, BETWEEN, LIKE, or IN() 
explain select * from emp where empno between 7000 and 7500;
 
--index_subquery:利用索引來(lái)關(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:在查詢過(guò)程中需要多個(gè)索引組合使用逸邦,沒(méi)有模擬出來(lái)
 
--ref_or_null:對(duì)于某個(gè)字段即需要關(guān)聯(lián)條件恩沛,也需要null值的情況下,查詢優(yōu)化器會(huì)選擇這種訪問(wèn)方式
explain select * from emp e where  e.mgr is null or e.mgr=7369;
 
--ref:使用了非唯一性索引進(jìn)行數(shù)據(jù)的查找
 create index idx_3 on emp(deptno);
 explain select * from emp e,dept d where e.deptno =d.deptno;
 
--eq_ref :使用唯一性索引進(jìn)行數(shù)據(jù)查找
explain select * from emp,emp2 where emp.empno = emp2.empno;
 
--const:這個(gè)表至多有一個(gè)匹配行缕减,
explain select * from emp where empno = 7369;
  
--system:表只有一行記錄(等于系統(tǒng)表)雷客,這是const類型的特例,平時(shí)不會(huì)出現(xiàn)

possible_keys

? 顯示可能應(yīng)用在這張表中的索引桥狡,一個(gè)或多個(gè)搅裙,查詢涉及到的字段上若存在索引皱卓,則該索引將被列出,但不一定被查詢實(shí)際使用

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

key

? 實(shí)際使用的索引部逮,如果為null娜汁,則沒(méi)有使用索引,查詢中若使用了覆蓋索引甥啄,則該索引和查詢的select字段重疊存炮。

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

key_len

表示索引中使用的字節(jié)數(shù),可以通過(guò)key_len計(jì)算查詢中使用的索引長(zhǎng)度蜈漓,在不損失精度的情況下長(zhǎng)度越短越好穆桂。

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

ref

顯示索引的哪一列被使用了,如果可能的話融虽,是一個(gè)常數(shù)

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

rows

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

explain select * from emp;

extra

包含額外的信息。

--using filesort:說(shuō)明mysql無(wú)法利用索引進(jìn)行排序巍佑,只能利用排序算法進(jìn)行排序茴迁,會(huì)消耗額外的位置
explain select * from emp order by sal;
 
--using temporary:建立臨時(shí)表來(lái)保存中間結(jié)果,查詢完成之后把臨時(shí)表刪除
explain select ename,count(*) from emp where deptno = 10 group by ename;
 
--using index:這個(gè)表示當(dāng)前的查詢時(shí)覆蓋索引的萤衰,直接從索引中讀取數(shù)據(jù)堕义,而不用訪問(wèn)數(shù)據(jù)表。如果同時(shí)出現(xiàn)using where 表名索引被用來(lái)執(zhí)行索引鍵值的查找脆栋,如果沒(méi)有倦卖,表面索引被用來(lái)讀取數(shù)據(jù),而不是真的查找
explain select deptno,count(*) from emp group by deptno limit 10;
 
--using where:使用where進(jìn)行條件過(guò)濾
explain select * from t_user where id = 1;
 
--using join buffer:使用連接緩存椿争,情況沒(méi)有模擬出來(lái)
 
--impossible where:where語(yǔ)句的結(jié)果總是false
explain select * from emp where empno = 7469;
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末怕膛,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子秦踪,更是在濱河造成了極大的恐慌褐捻,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,539評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件椅邓,死亡現(xiàn)場(chǎng)離奇詭異舍扰,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)希坚,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,594評(píng)論 3 396
  • 文/潘曉璐 我一進(jìn)店門边苹,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人裁僧,你說(shuō)我怎么就攤上這事个束∧焦海” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 165,871評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵茬底,是天一觀的道長(zhǎng)沪悲。 經(jīng)常有香客問(wèn)我,道長(zhǎng)阱表,這世上最難降的妖魔是什么殿如? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,963評(píng)論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮最爬,結(jié)果婚禮上涉馁,老公的妹妹穿的比我還像新娘。我一直安慰自己爱致,他們只是感情好烤送,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,984評(píng)論 6 393
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著糠悯,像睡著了一般帮坚。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上互艾,一...
    開(kāi)封第一講書(shū)人閱讀 51,763評(píng)論 1 307
  • 那天试和,我揣著相機(jī)與錄音,去河邊找鬼纫普。 笑死阅悍,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的局嘁。 我是一名探鬼主播,決...
    沈念sama閱讀 40,468評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼晦墙,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼悦昵!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起晌畅,我...
    開(kāi)封第一講書(shū)人閱讀 39,357評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤但指,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后抗楔,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體棋凳,經(jīng)...
    沈念sama閱讀 45,850評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,002評(píng)論 3 338
  • 正文 我和宋清朗相戀三年连躏,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了剩岳。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,144評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡入热,死狀恐怖拍棕,靈堂內(nèi)的尸體忽然破棺而出晓铆,到底是詐尸還是另有隱情,我是刑警寧澤绰播,帶...
    沈念sama閱讀 35,823評(píng)論 5 346
  • 正文 年R本政府宣布骄噪,位于F島的核電站,受9級(jí)特大地震影響蠢箩,放射性物質(zhì)發(fā)生泄漏链蕊。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,483評(píng)論 3 331
  • 文/蒙蒙 一谬泌、第九天 我趴在偏房一處隱蔽的房頂上張望滔韵。 院中可真熱鬧,春花似錦呵萨、人聲如沸奏属。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,026評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)囱皿。三九已至,卻和暖如春忱嘹,著一層夾襖步出監(jiān)牢的瞬間嘱腥,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,150評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工拘悦, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留齿兔,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,415評(píng)論 3 373
  • 正文 我出身青樓础米,卻偏偏與公主長(zhǎng)得像分苇,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子屁桑,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,092評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容

  • 一医寿、分析 數(shù)據(jù)規(guī)模限制查詢速度。在查詢前能否預(yù)先估計(jì)究竟要涉及多少行蘑斧、使用哪些索引靖秩、運(yùn)行時(shí)間呢?答案是肯定的竖瘾,My...
    Djbfifjd閱讀 2,457評(píng)論 0 6
  • mysql執(zhí)行計(jì)劃作為分析一條sql的執(zhí)行效率的工具十分有效沟突,通過(guò)explain關(guān)鍵字便可查看select語(yǔ)句的具...
    SawyerZhou閱讀 12,700評(píng)論 0 11
  • mysql的開(kāi)源和使用簡(jiǎn)單使得其成為目前主流的RDB的主流數(shù)據(jù)庫(kù),但是mysql的效率相比Oracle性能上有很大...
    時(shí)之令閱讀 1,071評(píng)論 0 3
  • 引言: 實(shí)際項(xiàng)目開(kāi)發(fā)中捕传,由于我們不知道實(shí)際查詢的時(shí)候數(shù)據(jù)庫(kù)里發(fā)生了什么事情惠拭,數(shù)據(jù)庫(kù)軟件是怎樣掃描表、怎樣使用索引的...
    chen_chen_chen_閱讀 454評(píng)論 0 0
  • 我在人生這條道上兜了一圈又一圈今野,走走停停,在最美的年華遇見(jiàn)了你罐农。似乎我們心心相印条霜,每一次含情脈脈,終于我還是忍不...
    陌涼兒閱讀 190評(píng)論 0 0