您现在的位置是:热点 >>正文
Hive底层原理:Explain执行计划详解
热点96118人已围观
简介...
理论
本节将介绍 explain 的用法及参数介绍
HIVE提供了EXPLAIN命令来展示一个查询的执行计划,这个执行计划对于我们了解底层原理,hive 调优,排查数据倾斜等很有帮助
使用语法如下:
EXPLAIN [EXTENDED|CBO|AST|DEPENDENCY|AUTHORIZATION|LOCKS|VECTORIZATION|ANALYZE] query
explain 后面可以跟以下可选参数,注意:这几个可选参数不是 hive 每个版本都支持的
EXTENDED:加上 extended 可以输出有关计划的额外信息。这通常是物理信息,例如文件名。这些额外信息对我们用处不大 CBO:输出由Calcite优化器生成的计划。CBO 从 hive 4.0.0 版本开始支持 AST:输出查询的抽象语法树。AST 在hive 2.1.0 版本删除了,存在bug,转储AST可能会导致OOM错误,将在4.0.0版本修复 DEPENDENCY:dependency在EXPLAIN语句中使用会产生有关计划中输入的额外信息。它显示了输入的各种属性 AUTHORIZATION:显示所有的实体需要被授权执行(如果存在)的查询和授权失败 LOCKS:这对于了解系统将获得哪些锁以运行指定的查询很有用。LOCKS 从 hive 3.2.0 开始支持 VECTORIZATION:将详细信息添加到EXPLAIN输出中,以显示为什么未对Map和Reduce进行矢量化。从 Hive 2.3.0 开始支持 ANALYZE:用实际的行数注释计划。从 Hive 2.2.0 开始支持
在 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:&Hinbsp;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 &nbvesp; 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。
我们将上述结果拆分看,先从最外层开始,包含两个大的部分:
stage dependencies: 各个stage之间的依赖性 stage plan: 各个stage的执行计划
先看第一部分 stage dependencies ,包含两个 stage,Stage-1 是根stage,说明这是开始的stage,Stage-0 依赖 Stage-1,Stage-1执行完成后执行Stage-0。
再看第二部分 stage plan,里面有一个 Map Reduce,一个MR的执行计划分为两个部分:
Map Operator Tree: MAP端的执行计划树 Reduce Operator Tree: Reduce端的执行计划树
这两个执行计划树里面包含这条sql语句的 operator:
1.map端第一个操作肯定是加载表,所以就是 TableScan 表扫描操作底层,常见的属性:
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 表示不限制条数,其他值为限制的条数
好,学到这里再翻到上面 explain 的查询结果,是不是感觉基本都能看懂了。
实践
本节介绍 explain 能够为我们在生产实践中带来哪些便利及解决我们哪些迷惑
1. 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 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 &n原理bsp; 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 是不会自动过滤的,大家可以自行尝试下。
2. 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 :E 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 字段进行正序排序的。
3. 哪条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 xpjoin 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 la 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: 25in 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: o计划rg.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 &nb详解sp;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 &nbHisp;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: ve 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 &n底层bsp; 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 调优等,小伙伴们可以自行尝试。
Tags:
相关文章
标签: 机器人
热点撰文 | 雁 秋 编辑 | 李信马 题图 | IC photo “智元率先报价6000万,宇树直接将价格抬至1亿元。” 近期,网上流传的这组2026年春晚赞助数额激起一阵波澜。尽管智元方面回应“不是真...
【热点】
阅读更多标签: 智元机器人 临界点 熊坤 灵巧手
热点云梦级1月15日云梦级消息,AI科技评论近日从多个信源处获悉,智元机器人已经悄然完成对灵巧手业务的拆分独立工作,成立了名为“临界点”的新公司,并于近期完成首轮融资。据悉,新公司临界点将由智元机器人灵巧...
【热点】
阅读更多阿里云与奇安信达成战略合作
热点3月4日,阿里云与奇安信(688561.SH)宣布达成战略合作,双方将围绕重点行业的云安全领域进行自主研发,同时将在工业物联网、应急与攻防演练等场景,展开技术、产品、市场、资本等领域全面深入合作。 根...
【热点】
阅读更多
热门文章
最新文章
友情链接
- 标签: 康师傅
- https://www.merchbar.com/search?q=谷歌快排【tg:seogoogle987】,iDE
- https://www.yidian51.com/taobao/谷歌快排【tg:seogoogle987】,nCS
- https://www.wheels24.gr/?action=CHECK&s=谷歌快排【tg:seogoogle987】,s2w
- https://www.stonerealestate.com.au/?cat=100&s=谷歌快排【tg:seogoogle987】,e6p
- https://search.danawa.com/dsearch.php?query=谷歌快排【tg:seogoogle987】Zyl
- https://www.kores.com/?s=谷歌快排【tg:seogoogle987】,l6E
- https://www.abbreviations.com/谷歌快排【tg:seogoogle987】H4O
- https://www.tarleton.edu/news/search/谷歌快排【tg:seogoogle987】,b5B
- https://www.kaiyueleather.com/search/谷歌快排【tg:seogoogle987】,gMQ
- https://www.valuecolleges.com/?action=QUERY&s=谷歌快排【tg:seogoogle987】,0EV
- https://www.unclefossil.com/search/谷歌快排【tg:seogoogle987】,HxU
- https://www.kawainter.com/catalogsearch/result/?q=谷歌快排【tg:seogoogle987】,1xB
- https://www.sic-info.org/?s=谷歌快排【tg:seogoogle987】,ner
- https://www.nctsn.org/search?cat=191&query=谷歌快排【tg:seogoogle987】,1nW
- https://www.usslexington.com/?action=QUERY&s=谷歌快排【tg:seogoogle987】,dgE
- https://www.universal-music.co.jp/search/?q=谷歌快排【tg:seogoogle987】,EIc
- https://www.varian.com/en-au/search?cat=113&search=谷歌快排【tg:seogoogle987】,z34
- https://www.lee.com/search?q=谷歌快排【tg:seogoogle987】,JUZ
- https://www.reallywildbirdfood.co.uk/store/search?q=谷歌快排【tg:seogoogle987】,IkQ
- https://www.kuaikanmanhua.com/sou/谷歌快排【tg:seogoogle987】,yrV
- https://www.repetto.jp/item/search?action=QUERY&q=谷歌快排【tg:seogoogle987】,DX6
- https://zhidao.baidu.com/index/?action=QUERY&word=谷歌快排【tg:seogoogle987】,LYu
- https://www.localfootdoc.com/?s=谷歌快排【tg:seogoogle987】,u09
- https://www.teamlab.art/?submit=谷歌快排【tg:seogoogle987】,3oW
- https://www.lssystems.co.uk/search?page=0&per_page=24&term=谷歌快排【tg:seogoogle987】,xEN
- https://www.lechzuers.com/de/search?cat=163&q=谷歌快排【tg:seogoogle987】,c0H
- https://www.wikiart.org/pt/Search/谷歌快排【tg:seogoogle987】,ZFe
- https://www.wellbees.com/catalogsearch/result/?action=CHECK&q=谷歌快排【tg:seogoogle987】,INs
- https://www.ng-slo.si/si/iskanje?cat=114&q=谷歌快排【tg:seogoogle987】,tFU
- https://www.openstreetmap.org/search?query=谷歌快排【tg:seogoogle987】,U6X
- https://jihoon471.cafe24.com/product/search.html?banner_action=&keyword=谷歌快排【tg:seogoogle987】O3v
- https://www.writingforward.com/?action=SEARCH&s=谷歌快排【tg:seogoogle987】,WcQ
- https://www.paxnet.co.kr/search/jongmok?kwd=谷歌快排【tg:seogoogle987】uDC
- https://www.abbreviations.com/谷歌快排【tg:seogoogle987】CAM
- https://www.flickr.com/photos/tags/谷歌快排【tg:seogoogle987】5TQ
- https://www.voices.com/talents/search?keywords=谷歌快排【tg:seogoogle987】,kzj
- https://www.safetv.go.kr/base/search/view?searchWord=谷歌快排【tg:seogoogle987】sqa
- https://www.safetv.go.kr/base/search/view?searchWord=谷歌快排【tg:seogoogle987】25x
- https://www.theaerogroup.net/?cat=28&s=谷歌快排【tg:seogoogle987】,NoW
- https://www.kaliastyle.com/catalogsearch/result/?q=GOOGLE谷歌快排【tg:seogoogle987】,4KY
- https://www.klatreverden.no/search?q=谷歌快排【tg:seogoogle987】,s7Q
- https://www.welshmountainzoo.org/shop/index.php?routeproduct/search&tag=谷歌快排【tg:seogoogle987】,8hd
- https://www.symatoys.com/search.html?ID=UyMD6aO58bPrJvQAB1I0N69KzJBZeNPC&keyword=谷歌快排【tg:seogoogle987】,B8s
- https://www.laoairlines.com/?action=QUERY&s=谷歌快排【tg:seogoogle987】,Uai
- https://www.nslcleaders.org/?s=谷歌快排【tg:seogoogle987】,Cda
- https://www.remit.co.jp/en/?s=谷歌快排【tg:seogoogle987】,obU
- https://www.paxnet.co.kr/search/jongmok?kwd=谷歌快排【tg:seogoogle987】JQE
- https://www.zutari.com/search/谷歌快排【tg:seogoogle987】,p0w
- https://www.silentiumpc.com/en/search/谷歌快排【tg:seogoogle987】,DAC
- https://www.lneg.pt/?cat=140&s=谷歌快排【tg:seogoogle987】,kqe