(12)Hive调优——count distinct去重优化

离线数仓开发过程中经常会对数据去重后聚合统计,count distinct使得map端无法预聚合,容易引发reduce端长尾,以下是count distinct去重调优的几种方式。

解决方案一:group by 替代

原sql 如下:

sql 复制代码
#=====7日、14日的app点击的用户数(user_id去重统计)
select
    group_id,
    app_id,
-- 7日内UV
    count(distinct case when dt >= '${7d_before}' then user_id else null end)  as 7d_uv, 
--14日内UV
    count(distinct case when dt >= '${14d_before}' then user_id else null end) as 14d_uv 
from tbl
where dt >= '${14d_before}'
group by group_id, --渠道
         app_id;  --app

优化思路:group by两阶段聚合

sql 复制代码
#=====7日、14日的app点击的用户数(user_id去重统计)
select
    group_id,
    app_id,
-- 7日内UV
    sum(case when 7d_cnt > 0 then 1 else 0 end) as 7d_uv,
--14日内UV
    sum(case when 14d_uv > 0 then 1 else 0 end) as 14d_uv

from (select
          group_id,
          app_id,
          -- 7日内各渠道各app下的每个用户的点击量
          count(case when dt >= '${7d_before}' then user_id else null end)  as 7d_cnt,
          -- 14日内各渠道各app下的每个用户点击量
          count(case when dt >= '${14d_before}' then user_id else null end) as 14d_uv
      from tbl
      where dt >= '${14d_before}'
      group by group_id,
               app_id,
               user_id) tmp1
group by group_id,
         app_id;

方案一弊端:数据倾斜风险

解决方案一通**过两阶段group by(分组聚合)**对count (distinct) 进行改造调优,需要注意的是:如果分组字段user_id在tbl 表中存在大量的重复值,group by底层走shuffle,会有数据倾斜的风险,因此方案一还可以进一步优化。

解决方案二:group by调优

1)添加随机数,两阶段聚合(推荐)

sql 复制代码
#===============优化前
insert overwrite table tblB partition (dt = '2022-10-19')
select
    cookie_id,
    event_query,
    count(*)  as cnt
from tblA
where dt >= '20220718'
  and dt <= '20221019'
  and event_query is not null
group by cookie_id, event_query



#===============优化后
insert overwrite table tblB partition (dt = '2022-10-19')
select
    split(tkey, '_')[1] as cookie_id,
    event_query,
 #--- 求出最终的聚合值
    sum(cnt)   as cnt
from (
         select
             concat_ws('_', cast(ceiling(rand() * 99) as string), cookie_id) as tkey,
             event_query,
      #---将热点Key值:cookie_id 进行打散后,先局部聚合得到cnt
             count(*)  as cnt
         from tblA
         where dt >= '20220718'
           and dt <= '20221019'
           and event_query is not null
        #--- 第一阶段:添加[0-99]随机整数,将热点Key值:cookie_id 进行打散( M -->R)
         group by concat_ws('_', cast(ceiling(rand() * 99) as string), cookie_id),
                  event_query
     ) temp
 #--- 第二阶段:对拼接的key值进行切分,还原原本的key值split(tkey, '_')[1] =cookie_id ( R -->R)
group by split(tkey, '_')[1], event_que

优化思路为:

  • 第一阶段:对需要聚合的Key值添加随机后缀进行打散,基于加工后的key值进行初步聚合(M-->R1)
  • 第二阶段:对加工后的key值进行切分还原,对第一阶段的聚合值进行再次聚合,求出最终结果值(R1-->R2)

**2)**开启Map端聚合

sql 复制代码
#--开启Map端聚合,默认为true
set hive.map.aggr = true;
#--在Map 端预先聚合操作的条数
set hive.groupby.mapaggr.checkinterval = 100000;

该参数可以将顶层的聚合操作放在 Map 阶段执行,从而减轻shuffle清洗阶段的数据传输和 Reduce阶段的执行时间,提升总体性能。

**3)**数据倾斜时自动负载均衡

sql 复制代码
#---有数据倾斜的时候自动负载均衡(默认是 false)
set hive.groupby.skewindata = true;

开启该参数后,当前程序会自动通过两个MapReduce来运行,将M->R阶段 拆解成 M->R->R阶段

  • 第一个MapReduce自动进行随机分布到Reducer中(负载均衡),每个Reducer做部分聚合操作,输出结果
  • 第二个MapReduce将上一步聚合的结果再按照业务(group by key)进行处理,保障相同的key分发到同一个reduce做最终聚合。
相关推荐
nvd1115 小时前
折腾 Niri WM:手搓一个完美的多显示器下拉终端 (Drop-down Terminal)
数据仓库
QQ129584550415 小时前
FERP50 - Excel以存储过程方式访问数据仓库
数据仓库·spark·excel
It's Q16 小时前
Hive序列函数&&排名函数
数据仓库·hive·hadoop
Irene199116 小时前
外部表(EXTERNAL_TABLE)Hive 借用数据,删表不删数据
hive·内部表·外部表
Irene19912 天前
Windows 11 WSL Ubuntu 环境:实际安装 Hive 踩坑实录
hive·windows·ubuntu
Irene19912 天前
(课堂笔记)Hive 分区、分桶与数据倾斜
hive·hadoop
Irene19913 天前
在 WSL Ubuntu 上安装和使用 Hive
linux·hive·ubuntu
Irene19913 天前
(课堂笔记)Hive 基础
hive·hadoop
云策数链3 天前
ERP报表系统设计与数据仓库
数据仓库·erp·用友·云策数链
水火既济__4 天前
加快hive效率
数据仓库·hive·hadoop