万字长⽂详解HiveSQL执⾏计划
Hive SQL的执⾏计划描述SQL实际执⾏的整体轮廓,通过执⾏计划能了解SQL程序在转换成相应计算引擎的执⾏逻辑,掌握了执⾏逻辑也就能更好地把握程序出现的瓶颈点,从⽽能够实现更有针对性的优化。此外还能帮助开发者识别看似等价的SQL其实是不等价的,看似不等价的SQL其实是等价的SQL。可以说执⾏计划是打开SQL优化⼤门的⼀把钥匙。
要想学SQL执⾏计划,就需要学习查看执⾏计划的命令:explain,在查询语句的SQL前⾯加上关键字explain是查看执⾏计划的基本⽅法。
本⽂⾸发于【五分钟学⼤数据】
学会explain,能够给我们⼯作中使⽤hive带来极⼤的便利!
查看SQL的执⾏计划
Hive提供的执⾏计划⽬前可以查看的信息有以下⼏种:
explain:查看执⾏计划的基本信息;
explain dependency:dependency在explain语句中使⽤会产⽣有关计划中输⼊的额外信息。它显⽰了输⼊的各种属性;
explain authorization:查看SQL操作相关权限的信息;
explain vectorization:查看SQL的向量化描述信息,显⽰为什么未对Map和Reduce进⾏⽮量化。从 Hive 2.3.0 开始⽀持;
explain analyze:⽤实际的⾏数注释计划。从 Hive 2.2.0 开始⽀持;
explain cbo:输出由Calcite优化器⽣成的计划。CBO 从 Hive 4.0.0 版本开始⽀持;
explain locks:这对于了解系统将获得哪些锁以运⾏指定的查询很有⽤。LOCKS 从 Hive 3.2.0 开始⽀持;
explain ast:输出查询的抽象语法树。AST 在 Hive 2.1.0 版本删除了,存在bug,转储AST可能会导致OOM错误,将在4.0.0版本修复;
explain extended:加上 extended 可以输出有关计划的额外信息。这通常是物理信息,例如⽂件名,这些额外信息对我们⽤处不⼤;
1. explain 的⽤法
Hive提供了explain命令来展⽰⼀个查询的执⾏计划,这个执⾏计划对于我们了解底层原理,Hive 调优,排查数据倾斜等很有帮助。
使⽤语法如下:
explain query;
在 hive cli 中输⼊以下命令(hive 2.3.7):
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。
我们将上述结果拆分看,先从最外层开始,包含两个⼤的部分:
1. stage dependencies:各个stage之间的依赖性
2. stage plan:各个stage的执⾏计划
先看第⼀部分 stage dependencies ,包含两个 stage,Stage-1 是根stage,说明这是开始的stage,Stage-0 依赖 Stage-1,Stage-1执⾏完成后执⾏Stage-0。
再看第⼆部分 stage plan,⾥⾯有⼀个 Map Reduce,⼀个MR的执⾏计划分为两个部分:
1. Map Operator Tree: MAP端的执⾏计划树
2. Reduce Operator Tree: Reduce端的执⾏计划树
这两个执⾏计划树⾥⾯包含这条sql语句的 operator:
1. TableScan:表扫描操作,map端第⼀个操作肯定是加载表,所以就是表扫描操作,常见的属性:
alias:表名称
Statistics:表统计信息,包含表中数据条数,数据⼤⼩等
2. Select Operator:选取操作,常见的属性:
expressions:需要的字段名称及字段类型
outputColumnNames:输出的列名称
Statistics:表统计信息,包含表中数据条数,数据⼤⼩等
3. Group By Operator:分组聚合操作,常见的属性:
aggregations:显⽰聚合函数信息
mode:聚合模式,值有 hash:随机聚合,就是hash partition;partial:局部聚合;final:最终聚合
keys:分组的字段,如果没有分组,则没有此字段
outputColumnNames:聚合之后输出列名
Statistics:表统计信息,包含分组聚合之后的数据条数,数据⼤⼩等
4. Reduce Output Operator:输出到reduce操作,常见属性:
sort order:值为空不排序;值为 + 正序排序,值为 - 倒序排序;值为 +- 排序的列为两列,第⼀列为正序,第⼆列为倒序
5. Filter Operator:过滤操作,常见的属性:
predicate:过滤条件,如sql语句中的where id>=1,则此处显⽰(id >= 1)
6. Map Join Operator:join 操作,常见的属性:
condition map:join⽅式,如Inner Join 0 to 1 Left Outer Join0 to 2
keys: join 的条件字段
outputColumnNames: join 完成之后输出的字段
Statistics: join 完成之后⽣成的数据条数,⼤⼩等
7. File Output Operator:⽂件输出操作,常见的属性
compressed:是否压缩
table:表的信息,包含输⼊输出⽂件格式化⽅式,序列化⽅式等
8. Fetch Operator 客户端获取数据操作,常见的属性:
limit,值为 -1 表⽰不限制条数,其他值为限制的条数
2. explain 的使⽤场景
本节介绍 explain 能够为我们在⽣产实践中带来哪些便利及解决我们哪些迷惑
案例⼀:join 语句会过滤 null 的值吗?
现在,我们在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
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 是不会⾃动过滤null值的,⼤家可以⾃⾏尝试下。
案例⼆:group by 分组语句会进⾏排序吗?
看下⾯这条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执⾏效率⾼呢?
观察两条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: 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
group by的用法及原理详解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 调优等,⼩伙伴们可以⾃⾏尝试。
2. explain dependency的⽤法
explain dependency⽤于描述⼀段SQL需要的数据来源,输出是⼀个json格式的数据,⾥⾯包含以下两个部分的内容:input_partitions:描述⼀段SQL依赖的数据来源表分区,⾥⾯存储的是分区名的列表,如果整段SQL包含的所有表都是⾮分区表,则显⽰为空。
input_tables:描述⼀段SQL依赖的数据来源表,⾥⾯存储的是Hive表名的列表。
使⽤explain dependency查看SQL查询⾮分区普通表,在 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查询分区表,在 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"}]
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论