谓词下推

一、参考地址

  1)官网参考:https://cwiki.apache.org/confluence/display/Hive/OuterJoinBehavior
  2)cbo参考:https://cwiki.apache.org/confluence/display/Hive/Cost-based+optimization+in+Hive

二、分析

  谓词下推:就是在join之前的 mr 任务的 map 阶段提前对表进行过滤优化,使得最后参与 join 的表的数据量更小

  首先定义了join的四种类型的表:

  1、Preserved Row table:保留表,通俗地说就是
      a left (outer) join b 中的 a 表;
      a right (outer) join b 中的 b 表;
      a full outer join b a 和 b 表都是 保留表。
  2、Null Supplying table:提供null值的表,也就是 非保留表,在join中如果不能匹配上的使用null填充的表
      a left (outer) join b 中的 b 表;
      a right (outer) join b 中的 a 表,
      a full outer join b a 和 b 表都是 null值保留表
  3、During Join predicate:join中谓词,就是on后面的条件: R1 join R2 on R1.x = 5   -->  R1.x = 5就是join中谓词
  4、After Join predicate:join后谓词,就是where后面的条件: a left join b where a.t=1    -->   a.t=1 就是join后谓词

三、测试

  测试时,关闭 cbo 优化:set hive.cbo.enable=false

3.1 Left outer Join & Right outer Join

  案例一:过滤条件写在 where, 且是 保留表的字段 --> 可以谓词下推(能在 map 阶段提前过滤)

explain
select o.id from bigtable b
left join bigtable o 
where b.id <= 10;

  案例二:过滤条件写在 where, 且是 非保留表的字段 --> 不可以谓词下推(不能在 map 阶段提前过滤)
      注意: 关闭cbo去测 set hive.cbo.enable=false,如果开启了cbo,会自动优化成可以谓词下推;

explain
select b.id,o.id from bigtable b
left join bigtable o
where o.id <= 10;

  案例三:过滤条件写在 on, 且是 保留表的字段 --> 不可以谓词下推(不能在 map 阶段提前过滤)

explain
select o.id from bigtable b
left join bigtable o
on b.id <= 10;

  案例四:过滤条件写在 on, 且是 非保留表的字段 --> 可以谓词下推(能在 map 阶段提前过滤)

explain
select o.id from bigtable b
left join bigtable o
on o.id <= 10;

  总结:

    在关闭cbo的情况下

  保留表字段(left的左表) 非保留表字段 (left的右表)
on 不可以 可以
where 可以 不可以(开启cbo,可以)

  1、对于 Left outer Join ,右侧的表写在 on后面、左侧的表写在 where后面,性能上有提高;

  2、对于 Right outer Join,左侧的表写在 on后面、右侧的表写在 where后面,性能上有提高;

3.2 Full outer Join

  案例一:过滤条件写在 where, 且是 保留表的字段 --> 可以谓词下推(能在 map 阶段提前过滤)

explain
select o.id from bigtable b
full join bigtable o 
where b.id <= 10;

  案例二:过滤条件写在 where, 且是 非保留表的字段 --> 可以谓词下推(能在 map 阶段提前过滤)

explain
select b.id,o.id from bigtable b
full join bigtable o
where o.id <= 10;

  案例三:过滤条件写在 on, 且是 保留表的字段 --> 不可以谓词下推(不能在 map 阶段提前过滤)

explain
select o.id from bigtable b
full join bigtable o
on b.id <= 10;

  案例四:过滤条件写在 on, 且是 非保留表的字段 --> 不可以谓词下推(不能在 map 阶段提前过滤)

explain
select o.id from bigtable b
full join bigtable o
on o.id <= 10;

  总结:

    Full outer Join情况下

      如果不开启 cbo,写在 on后面,还是 where后面,都不会谓词下推
      如果开启了 cbo,写在 where 可以 谓词下推, 写在 on 不可以 谓词下推

3.3 Inner Join

  案例一:过滤条件写在 where, 且是 保留表的字段 --> 可以谓词下推(能在 map 阶段提前过滤)

explain
select o.id from bigtable b
join bigtable o 
where b.id <= 10;

  案例二:过滤条件写在 where, 且是 非保留表的字段 --> 可以谓词下推(能在 map 阶段提前过滤)

explain
select b.id,o.id from bigtable b
join bigtable o
where o.id <= 10;

  案例三:过滤条件写在 on, 且是 保留表的字段 --> 可以谓词下推(能在 map 阶段提前过滤)

explain
select o.id from bigtable b
join bigtable o
on b.id <= 10;

  案例四:过滤条件写在 on, 且是 非保留表的字段 --> 可以谓词下推(能在 map 阶段提前过滤)

explain
select o.id from bigtable b
join bigtable o
on o.id <= 10;

  总结: Inner Join 不管有没有开启cbo,不管写在 on后面 还是 where后面,都会进行谓词下推

原文地址:https://www.cnblogs.com/LzMingYueShanPao/p/15167014.html