Hive MapJoin 执行计划

本文对比分析Hive中使用与未使用MapJoin的执行计划差异,通过具体SQL案例,展示了如何通过调整hive.mapjoin.smalltable.filesize参数,实现MapJoin优化,减少Shuffle过程,提升大数据处理效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

本文通过展示hive.mapjoin.smalltable.filesize 这个参数的设置,来比较是否使用mapjoin的执行计划的区别

测试sql:

SELECT id, clienttime
FROM (
  SELECT id, clienttime, key
  FROM log_table
  WHERE day = '20180801'
) a1
LEFT JOIN (SELECT key, field2 FROM key_mapping) a2 ON a1.key = a2.key

1. 未使用mapjoin

STAGE DEPENDENCIES:
  Stage-4 is a root stage , consists of Stage-1
  Stage-1
  Stage-0 depends on stages: Stage-1
STAGE PLANS:
  Stage: Stage-4
    Conditional Operator

  Stage: Stage-1
    Map Reduce
      Map Operator Tree:
          TableScan
            alias: id_app_runtimes
            Statistics: Num rows: 3156646800 Data size: 1666709511947 Basic stats: COMPLETE Column stats: NONE
            Select Operator
              expressions: id (type: string), clienttime (type: bigint), key (type: string)
              outputColumnNames: _col0, _col1, _col2
              Statistics: Num rows: 3156646800 Data size: 1666709511947 Basic stats: COMPLETE Column stats: NONE
              Reduce Output Operator
                key expressions: _col2 (type: string)
                sort order: +
                Map-reduce partition columns: _col2 (type: string)
                Statistics: Num rows: 3156646800 Data size: 1666709511947 Basic stats: COMPLETE Column stats: NONE
                value expressions: _col0 (type: string), _col1 (type: bigint)
          TableScan
            alias: key_mapping
            Statistics: Num rows: 145794 Data size: 29158920 Basic stats: COMPLETE Column stats: NONE
            Select Operator
              expressions: key (type: string), field2 (type: string)
              outputColumnNames: _col0, _col1
              Statistics: Num rows: 145794 Data size: 29158920 Basic stats: COMPLETE Column stats: NONE
              Reduce Output Operator
                key expressions: _col0 (type: string)
                sort order: +
                Map-reduce partition columns: _col0 (type: string)
                Statistics: Num rows: 145794 Data size: 29158920 Basic stats: COMPLETE Column stats: NONE
                value expressions: _col1 (type: string)
      Reduce Operator Tree:
        Join Operator
          condition map:
               Left Outer Join0 to 1
          keys:
            0 _col2 (type: string)
            1 _col0 (type: string)
          outputColumnNames: _col0, _col1
          Statistics: Num rows: 3472311555 Data size: 1833380502879 Basic stats: COMPLETE Column stats: NONE
          Select Operator
            expressions: _col0 (type: string), _col1 (type: bigint)
            Statistics: Num rows: 3472311555 Data size: 1833380502879 Basic stats: COMPLETE Column stats: NONE
            File Output Operator
              compressed: false
              Statistics: Num rows: 3472311555 Data size: 1833380502879 Basic stats: COMPLETE Column stats: NONE
              table:
                  input format: org.apache.hadoop.mapred.TextInputFormat
                  output format: org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat
                  serde: org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe

  Stage: Stage-0
    Fetch Operator
      limit: -1
      Processor Tree:
        ListSink

2. 使用了mapjoin

STAGE DEPENDENCIES:
  Stage-4 is a root stage , consists of Stage-5, Stage-1
  Stage-5 has a backup stage: Stage-1
  Stage-3 depends on stages: Stage-5
  Stage-1
  Stage-0 depends on stages: Stage-3, Stage-1
STAGE PLANS:
  Stage: Stage-4
    Conditional Operator

  Stage: Stage-5
    Map Reduce Local Work
      Alias -> Map Local Tables:
        a2:key_mapping 
          Fetch Operator
            limit: -1
      Alias -> Map Local Operator Tree:
        a2:key_mapping 
          TableScan
            alias: key_mapping
            Statistics: Num rows: 145794 Data size: 29158920 Basic stats: COMPLETE Column stats: NONE
            Select Operator
              expressions: key (type: string),  (type: string)
              outputColumnNames: _col0, _col1
              Statistics: Num rows: 145794 Data size: 29158920 Basic stats: COMPLETE Column stats: NONE
              HashTable Sink Operator
                keys:
                  0 _col2 (type: string)
                  1 _col0 (type: string)

  Stage: Stage-3
    Map Reduce
      Map Operator Tree:
          TableScan
            alias: id_app_runtimes
            Statistics: Num rows: 3156646800 Data size: 1666709511947 Basic stats: COMPLETE Column stats: NONE
            Select Operator
              expressions: id (type: string), clienttime (type: bigint), key (type: string)
              outputColumnNames: _col0, _col1, _col2
              Statistics: Num rows: 3156646800 Data size: 1666709511947 Basic stats: COMPLETE Column stats: NONE
              Map Join Operator
                condition map:
                     Left Outer Join0 to 1
                keys:
                  0 _col2 (type: string)
                  1 _col0 (type: string)
                outputColumnNames: _col0, _col1
                Statistics: Num rows: 3472311555 Data size: 1833380502879 Basic stats: COMPLETE Column stats: NONE
                Select Operator
                  expressions: _col0 (type: string), _col1 (type: bigint)
                  Statistics: Num rows: 3472311555 Data size: 1833380502879 Basic stats: COMPLETE Column stats: NONE
                  File Output Operator
                    compressed: false
                    Statistics: Num rows: 3472311555 Data size: 1833380502879 Basic stats: COMPLETE Column stats: NONE
                    table:
                        input format: org.apache.hadoop.mapred.TextInputFormat
                        output format: org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat
                        serde: org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe
      Local Work:
        Map Reduce Local Work

  Stage: Stage-1
    Map Reduce
      Map Operator Tree:
          TableScan
            alias: id_app_runtimes
            Statistics: Num rows: 3156646800 Data size: 1666709511947 Basic stats: COMPLETE Column stats: NONE
            Select Operator
              expressions: id (type: string), clienttime (type: bigint), key (type: string)
              outputColumnNames: _col0, _col1, _col2
              Statistics: Num rows: 3156646800 Data size: 1666709511947 Basic stats: COMPLETE Column stats: NONE
              Reduce Output Operator
                key expressions: _col16 (type: string)
                sort order: +
                Map-reduce partition columns: _col2 (type: string)
                Statistics: Num rows: 3156646800 Data size: 1666709511947 Basic stats: COMPLETE Column stats: NONE
                value expressions: _col0 (type: string), _col1 (type: bigint)
          TableScan
            alias: key_mapping
            Statistics: Num rows: 145794 Data size: 29158920 Basic stats: COMPLETE Column stats: NONE
            Select Operator
              expressions: key (type: string), field2 (type: string)
              outputColumnNames: _col0, _col1
              Statistics: Num rows: 145794 Data size: 29158920 Basic stats: COMPLETE Column stats: NONE
              Reduce Output Operator
                key expressions: _col0 (type: string)
                sort order: +
                Map-reduce partition columns: _col0 (type: string)
                Statistics: Num rows: 145794 Data size: 29158920 Basic stats: COMPLETE Column stats: NONE
                value expressions: _col1 (type: string)
      Reduce Operator Tree:
        Join Operator
          condition map:
               Left Outer Join0 to 1
          keys:
            0 _col2 (type: string)
            1 _col0 (type: string)
          outputColumnNames: _col0, _col1
          Statistics: Num rows: 3472311555 Data size: 1833380502879 Basic stats: COMPLETE Column stats: NONE
          Select Operator
            expressions: _col0 (type: string), _col1 (type: bigint)
            Statistics: Num rows: 3472311555 Data size: 1833380502879 Basic stats: COMPLETE Column stats: NONE
            File Output Operator
              compressed: false
              Statistics: Num rows: 3472311555 Data size: 1833380502879 Basic stats: COMPLETE Column stats: NONE
              table:
                  input format: org.apache.hadoop.mapred.TextInputFormat
                  output format: org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat
                  serde: org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe

  Stage: Stage-0
    Fetch Operator
      limit: -1
      Processor Tree:
        ListSink

上述执行计划中:

  1. stage5 有一个Map Local Tables 和 HashTable Sink Operator
  2. stage3 有一个Map Join Operator

两个stage结合起来完成了mapjoin 这样一个过程

而后面多出的stage1则是一个备份任务,即 stage5如果执行成功的话就不会执行该stage

3. 总结

使用mapjoin会多一个stage,但是将map-reduce简化成了一个单纯的map任务,少了一个shuffle和聚合的动作,最终执行的时候会更快

<think>嗯,用户想了解Hive中Map Join的工作原理,并且希望用图表来展示。首先,我需要回忆一下Hive的Map Join相关知识。记得Map Join主要是为了优化小表和大表的连接操作,避免Shuffle和Reduce阶段的开销。根据用户提供的引用内容,MapJoin是在Map阶段将小表加载到内存中,生成HashTableFiles,然后处理大表的数据进行连接。 接下来,我需要整理Map Join的工作步骤。首先,驱动表应该是小表,对吧?Hive会自动识别小表的大小,如果符合条件就会启用Map Join。然后,在Map阶段,小表的数据会被读取到内存,并构建成哈希表。之后处理大表的每个记录,用连接键去哈希表里查找匹配项,合并结果直接输出。整个过程不需要Shuffle和Reduce,这样效率更高。 用户提到需要图解,虽然文本形式无法画图,但可以通过文字描述图表的结构。例如,流程图分Map阶段和可能的Distributed Cache步骤。需要确保每个步骤对应引用中的内容,比如引用[1]和[3]提到的小表加载和分布式缓存的作用。可能还需要比较Common Join和Map Join的区别,如引用[2]和[4]中的Common Join流程,涉及Shuffle和Reduce阶段。 另外,用户可能对优化细节感兴趣,比如为什么使用分布式缓存,或者小表的大小限制。根据引用[3],当大量Mapper同时读取小表时,可能会有性能问题,所以用分布式缓存来缓解。这部分需要解释清楚,避免用户混淆。 然后,生成相关问题时要考虑用户可能的后续疑问,比如如何配置小表大小、Map Join的限制,或者性能调优的方法。这些问题需要基于已有的引用内容,确保答案的相关性和准确性。 最后,确保整个回答符合格式要求:行内公式用$,独立公式用$$,正确的中文回答,结构清晰。同时,引用标识要正确对应,比如引用[1]对应MapJoin的基本原理,引用[3]对应分布式缓存的作用,引用[4]对比Common Join的流程。 总结一下,回答的结构应该是先解释工作原理,分步骤描述,然后对比Common Join,再用文字图表展示流程,最后列出相关问题。需要确保每个关键点都有对应的引用支持,并且语言简洁明了,便于用户理解。</think>### Hive Map Join 工作原理图解说明 #### 一、核心原理 MapJoinHive 在 Map 阶段完成表连接的一种优化手段,**适用于小表(维度表)与大表(事实表)的 Join 操作**。其核心步骤如下: 1. **小表加载**:将小表数据从 HDFS 读取到内存,构建为 **哈希表(HashTable)**[^1]。 2. **大表处理**:顺序扫描大表,逐行用 Join Key 查询内存中的哈希表,匹配后直接输出结果[^3]。 3. **规避 Shuffle/Reduce**:整个过程仅在 Map 阶段完成,无需数据重分区和 Reduce 阶段[^2]。 #### 二、流程对比(文字图表) ``` |---------------MapJoin 流程---------------| 1. 驱动表(小表) │ ├─ 从 HDFS 加载到内存 → 构建哈希表(Key: Join列,Value: 其他列) │ 2. 被驱动表(大表) │ ├─ 逐行扫描大表数据 │ ├─ 用 Join Key 查询内存哈希表 │ └─ 匹配成功则合并两表数据 → 直接输出结果 ``` #### 三、与 Common Join 对比 | **特性** | **MapJoin** | **Common Join** | |------------------|--------------------------------------|--------------------------------------| | **执行阶段** | 仅 Map 阶段 | Map + Shuffle + Reduce 阶段 | | **数据移动** | 无 Shuffle | 需按 Key 分区到 Reducer[^4] | | **适用场景** | 小表(几十MB内) | 大表间 Join | | **性能瓶颈** | 小表加载内存的并行度[^3] | Shuffle 网络开销和 Reducer 计算压力 | #### 四、优化扩展 - **分布式缓存**:当小表较大时,通过分布式缓存(Distributed Cache)将哈希表分发给所有 Mapper,避免集中读取 HDFS 的性能瓶颈。 - **自动触发条件**:Hive 默认根据 `hive.auto.convert.join` 参数判断小表大小(如 `hive.mapjoin.smalltable.filesize=25MB`)。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值