不懂hive中的explain,說明hive還沒入門,學會explain,可以給咱們工做中使用hive帶來極大的便利!sql
本節將介紹 explain 的用法及參數介紹
HIVE提供了EXPLAIN命令來展現一個查詢的執行計劃,這個執行計劃對於咱們瞭解底層原理,hive 調優,排查數據傾斜等頗有幫助 express
使用語法以下:apache
EXPLAIN [EXTENDED|CBO|AST|DEPENDENCY|AUTHORIZATION|LOCKS|VECTORIZATION|ANALYZE] query
explain 後面能夠跟如下可選參數,注意:這幾個可選參數不是 hive 每一個版本都支持的函數
在 hive cli 中輸入如下命令(hive 2.3.7):oop
explain select sum(id) from test1;
獲得結果(請逐行看完,即便看不懂也要每行都看):性能
STAGE DEPENDENCIES: Stage-1 is a root stage Stage-0 depends on stages: Stage-1 STAGE PLANS: Stage: Stage-1 Map Reduce Map Operator Tree: TableScan alias: test1 Statistics: Num rows: 6 Data size: 75 Basic stats: COMPLETE Column stats: NONE Select Operator expressions: id (type: int) outputColumnNames: id Statistics: Num rows: 6 Data size: 75 Basic stats: COMPLETE Column stats: NONE Group By Operator aggregations: sum(id) mode: hash outputColumnNames: _col0 Statistics: Num rows: 1 Data size: 8 Basic stats: COMPLETE Column stats: NONE Reduce Output Operator sort order: Statistics: Num rows: 1 Data size: 8 Basic stats: COMPLETE Column stats: NONE value expressions: _col0 (type: bigint) Reduce Operator Tree: Group By Operator aggregations: sum(VALUE._col0) mode: mergepartial outputColumnNames: _col0 Statistics: Num rows: 1 Data size: 8 Basic stats: COMPLETE Column stats: NONE File Output Operator compressed: false Statistics: Num rows: 1 Data size: 8 Basic stats: COMPLETE Column stats: NONE table: input format: org.apache.hadoop.mapred.SequenceFileInputFormat output format: org.apache.hadoop.hive.ql.io.HiveSequenceFileOutputFormat serde: org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe Stage: Stage-0 Fetch Operator limit: -1 Processor Tree: ListSink
看完以上內容有什麼感覺,是否是感受都看不懂,不要着急,下面將會詳細講解每一個參數,相信你學完下面的內容以後再看 explain 的查詢結果將遊刃有餘。優化
一個HIVE查詢被轉換爲一個由一個或多個stage組成的序列(有向無環圖DAG)。這些stage能夠是MapReduce stage,也能夠是負責元數據存儲的stage,也能夠是負責文件系統的操做(好比移動和重命名)的stage。
咱們將上述結果拆分看,先從最外層開始,包含兩個大的部分:code
先看第一部分 stage dependencies ,包含兩個 stage,Stage-1 是根stage,說明這是開始的stage,Stage-0 依賴 Stage-1,Stage-1執行完成後執行Stage-0。orm
再看第二部分 stage plan,裏面有一個 Map Reduce,一個MR的執行計劃分爲兩個部分:排序
這兩個執行計劃樹裏面包含這條sql語句的 operator:
map端第一個操做確定是加載表,因此就是 TableScan 表掃描操做,常見的屬性:
Select Operator: 選取操做,常見的屬性 :
Group By Operator:分組聚合操做,常見的屬性:
Reduce Output Operator:輸出到reduce操做,常見屬性:
Filter Operator:過濾操做,常見的屬性:
Map Join Operator:join 操做,常見的屬性:
File Output Operator:文件輸出操做,常見的屬性
Fetch Operator 客戶端獲取數據操做,常見的屬性:
好,學到這裏再翻到上面 explain 的查詢結果,是否是感受基本都能看懂了。
本節介紹 explain 可以爲咱們在生產實踐中帶來哪些便利及解決咱們哪些迷惑
如今,咱們在hive cli 輸入如下查詢計劃語句
select a.id,b.user_name from test1 a join test2 b on a.id=b.id;
問:上面這條 join 語句會過濾 id 爲 null 的值嗎
執行下面語句:
explain select a.id,b.user_name from test1 a join test2 b on a.id=b.id;
咱們來看結果 (爲了適應頁面展現,僅截取了部分輸出信息):
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) ...
從上述結果能夠看到 predicate: id is not null 這樣一行,**說明 join 時會自動過濾掉關聯字段爲 null
值的狀況,但 left join 或 full join 是不會自動過濾的**,你們能夠自行嘗試下。
看下面這條sql
select id,max(user_name) from test1 group by id;
問:group by 分組語句會進行排序嗎
直接來看 explain 以後結果 (爲了適應頁面展現,僅截取了部分輸出信息)
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) 說明按照 id 進行分組的,再往下看還有 sort order: + ,說明是按照 id 字段進行正序排序的。
觀察兩條sql語句
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語句輸出的結果是同樣的,可是哪條sql執行效率高呢
有人說第一條sql執行效率高,由於第二條sql有子查詢,子查詢會影響性能
有人說第二條sql執行效率高,由於先過濾以後,在進行join時的條數減小了,因此執行效率就高了
到底哪條sql效率高呢,咱們直接在sql語句前面加上 explain,看下執行計劃不就知道了嘛
在第一條sql語句前加上 explain,獲得以下結果
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.apache.hadoop.mapred.SequenceFileInputFormat output format: org.apache.hadoop.hive.ql.io.HiveSequenceFileOutputFormat serde: org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe Local Work: Map Reduce Local Work Stage: Stage-0 Fetch Operator limit: -1 Processor Tree: ListSink
在第二條sql語句前加上 explain,獲得以下結果
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.apache.hadoop.mapred.SequenceFileInputFormat output format: org.apache.hadoop.hive.ql.io.HiveSequenceFileOutputFormat serde: org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe Local Work: Map Reduce Local Work Stage: Stage-0 Fetch Operator limit: -1 Processor Tree: ListSink
你們有什麼發現,除了表別名不同,其餘的執行計劃徹底同樣,都是先進行 where 條件過濾,在進行 join 條件關聯。說明 hive 底層會自動幫咱們進行優化,因此這兩條sql語句執行效率是同樣的。
以上僅列舉了3個咱們生產中既熟悉又有點迷糊的例子,explain 還有不少其餘的用途,如查看stage的依賴狀況、排查數據傾斜、hive 調優等,小夥伴們能夠自行嘗試。