当前位置:首页 > 文章中心 > 正文内容

HiveSQL执行计划解读-查看扩展/依赖信息

dgx66615小时前文章中心2

今天我们继续HiveSQL执行计划的学习。在学习之前我们先复习之前:

  1. 大数据Hive-SQL执行计划解读(explain)

回顾完之前的文章,我们继续解读SQL执行计划的扩展和依赖信息,即:

  • explain extended
  • explain dependency

查看执行计划的扩展信息

explain extended顾名思义就是对explain的扩展,打印的信息会比explain更加丰富,包含三个部分:

第一部分:抽象语法树(ABSTRACT SYNTAX TREE,简称AST):是SQL转换成MapReduce或其它计算引擎的任务中的一个过程。在Hive 3.0版本,AST会从explain extended的移除,要查看AST,需要使用explain ast的命令。

第二部分:作业的依赖关系图,即STAGE DEPENDENCIES,其内容和explain所展现的一样,不做重复介绍。

第三部分:每个作业的详细信息,即STAGE PLANS,在打印每个作业详细信息时,explain extend会打印出更多的信息,除了explain打印出的内容,还包括每个表的HDFS读取路径,每个Hive表的表配置信息等。

查看SQL数据输入依赖的信息

explain dependency用于描述一段SQL需要的数据来源,输出是一个json格式的数据,里面包含两个部分:

第一部分:input_partitions:描述一段SQL依赖的数据来源表分区,里面存储的是分区名的列表,格式如下:

{"partitionName":"库名@表名 @分区列=分区列的值"}

如果整段SQL包含的所有表都是非分区表,则显示为空。

第二部分:input_tables:描述一段SQL依赖的数据来源表,里面存储的是Hive表名的列表,格式如下:

{"tablename":"库名@表名 ","tabletype":表的类型(外部表/内部表)"}

下面看两个案例,案例6.3是查询非分区普通表SQL的explain dependency,案例6.4时查询分区表SQL的explain dependency

案例6.3 使用explain dependency查看SQL查询非分区普通

--业务逻辑同案例6.1
explain dependency
select s_age,count(1) num from student_tb_orc
where s_age<30 and s_name like '%红%'
group by s_age;
输出结果如下:
{"input_partitions":[],"input_tables":[{"tablename":"default@student_tb_orc","tabletype":"MANAGED_TABLE"}]}

案例6.4 使用explain denpendency查看SQL查询分区表

--业务逻辑同案例6.1
explain dependency
select s_age,count(1) num from student_orc_partition
where s_age<30 and s_name like '%红%'
group by s_age
输出结果如下:
{"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使用场景:

场景一:快速排错。快速排除因为读取不到相应分区的数据导致任务数据输出异常的情况。例如,在一个以天分区任务中,上游任务因为生产过程不可控因素导致出现异常或者出现空跑,导致下游任务引发异常。通过这种方式,可以快速查看SQL读取的分区,是否出现异常。

场景二:帮助理清表的输入,帮助理解程序的运行,特别是有助于理解有多重数据查询,多表连接的依赖输入。

下面通过两个案例来看explain dependency的实际运用。在案例6.5中,我们会通过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”这种写法,我们可以通过案例6.5,来查看下它们的区别。

案例6.5 通过explain dependency识别看似等价的代码

--代码片段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并不等价,在内连接(inner join)中的连接条件中加入非等值的过滤条件,并没法将内连接的左右两表按照过滤条件进行过滤,内连接在执行时候会多读取part=0的分区数据。

explain dependency可以帮助纠正错误的认知。大部分SQL的学习者,在学习外连接,包括左外连接、右外连接、全外连接,如果不细抠概念,很容易将下面案例6.6中两种情况进行搞混。

案例6.6 使用explain dependency识别SQL读取数据范围差别

---代码片段1
explain dependency
select a.s_no
from student_orc_partition  a
left outer 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 outer 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可以像代码片段1一样进行数据过滤,通过查看explain dependency输出结果,可以知道不是如此。

下面是代码片段1和2的explain dependency输出结果:

--代码片段1的打印结果
{"input_partitions":
[{"partitionName": "default@student_orc_partition@part=0"},
{"partitionName":"default@student_orc_partition@part=1"},
…中间省略7个分区
{"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的打印结果
{"input_partitions":
[{"partitionName":"default@student_orc_partition@part=0"},
{"partitionName":"default@student_orc_partition@part=1"},
…中间省略7个分区
{"partitionName":"default@student_orc_partition@part=9"},
{"partitionName":"default@student_orc_partition_only@part=0"},
{"partitionName":"default@student_orc_partition_only@part=1"},
..中间省略7个分区
{"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表)有起到过滤的效果,右表只要扫描两分区,但是左表(a表)会进行全表扫描。如果过滤条件是针对左边的表,则完全没有起到过滤的作用,两个表进行全表扫描。这时的情况就如全外连接一样都需要对两个数据进行全表扫描。

扩展:如果要使用外连接并需要对左右两表进行条件过滤,最好的方式就是将过滤条件放到表的就近处,即如果已经知道表数据过滤筛选条件,那么在使用该表前,就用该过滤条件进行过滤,一些SQL内置优化器的也会做上述的优化,但是我们还是建议按上面介绍的方式写出来。例如将代码片段2改写成如下案例6.7形式,即在使用表数据之前尽可能过滤掉不需要的数据:

案例6.7 尽早过滤掉不需要的数据

select a.s_no
from (
  select s_no,part
  from student_orc_partition
  --在自查询内部进行过滤
  where part>=1 and part<=2
)  a
left outer join student_orc_partition_only b
on a.s_no=b.s_no and a.part=b.part ;

#Hive# #大数据# #SQL#

扫描二维码推送至手机访问。

版权声明:本文由第六芝士网发布,如需转载请注明出处。

本文链接:http://www.dgx666.com/post/2111.html

分享给朋友:

“HiveSQL执行计划解读-查看扩展/依赖信息” 的相关文章

方正字库175种字体包(常用的基本都有了)

方正字库_175种字体包(常用的基本都有了) 请勿商用 (cmct_中文字体)hwKaiTi.ttf (cmct_英文字体)Cronos Pro Subhead.otf 4078_方正报宋_GBK.TTF 4079_方正彩云_GBK.ttf 4080_方正超粗黑_GBK.ttf 4081_方正粗宋_...

《魔兽争霸III》官方对战平台1.2.0版本更新公告

《魔兽争霸III》官方对战平台1.2.0版本更新完毕,备受期待的RPG游戏大厅也随之正式上线。以下是本次更新的具体内容:房间列表入口调整:房间列表的入口被调整到标签页的右侧,与游戏大厅互相独立开放RPG游戏大厅,大厅新增功能如下:新增地图分类及搜索功能:玩家可以在游戏大厅内通过地图类型或地图名快速搜...

CAD高版本图纸打不开,用它解决

相信用过autoCAD的人都遇见过高版本打不开低版本的图纸这个情况,这个时候通常就需要找一个版本转换器,这里就告诉大家一个不用找转换器的方式用到的操作软件:CAD梦想画图,点此进入官网。解决的问题操作步骤1.打开需要转换的图纸,点击上方“功能-高级工具”随后弹出高级工具条在里面找到“DWG版本转换”...

CAD打印怎么设置黑白?一文教会你CAD打印黑白图纸

CAD设计过程中,为了区分图纸中各个图形对象使其看起来更加直观,方便二次编辑,通常会用不同颜色来标识不同的内容。但在CAD打印时并不需要将这些颜色全部打印出来,只需要黑白打印即可。那么,你知道CAD打印怎么设置黑白吗?本文小编就以浩辰CAD软件为例来给大家分享一下CAD打印怎么设置黑白吧!CAD打印...

AUTOCAD——中心线绘制

创建与选定直线和多段线关联的指定线型的中心线几何图形。执行方式1.输入中心线绘制命令“CENTERLINE”(快捷键:CL)。中心线绘制命令2.根据提示选择第一条直线,鼠标左键点击。第一条直线3.接下来再选择第二条直线,鼠标左键点击。第二条直线4.中心线标注完成,标注效果如下图:标注完成标注效果...

三款好看好用的电脑桌面便签备忘录推荐

怎么在Win电脑桌面上添加便签/备忘录显示呢?给大家介绍三款常用的桌面便签备忘录工具!1、电脑自带便笺工具:sticky notesWindows系统自带的便笺工具,英文名sticky notes,在电脑桌面上点击“开始”,即可在程序列表中找到并打开。它以一张一张的彩色便利贴呈现在桌面上,可以记录简...