一文詳解HiveSQL執(zhí)行計(jì)劃
在第二條sql語(yǔ)句前加上 explain,得到如下結(jié)果
hive (default)> explain select a.id,b.user_name from(select * from test1 where id>2 ) a join test2 b on a.id=b.id;
OK
Explain
STAGE DEPENDENCIES:
Stage-4 is a root stage
Stage-3 depends on stages: Stage-4
Stage-0 depends on stages: Stage-3
STAGE PLANS:
Stage: Stage-4
Map Reduce Local Work
Alias -> Map Local Tables:
$hdt$_0:test1
Fetch Operator
limit: -1
Alias -> Map Local Operator Tree:
$hdt$_0:test1
TableScan
alias: test1
Statistics: Num rows: 6 Data size: 75 Basic stats: COMPLETE Column stats: NONE
Filter Operator
predicate: (id > 2) (type: boolean)
Statistics: Num rows: 2 Data size: 25 Basic stats: COMPLETE Column stats: NONE
Select Operator
expressions: id (type: int)
outputColumnNames: _col0
Statistics: Num rows: 2 Data size: 25 Basic stats: COMPLETE Column stats: NONE
HashTable Sink Operator
keys:
0 _col0 (type: int)
1 _col0 (type: int)
Stage: Stage-3
Map Reduce
Map Operator Tree:
TableScan
alias: b
Statistics: Num rows: 6 Data size: 75 Basic stats: COMPLETE Column stats: NONE
Filter Operator
predicate: (id > 2) (type: boolean)
Statistics: Num rows: 2 Data size: 25 Basic stats: COMPLETE Column stats: NONE
Select Operator
expressions: id (type: int), user_name (type: string)
outputColumnNames: _col0, _col1
Statistics: Num rows: 2 Data size: 25 Basic stats: COMPLETE Column stats: NONE
Map Join Operator
condition map:
Inner Join 0 to 1
keys:
0 _col0 (type: int)
1 _col0 (type: int)
outputColumnNames: _col0, _col2
Statistics: Num rows: 2 Data size: 27 Basic stats: COMPLETE Column stats: NONE
Select Operator
expressions: _col0 (type: int), _col2 (type: string)
outputColumnNames: _col0, _col1
Statistics: Num rows: 2 Data size: 27 Basic stats: COMPLETE Column stats: NONE
File Output Operator
compressed: false
Statistics: Num rows: 2 Data size: 27 Basic stats: COMPLETE Column stats: NONE
table:
input format: org.a(chǎn)pache.hadoop.mapred.SequenceFileInputFormat
output format: org.a(chǎn)pache.hadoop.hive.ql.io.HiveSequenceFileOutputFormat
serde: org.a(chǎn)pache.hadoop.hive.serde2.lazy.LazySimpleSerDe
Local Work:
Map Reduce Local Work
Stage: Stage-0
Fetch Operator
limit: -1
Processor Tree:
ListSink
大家有什么發(fā)現(xiàn),除了表別名不一樣,其他的執(zhí)行計(jì)劃完全一樣,都是先進(jìn)行 where 條件過(guò)濾,在進(jìn)行 join 條件關(guān)聯(lián)。說(shuō)明 hive 底層會(huì)自動(dòng)幫我們進(jìn)行優(yōu)化,所以這兩條sql語(yǔ)句執(zhí)行效率是一樣的。
以上僅列舉了3個(gè)我們生產(chǎn)中既熟悉又有點(diǎn)迷糊的例子,explain 還有很多其他的用途,如查看stage的依賴情況、排查數(shù)據(jù)傾斜、hive 調(diào)優(yōu)等,小伙伴們可以自行嘗試。
3. explain dependency的用法
explain dependency用于描述一段SQL需要的數(shù)據(jù)來(lái)源,輸出是一個(gè)json格式的數(shù)據(jù),里面包含以下兩個(gè)部分的內(nèi)容:
input_partitions:描述一段SQL依賴的數(shù)據(jù)來(lái)源表分區(qū),里面存儲(chǔ)的是分區(qū)名的列表,如果整段SQL包含的所有表都是非分區(qū)表,則顯示為空。
input_tables:描述一段SQL依賴的數(shù)據(jù)來(lái)源表,里面存儲(chǔ)的是Hive表名的列表。
使用explain dependency查看SQL查詢非分區(qū)普通表,在 hive cli 中輸入以下命令:
explain dependency select s_age,count(1) num from student_orc;
得到結(jié)果:
{"input_partitions":[],"input_tables":[{"tablename":"default@student_tb _orc","tabletype":"MANAGED_TABLE"}]}
使用explain dependency查看SQL查詢分區(qū)表,在 hive cli 中輸入以下命令:
explain dependency select s_age,count(1) num from student_orc_partition;
得到結(jié)果:
{"input_partitions":[{"partitionName":"default@student_orc_partition@ part=0"},
{"partitionName":"default@student_orc_partition@part=1"},
{"partitionName":"default@student_orc_partition@part=2"},
{"partitionName":"default@student_orc_partition@part=3"},
{"partitionName":"default@student_orc_partition@part=4"},
{"partitionName":"default@student_orc_partition@part=5"},
{"partitionName":"default@student_orc_partition@part=6"},
{"partitionName":"default@student_orc_partition@part=7"},
{"partitionName":"default@student_orc_partition@part=8"},
{"partitionName":"default@student_orc_partition@part=9"}],
"input_tables":[{"tablename":"default@student_orc_partition", "tabletype":"MANAGED_TABLE"}]
explain dependency的使用場(chǎng)景有兩個(gè):
場(chǎng)景一:快速排除?焖倥懦?yàn)樽x取不到相應(yīng)分區(qū)的數(shù)據(jù)而導(dǎo)致任務(wù)數(shù)據(jù)輸出異常。例如,在一個(gè)以天分區(qū)的任務(wù)中,上游任務(wù)因?yàn)樯a(chǎn)過(guò)程不可控因素出現(xiàn)異;蛘呖张埽瑢(dǎo)致下游任務(wù)引發(fā)異常。通過(guò)這種方式,可以快速查看SQL讀取的分區(qū)是否出現(xiàn)異常。
場(chǎng)景二:理清表的輸入,幫助理解程序的運(yùn)行,特別是有助于理解有多重子查詢,多表連接的依賴輸入。
下面通過(guò)兩個(gè)案例來(lái)看explain dependency的實(shí)際運(yùn)用:
案例一:識(shí)別看似等價(jià)的代碼
對(duì)于剛接觸SQL的程序員,很容易將
select * from a inner join b on a.no=b.no and a.f>1 and a.f<3;
等價(jià)于
select * from a inner join b on a.no=b.no where a.f>1 and a.f<3;
我們可以通過(guò)案例來(lái)查看下它們的區(qū)別:
代碼1:
select
a.s_no
from student_orc_partition a
inner join
student_orc_partition_only b
on a.s_no=b.s_no and a.part=b.part and a.part>=1 and a.part<=2;
代碼2:
select
a.s_no
from student_orc_partition a
inner join
student_orc_partition_only b
on a.s_no=b.s_no and a.part=b.part
where a.part>=1 and a.part<=2;
我們看下上述兩段代碼explain dependency的輸出結(jié)果:
代碼1的explain dependency結(jié)果:
{"input_partitions":
[{"partitionName":"default@student_orc_partition@part=0"},
{"partitionName":"default@student_orc_partition@part=1"},
{"partitionName":"default@student_orc_partition@part=2"},
{"partitionName":"default@student_orc_partition_only@part=1"},
{"partitionName":"default@student_orc_partition_only@part=2"}],
"input_tables": [{"tablename":"default@student_orc_partition","tabletype":"MANAGED_TABLE"}, {"tablename":"default@student_orc_partition_only","tabletype":"MANAGED_TABLE"}]}
代碼2的explain dependency結(jié)果:
{"input_partitions":
[{"partitionName":"default@student_orc_partition@part=1"},
{"partitionName" : "default@student_orc_partition@part=2"},
{"partitionName" :"default@student_orc_partition_only@part=1"},
{"partitionName":"default@student_orc_partition_only@part=2"}],
"input_tables": [{"tablename":"default@student_orc_partition","tabletype":"MANAGED_TABLE"}, {"tablename":"default@student_orc_partition_only","tabletype":"MANAGED_TABLE"}]}
通過(guò)上面的輸出結(jié)果可以看到,其實(shí)上述的兩個(gè)SQL并不等價(jià),代碼1在內(nèi)連接(inner join)中的連接條件(on)中加入非等值的過(guò)濾條件后,并沒(méi)有將內(nèi)連接的左右兩個(gè)表按照過(guò)濾條件進(jìn)行過(guò)濾,內(nèi)連接在執(zhí)行時(shí)會(huì)多讀取part=0的分區(qū)數(shù)據(jù)。而在代碼2中,會(huì)過(guò)濾掉不符合條件的分區(qū)。
案例二:識(shí)別SQL讀取數(shù)據(jù)范圍的差別
代碼1:
explain dependency
select
a.s_no
from student_orc_partition a
left join
student_orc_partition_only b
on a.s_no=b.s_no and a.part=b.part and b.part>=1 and b.part<=2;
代碼2:
explain dependency
select
a.s_no
from student_orc_partition a
left join
student_orc_partition_only b
on a.s_no=b.s_no and a.part=b.part and a.part>=1 and a.part<=2;
以上兩個(gè)代碼的數(shù)據(jù)讀取范圍是一樣的嗎?答案是不一樣,我們通過(guò)explain dependency來(lái)看下:
代碼1的explain dependency結(jié)果:
{"input_partitions":
[{"partitionName": "default@student_orc_partition@part=0"},
{"partitionName":"default@student_orc_partition@part=1"}, …中間省略7個(gè)分區(qū)
{"partitionName":"default@student_orc_partition@part=9"},
{"partitionName":"default@student_orc_partition_only@part=1"},
{"partitionName":"default@student_orc_partition_only@part=2"}],
"input_tables": [{"tablename":"default@student_orc_partition","tabletype":"MANAGED_TABLE"}, {"tablename":"default@student_orc_partition_only","tabletype":"MANAGED_TABLE"}]}
代碼2的explain dependency結(jié)果:
{"input_partitions":
[{"partitionName":"default@student_orc_partition@part=0"},
{"partitionName":"default@student_orc_partition@part=1"}, …中間省略7個(gè)分區(qū)
{"partitionName":"default@student_orc_partition@part=9"},
{"partitionName":"default@student_orc_partition_only@part=0"},
{"partitionName":"default@student_orc_partition_only@part=1"}, …中間省略7個(gè)分區(qū)
{"partitionName":"default@student_orc_partition_only@part=9"}],
"input_tables": [{"tablename":"default@student_orc_partition","tabletype":"MANAGED_TABLE"}, {"tablename":"default@student_orc_partition_only","tabletype":"MANAGED_TABLE"}]}
可以看到,對(duì)左外連接在連接條件中加入非等值過(guò)濾的條件,如果過(guò)濾條件是作用于右表(b表)有起到過(guò)濾的效果,則右表只要掃描兩個(gè)分區(qū)即可,但是左表(a表)會(huì)進(jìn)行全表掃描。如果過(guò)濾條件是針對(duì)左表,則完全沒(méi)有起到過(guò)濾的作用,那么兩個(gè)表將進(jìn)行全表掃描。這時(shí)的情況就如同全外連接一樣都需要對(duì)兩個(gè)數(shù)據(jù)進(jìn)行全表掃描。
在使用過(guò)程中,容易認(rèn)為代碼片段2可以像代碼片段1一樣進(jìn)行數(shù)據(jù)過(guò)濾,通過(guò)查看explain dependency的輸出結(jié)果,可以知道不是如此。
4. explain authorization 的用法
通過(guò)explain authorization可以知道當(dāng)前SQL訪問(wèn)的數(shù)據(jù)來(lái)源(INPUTS) 和數(shù)據(jù)輸出(OUTPUTS),以及當(dāng)前Hive的訪問(wèn)用戶 (CURRENT_USER)和操作(OPERATION)。
在 hive cli 中輸入以下命令:
explain authorization
select variance(s_score) from student_tb_orc;
結(jié)果如下:
INPUTS:
default@student_tb_orc
OUTPUTS:
hdfs://node01:8020/tmp/hive/hdfs/cbf182a5-8258-4157-9194- 90f1475a3ed5/-mr-10000
CURRENT_USER:
hdfs
OPERATION:
QUERY
AUTHORIZATION_FAILURES:
No privilege 'Select' found for inputs { database:default, table:student_ tb_orc, columnName:s_score}
從上面的信息可知:
上面案例的數(shù)據(jù)來(lái)源是defalut數(shù)據(jù)庫(kù)中的 student_tb_orc表;
數(shù)據(jù)的輸出路徑是hdfs://node01:8020/tmp/hive/hdfs/cbf182a5-8258-4157-9194-90f1475a3ed5/-mr-10000;
當(dāng)前的操作用戶是hdfs,操作是查詢;
觀察上面的信息我們還會(huì)看到AUTHORIZATION_FAILURES信息,提示對(duì)當(dāng)前的輸入沒(méi)有查詢權(quán)限,但如果運(yùn)行上面的SQL的話也能夠正常運(yùn)行。為什么會(huì)出現(xiàn)這種情況?Hive在默認(rèn)不配置權(quán)限管理的情況下不進(jìn)行權(quán)限驗(yàn)證,所有的用戶在Hive里面都是超級(jí)管理員,即使不對(duì)特定的用戶進(jìn)行賦權(quán),也能夠正常查詢。
最后
通過(guò)上面對(duì)explain的介紹,可以發(fā)現(xiàn)explain中有很多值得我們?nèi)パ芯康膬?nèi)容,讀懂 explain 的執(zhí)行計(jì)劃有利于我們優(yōu)化Hive SQL,同時(shí)也能提升我們對(duì)SQL的掌控力。

發(fā)表評(píng)論
請(qǐng)輸入評(píng)論內(nèi)容...
請(qǐng)輸入評(píng)論/評(píng)論長(zhǎng)度6~500個(gè)字
最新活動(dòng)更多
-
6月20日立即下載>> 【白皮書(shū)】精準(zhǔn)測(cè)量 安全高效——福祿克光伏行業(yè)解決方案
-
7月3日立即報(bào)名>> 【在線會(huì)議】英飛凌新一代智能照明方案賦能綠色建筑與工業(yè)互聯(lián)
-
7月22-29日立即報(bào)名>> 【線下論壇】第三屆安富利汽車生態(tài)圈峰會(huì)
-
7.30-8.1火熱報(bào)名中>> 全數(shù)會(huì)2025(第六屆)機(jī)器人及智能工廠展
-
7月31日免費(fèi)預(yù)約>> OFweek 2025具身機(jī)器人動(dòng)力電池技術(shù)應(yīng)用大會(huì)
-
免費(fèi)參會(huì)立即報(bào)名>> 7月30日- 8月1日 2025全數(shù)會(huì)工業(yè)芯片與傳感儀表展
推薦專題
- 1 AI 眼鏡讓百萬(wàn) APP「集體失業(yè)」?
- 2 大廠紛紛入局,百度、阿里、字節(jié)搶奪Agent話語(yǔ)權(quán)
- 3 深度報(bào)告|中國(guó)AI產(chǎn)業(yè)正在崛起成全球力量,市場(chǎng)潛力和關(guān)鍵挑戰(zhàn)有哪些?
- 4 上海跑出80億超級(jí)獨(dú)角獸:獲上市公司戰(zhàn)投,干人形機(jī)器人
- 5 一文看懂視覺(jué)語(yǔ)言動(dòng)作模型(VLA)及其應(yīng)用
- 6 國(guó)家數(shù)據(jù)局局長(zhǎng)劉烈宏調(diào)研格創(chuàng)東智
- 7 下一代入口之戰(zhàn):大廠為何紛紛押注智能體?
- 8 百億AI芯片訂單,瘋狂傾銷中東?
- 9 Robotaxi新消息密集釋放,量產(chǎn)元年誰(shuí)在領(lǐng)跑?
- 10 格斗大賽出圈!人形機(jī)器人致命短板曝光:頭腦過(guò)于簡(jiǎn)單