學(xué)會(huì)hive中的explain 能為我們?cè)谏a(chǎn)實(shí)踐中帶來(lái)哪些便利?
這兩個(gè)執(zhí)行計(jì)劃樹(shù)里面包含這條sql語(yǔ)句的 operator:
map端第一個(gè)操作肯定是加載表,所以就是 TableScan 表掃描操作,常見(jiàn)的屬性:
alias: 表名稱
Statistics: 表統(tǒng)計(jì)信息,包含表中數(shù)據(jù)條數(shù),數(shù)據(jù)大小等
Select Operator: 選取操作,常見(jiàn)的屬性 :
expressions:需要的字段名稱及字段類型
outputColumnNames:輸出的列名稱
Statistics:表統(tǒng)計(jì)信息,包含表中數(shù)據(jù)條數(shù),數(shù)據(jù)大小等
Group By Operator:分組聚合操作,常見(jiàn)的屬性:
aggregations:顯示聚合函數(shù)信息
mode:聚合模式,值有 hash:隨機(jī)聚合,就是hash partition;partial:局部聚合;final:最終聚合
keys:分組的字段,如果沒(méi)有分組,則沒(méi)有此字段
outputColumnNames:聚合之后輸出列名
Statistics: 表統(tǒng)計(jì)信息,包含分組聚合之后的數(shù)據(jù)條數(shù),數(shù)據(jù)大小等
Reduce Output Operator:輸出到reduce操作,常見(jiàn)屬性:
sort order:值為空 不排序;值為 + 正序排序,值為 - 倒序排序;值為 +- 排序的列為兩列,第一列為正序,第二列為倒序
Filter Operator:過(guò)濾操作,常見(jiàn)的屬性:
predicate:過(guò)濾條件,如sql語(yǔ)句中的where id>=1,則此處顯示(id >= 1)
Map Join Operator:join 操作,常見(jiàn)的屬性:
condition map:join方式 ,如Inner Join 0 to 1 Left Outer Join0 to 2
keys: join 的條件字段
outputColumnNames: join 完成之后輸出的字段
Statistics: join 完成之后生成的數(shù)據(jù)條數(shù),大小等
File Output Operator:文件輸出操作,常見(jiàn)的屬性
compressed:是否壓縮
table:表的信息,包含輸入輸出文件格式化方式,序列化方式等
Fetch Operator 客戶端獲取數(shù)據(jù)操作,常見(jiàn)的屬性:
limit,值為 -1 表示不限制條數(shù),其他值為限制的條數(shù)
好,學(xué)到這里再翻到上面 explain 的查詢結(jié)果,是不是感覺(jué)基本都能看懂了。
實(shí)踐
本節(jié)介紹 explain 能夠?yàn)槲覀冊(cè)谏a(chǎn)實(shí)踐中帶來(lái)哪些便利及解決我們哪些迷惑
1. join 語(yǔ)句會(huì)過(guò)濾 null 的值嗎?
現(xiàn)在,我們?cè)趆ive cli 輸入以下查詢計(jì)劃語(yǔ)句
select a.id,b.user_name from test1 a join test2 b on a.id=b.id;
問(wèn):上面這條 join 語(yǔ)句會(huì)過(guò)濾 id 為 null 的值嗎
執(zhí)行下面語(yǔ)句:
explain select a.id,b.user_name from test1 a join test2 b on a.id=b.id;
我們來(lái)看結(jié)果 (為了適應(yīng)頁(yè)面展示,僅截取了部分輸出信息):
TableScan
alias: a
Statistics: Num rows: 6 Data size: 75 Basic stats: COMPLETE Column stats: NONE
Filter Operator
predicate: id is not null (type: boolean)
Statistics: Num rows: 6 Data size: 75 Basic stats: COMPLETE Column stats: NONE
Select Operator
expressions: id (type: int)
outputColumnNames: _col0
Statistics: Num rows: 6 Data size: 75 Basic stats: COMPLETE Column stats: NONE
HashTable Sink Operator
keys:
0 _col0 (type: int)
1 _col0 (type: int)
...
從上述結(jié)果可以看到 predicate: id is not null 這樣一行,說(shuō)明 join 時(shí)會(huì)自動(dòng)過(guò)濾掉關(guān)聯(lián)字段為 null值的情況,但 left join 或 full join 是不會(huì)自動(dòng)過(guò)濾的,大家可以自行嘗試下。
2. group by 分組語(yǔ)句會(huì)進(jìn)行排序嗎?
看下面這條sql
select id,max(user_name) from test1 group by id;
問(wèn):group by 分組語(yǔ)句會(huì)進(jìn)行排序嗎
直接來(lái)看 explain 之后結(jié)果 (為了適應(yīng)頁(yè)面展示,僅截取了部分輸出信息)
TableScan
alias: test1
Statistics: Num rows: 9 Data size: 108 Basic stats: COMPLETE Column stats: NONE
Select Operator
expressions: id (type: int), user_name (type: string)
outputColumnNames: id, user_name
Statistics: Num rows: 9 Data size: 108 Basic stats: COMPLETE Column stats: NONE
Group By Operator
aggregations: max(user_name)
keys: id (type: int)
mode: hash
outputColumnNames: _col0, _col1
Statistics: Num rows: 9 Data size: 108 Basic stats: COMPLETE Column stats: NONE
Reduce Output Operator
key expressions: _col0 (type: int)
sort order: +
Map-reduce partition columns: _col0 (type: int)
Statistics: Num rows: 9 Data size: 108 Basic stats: COMPLETE Column stats: NONE
value expressions: _col1 (type: string)
...
我們看 Group By Operator,里面有 keys: id (type: int) 說(shuō)明按照 id 進(jìn)行分組的,再往下看還有 sort order: + ,說(shuō)明是按照 id 字段進(jìn)行正序排序的。
3. 哪條sql執(zhí)行效率高呢?
觀察兩條sql語(yǔ)句
SELECT
a.id,
b.user_name
FROM
test1 a
JOIN test2 b ON a.id = b.id
WHERE
a.id > 2;
SELECT
a.id,
b.user_name
FROM
(SELECT * FROM test1 WHERE id > 2) a
JOIN test2 b ON a.id = b.id;
這兩條sql語(yǔ)句輸出的結(jié)果是一樣的,但是哪條sql執(zhí)行效率高呢
有人說(shuō)第一條sql執(zhí)行效率高,因?yàn)榈诙䲢lsql有子查詢,子查詢會(huì)影響性能
有人說(shuō)第二條sql執(zhí)行效率高,因?yàn)橄冗^(guò)濾之后,在進(jìn)行join時(shí)的條數(shù)減少了,所以執(zhí)行效率就高了
到底哪條sql效率高呢,我們直接在sql語(yǔ)句前面加上 explain,看下執(zhí)行計(jì)劃不就知道了嘛
在第一條sql語(yǔ)句前加上 explain,得到如下結(jié)果
hive (default)> explain select a.id,b.user_name from test1 a join test2 b on a.id=b.id where a.id >2;
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:a
Fetch Operator
limit: -1
Alias -> Map Local Operator Tree:
$hdt$_0:a
TableScan
alias: a
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
在第二條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)等,小伙伴們可以自行嘗試。

發(fā)表評(píng)論
請(qǐng)輸入評(píng)論內(nèi)容...
請(qǐng)輸入評(píng)論/評(píng)論長(zhǎng)度6~500個(gè)字
圖片新聞
-
機(jī)器人奧運(yùn)會(huì)戰(zhàn)報(bào):宇樹(shù)機(jī)器人摘下首金,天工Ultra搶走首位“百米飛人”
-
存儲(chǔ)圈掐架!江波龍起訴佰維,索賠121萬(wàn)
-
長(zhǎng)安汽車母公司突然更名:從“中國(guó)長(zhǎng)安”到“辰致科技”
-
豆包前負(fù)責(zé)人喬木出軌BP后續(xù):均被辭退
-
字節(jié)AI Lab負(fù)責(zé)人李航卸任后返聘,Seed進(jìn)入調(diào)整期
-
員工持股爆雷?廣汽埃安緊急回應(yīng)
-
中國(guó)“智造”背后的「關(guān)鍵力量」
-
小米汽車研發(fā)中心重磅落地,寶馬家門(mén)口“搶人”
最新活動(dòng)更多
-
即日-9.1立即下載>> 【限時(shí)下載】ADI中國(guó)三十周年感恩回饋助力企業(yè)升級(jí)!
-
即日-9.16點(diǎn)擊進(jìn)入 >> 【限時(shí)福利】TE 2025國(guó)際物聯(lián)網(wǎng)展·深圳站
-
10月23日立即報(bào)名>> Works With 開(kāi)發(fā)者大會(huì)深圳站
-
10月24日立即參評(píng)>> 【評(píng)選】維科杯·OFweek 2025(第十屆)物聯(lián)網(wǎng)行業(yè)年度評(píng)選
-
11月27日立即報(bào)名>> 【工程師系列】汽車電子技術(shù)在線大會(huì)
-
12月18日立即報(bào)名>> 【線下會(huì)議】OFweek 2025(第十屆)物聯(lián)網(wǎng)產(chǎn)業(yè)大會(huì)
推薦專題
- 1 阿里首位程序員,“掃地僧”多隆已離職
- 2 先進(jìn)算力新選擇 | 2025華為算力場(chǎng)景發(fā)布會(huì)暨北京xPN伙伴大會(huì)成功舉辦
- 3 宇樹(shù)機(jī)器人撞人事件的深度剖析:六維力傳感器如何成為人機(jī)安全的關(guān)鍵屏障
- 4 清華跑出具身智能獨(dú)角獸:給機(jī)器人安上眼睛和大腦,融資近20億
- 5 特朗普要求英特爾首位華人 CEO 辭職
- 6 踢館大廠和微軟,剖析WPS靈犀的AI實(shí)用主義
- 7 騰訊 Q2 財(cái)報(bào)亮眼:AI 已成第二增長(zhǎng)曲線
- 8 谷歌吹響AI沖鋒號(hào),AI還有哪些機(jī)會(huì)
- 9 蘋(píng)果把身家押在Siri上:一場(chǎng)輸不起的自我革命
- 10 騰訊米哈游押寶的中國(guó)AI應(yīng)用,正在海外悶聲發(fā)財(cái)