Hive 调优总结2

  1. 无需MapReduce

hive-default.xmlhive.fetch.task.conversion默认是more,老版本是minimal,该属性改为more后,在全局查找、字段查找、limit查找等都不走mapreduce

Expects one of [none, minimal, more].
    Some select queries can be converted to single FETCH task minimizing latency.
    Currently the query should be single sourced not having any subquery and should not have
    any aggregations or distincts (which incurs RS), lateral views and joins.
    0. none : disable hive.fetch.task.conversion
    1. minimal : SELECT STAR, FILTER on partition columns, LIMIT only
    2. more    : SELECT, FILTER, LIMIT only (support TABLESAMPLE and virtual columns)
  1. 空Key过滤(非inner join)
    方式:添加子查询where id is not null
    不适用情况:inner join会自动过滤。不需要字段为NULL的
  2. 空Key转换
    通过如自定义函数等手段将空Key转换位其他散列数值。注意新Key与老Key要完全不重合,否则可能出现关联行为。数据倾斜 - 将Null分散到每个reducer,nvl(n.id, rand()) = o.id
  3. 数据倾斜
    当出现数据倾斜的时候可以设置参数:set hive.groupby.skewindata = true,此时生成的查询计划会有两个MR Job。第一个MR Job增加Key,为了结果随机分布到Reduce中,每个Reduce做部分聚合并输出结果,达到负载均衡的目的;第二个MR Job在根据预处理的数据按照GROUP By Key分布到Reduce中(这个过程可以保证相容的Key被分布到同一个Reduce中),最后完成最终的聚合。
  4. Count(Distinct)去重统计
    此场景下只会有一个Reduce。优化方式:select count(*) from (select id from table group by id) t1;。原理是通过group by 去重,但此方法可以并行执行提高效率。
  5. 行列过滤
    在关联查询前先通过子查询缩小表。如:
直接进行关联查询,则关联之后再做全局条件行裁剪。
select o.id from table b
join table o on o.id = b.id
where o.id < 10
先通过子查询,极大减少了关联结果
select b.id from table b
join (select id from table where id < 10) o
 on b.id = o.id

通过执行计划查看到,1、2两种方式在fetch数据时都带有filter条件,这是底层做了经典的谓词下推优化。关联条件与过滤条件相同会触发更深优化,如方式1中o,b两表都会触发谓词下推。SQL过于复杂可能导致方式1的自动优化失效,故推荐方式2.

  1. 笛卡尔积
    一般生产环境开启此检查:set hive.strict.checks.cartesian.product=true
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值