訂閱
糾錯
加入自媒體

一文詳解HiveSQL執(zhí)行計劃

2021-06-21 10:43
園陌
關注

在第二條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

大家有什么發(fā)現,除了表別名不一樣,其他的執(zhí)行計劃完全一樣,都是先進行 where 條件過濾,在進行 join 條件關聯。說明 hive 底層會自動幫我們進行優(yōu)化,所以這兩條sql語句執(zhí)行效率是一樣的。

以上僅列舉了3個我們生產中既熟悉又有點迷糊的例子,explain 還有很多其他的用途,如查看stage的依賴情況、排查數據傾斜、hive 調優(yōu)等,小伙伴們可以自行嘗試。

3. explain dependency的用法

explain dependency用于描述一段SQL需要的數據來源,輸出是一個json格式的數據,里面包含以下兩個部分的內容:

input_partitions:描述一段SQL依賴的數據來源表分區(qū),里面存儲的是分區(qū)名的列表,如果整段SQL包含的所有表都是非分區(qū)表,則顯示為空。

input_tables:描述一段SQL依賴的數據來源表,里面存儲的是Hive表名的列表。

使用explain dependency查看SQL查詢非分區(qū)普通表,在 hive cli 中輸入以下命令:

explain dependency select s_age,count(1) num from student_orc;

得到結果:

{"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;

得到結果:

{"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的使用場景有兩個:

場景一:快速排除?焖倥懦驗樽x取不到相應分區(qū)的數據而導致任務數據輸出異常。例如,在一個以天分區(qū)的任務中,上游任務因為生產過程不可控因素出現異;蛘呖张,導致下游任務引發(fā)異常。通過這種方式,可以快速查看SQL讀取的分區(qū)是否出現異常。

場景二:理清表的輸入,幫助理解程序的運行,特別是有助于理解有多重子查詢,多表連接的依賴輸入。

下面通過兩個案例來看explain dependency的實際運用:

案例一:識別看似等價的代碼

對于剛接觸SQL的程序員,很容易將

select * from a inner join b on a.no=b.no and a.f>1 and a.f<3;

等價于

select * from a inner join b on a.no=b.no where a.f>1 and a.f<3;

我們可以通過案例來查看下它們的區(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的輸出結果:

代碼1的explain dependency結果:

{"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結果:

{"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"}]}

通過上面的輸出結果可以看到,其實上述的兩個SQL并不等價,代碼1在內連接(inner join)中的連接條件(on)中加入非等值的過濾條件后,并沒有將內連接的左右兩個表按照過濾條件進行過濾,內連接在執(zhí)行時會多讀取part=0的分區(qū)數據。而在代碼2中,會過濾掉不符合條件的分區(qū)。

案例二:識別SQL讀取數據范圍的差別

代碼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;

以上兩個代碼的數據讀取范圍是一樣的嗎?答案是不一樣,我們通過explain dependency來看下:

代碼1的explain dependency結果:

{"input_partitions":
[{"partitionName": "default@student_orc_partition@part=0"},
{"partitionName":"default@student_orc_partition@part=1"}, …中間省略7個分區(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結果:

{"input_partitions":
[{"partitionName":"default@student_orc_partition@part=0"},
{"partitionName":"default@student_orc_partition@part=1"}, …中間省略7個分區(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個分區(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"}]}

可以看到,對左外連接在連接條件中加入非等值過濾的條件,如果過濾條件是作用于右表(b表)有起到過濾的效果,則右表只要掃描兩個分區(qū)即可,但是左表(a表)會進行全表掃描。如果過濾條件是針對左表,則完全沒有起到過濾的作用,那么兩個表將進行全表掃描。這時的情況就如同全外連接一樣都需要對兩個數據進行全表掃描。

在使用過程中,容易認為代碼片段2可以像代碼片段1一樣進行數據過濾,通過查看explain dependency的輸出結果,可以知道不是如此。

4. explain authorization 的用法

通過explain authorization可以知道當前SQL訪問的數據來源(INPUTS) 和數據輸出(OUTPUTS),以及當前Hive的訪問用戶 (CURRENT_USER)和操作(OPERATION)。

在 hive cli 中輸入以下命令:

explain authorization
select variance(s_score) from student_tb_orc;

結果如下:

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}

從上面的信息可知:

上面案例的數據來源是defalut數據庫中的 student_tb_orc表;

數據的輸出路徑是hdfs://node01:8020/tmp/hive/hdfs/cbf182a5-8258-4157-9194-90f1475a3ed5/-mr-10000;

當前的操作用戶是hdfs,操作是查詢;

觀察上面的信息我們還會看到AUTHORIZATION_FAILURES信息,提示對當前的輸入沒有查詢權限,但如果運行上面的SQL的話也能夠正常運行。為什么會出現這種情況?Hive在默認不配置權限管理的情況下不進行權限驗證,所有的用戶在Hive里面都是超級管理員,即使不對特定的用戶進行賦權,也能夠正常查詢。

最后

通過上面對explain的介紹,可以發(fā)現explain中有很多值得我們去研究的內容,讀懂 explain 的執(zhí)行計劃有利于我們優(yōu)化Hive SQL,同時也能提升我們對SQL的掌控力。


<上一頁  1  2  3  
聲明: 本文由入駐維科號的作者撰寫,觀點僅代表作者本人,不代表OFweek立場。如有侵權或其他問題,請聯系舉報。

發(fā)表評論

0條評論,0人參與

請輸入評論內容...

請輸入評論/評論長度6~500個字

您提交的評論過于頻繁,請輸入驗證碼繼續(xù)

暫無評論

暫無評論

    掃碼關注公眾號
    OFweek人工智能網
    獲取更多精彩內容
    文章糾錯
    x
    *文字標題:
    *糾錯內容:
    聯系郵箱:
    *驗 證 碼:

    粵公網安備 44030502002758號