在数仓开发中经常会对数据去重后统计,而对于大数据量来说,count(distinct )操作明显非常的消耗资源且性能很慢。
下面介绍我平时使用最多的一种优化方式,供大家参考。
原SQL:
select
group_id,
app_id,
count(distinct case when dt>='${7d_before}' then user_id else null end) as 7d_uv, -- 7日内UV
count(distinct case when dt>='${14d_before}' then user_id else null end) as 14d_uv --14日内UV
from tbl
where dt>='${14d_before}'
group by
group_id,
app_id
;
优化后:
先去重,再汇总。
select group_id
,app_id
,sum(case when 7d_cnt>0 then 1 else 0 end) AS 7d_uv, -- 7日内UV
,sum(case when 14d_cnt>0 then 1 else 0 end) AS 14d_uv --14日内UV
from (
select
group_id,
app_id,
user_id, --按user_id去重
count(case when dt>='${7d_before}' then user_id else null end) as 7d_cnt, -- 7日内各用户的点击量
count(case when dt>='${14d_before}' then user_id else null end) as 14d_cnt --14日内各用户的点击量
from tbl
where dt>='${14d_before}'
group by
group_id,
app_id,
user_id
) a
group by group_id,
app_id
;
希望本文对你有帮助,请点个赞鼓励一下作者吧~ 谢谢!
本文分享了一种优化数仓开发中大数据量去重统计的方法,通过先去重再汇总,避免了count(distinct)带来的性能瓶颈。通过实例展示了如何将SQL中的distinct操作替换为子查询和case when,提升查询效率。
3106

被折叠的 条评论
为什么被折叠?



