做消费网站流程,手工做的网站,网站建设误区图,精品课程网站怎么做离线数仓开发过程中经常会对数据去重后聚合统计#xff0c;而对于大数据量来说#xff0c;count(distinct ) 操作消耗资源且查询性能很慢#xff0c;以下是调优的方式。
解决方案一#xff1a;group by 替代
原sql 如下#xff1a;
#7日、14日的app点击的用户数#x… 离线数仓开发过程中经常会对数据去重后聚合统计而对于大数据量来说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博客