实例:HiveSql数据倾斜处理

本文探讨了面试中常遇的Hive数据倾斜问题,通过创建临时表处理热点key,介绍mapjoin加速策略,并针对空值特殊情况进行了优化。提供了一套完整的解决方案和SQL实例。

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

|0x00 问题概述

在面试的时候,经常会问到一个问题:Hive在Join的时候,经常因为热点key的问题,导致数据倾斜,应该怎么解决?方法很简单,对导致倾斜的key做单独处理即可,最后再做union,那么Sql应该怎么写?

|0x01 常规处理

我们首先建一张临时表:

insert overwrite table tmp_0
select hot_key
from (
    select   hot_key
             ,count(1) as cnt
    from     table_0
    where    dt = '20201206'
    group by hot_key
) a
where    cnt >= 10000
;

然后将热点的key单独组装一段sql:

select *
from  table_1 a
join   tmp_0 b
on     a.key = b.hot_key
;

最后处理非热点key:

select *
from  table_1 a
left join tmp_0 b
on     a.key = b.hot_key
where  b.hot_key is null
;

|0x02 考虑mapjoin

当然,我们还有办法提速吗?有的,是使用mapjoin,参考下面的写法:

select /*+mapjoin(b)*/
       b.*
from   tmp_0 a 
right join table_1 b
on     a.hot_key = b.key
where  a.hot_key is null
;

|0xFF 考虑空值

当然,大多数情况下,key都是有空值情况存在的,我们需要对空值做特殊处理,以上一段sql为例:

select  /*+mapjoin(b1)*/
        b.*
from    tmp_0 a 
right join table_1 b
on      a.hot_key = coalesce(b.key, concat('other', rand()))
where   a.hot_key is null
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值