ARTICLE DETAIL

建站实战干货

来自一线的建站与推广经验沉淀,每一条都经过真实交付验证。

Hive调优——count distinct去重优化

2026/9/11 9:27:23 拓冰建站 浏览量
Hive调优——count distinct去重优化

   离线数仓开发过程中经常会对数据去重后聚合统计,而对于大数据量来说,count(distinct ) 操作消耗资源且查询性能很慢,以下是调优的方式。

解决方案一:group by 替代

原sql 如下:

#=====7日、14日的app点击的用户数(user_id去重统计)
selectgroup_id,app_id,
-- 7日内UVcount(distinct case when dt >= '${7d_before}' then user_id else null end)  as 7d_uv, 
--14日内UVcount(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先去重,后聚合统计

#=====7日、14日的app点击的用户数(user_id去重统计)
selectgroup_id,app_id,
-- 7日内UVsum(case when 7d_cnt > 0 then 1 else 0 end) as 7d_uv,
--14日内UVsum(case when 14d_uv > 0 then 1 else 0 end) as 14d_uvfrom (selectgroup_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_uvfrom tblwhere dt >= '${14d_before}'group by group_id,app_id,user_id) tmp1
group by group_id,app_id;

方案一弊端:数据倾斜

  解决方案一通过两阶段的group by(分组聚合) 对count (distinct) 进行改造调优,需要注意的是:group by底层会走shuffle,会有数据倾斜的风险,因此方案一还可以进一步优化,待补充!

参考文章:

别再使用count distinct了_避免使用count distinct-CSDN博客