【大数据技巧】MaxCompute优化去重计算的性能

免费开通大数据服务:https://www.aliyun.com/product/odps

转载自dachuan

随着双十一数据量的暴增,之前用distinct去重可以简单处理的场景,现在消耗的时间成倍增长。如果用了multiple distinct,那就更要警惕,因为多重去重本身会带来数据量的成倍增长,很可能10分钟的任务,在双十一期间会跑上几个小时都没有结果。

这里介绍一个小技巧,其实在稳定性手册里面已经有过介绍,不过总感觉没有看懂。最近正好做了一次优化,于是在这里小结一下:

例如原来的代码是这样:

select D1,D2,
count(distinct case when A is not null then B end) as B_distinct_cnt 
from xxx group by D1,D2

那么优化方案可以是这样:


create table tmp1
as
select D1,D2,B,
count( case when A is not null then B end ) as B_cnt
from xxx
group by D1, D1, B


select D1,D2,
sum(case when B_cnt > 0 then 1 else 0 end) as B_distinct_cnt
from tmp1
group by D1,D2

多重去重的优化也可以采用上面的方案,只是要注意Group By的Key是以源表聚合维度为基础,根据distinct计算的值进行组合。

例如下面的这个例子:

select D1,D2,
count(distinct case when A is not null then B end) as B_distinct_cnt ,
count(distinct case when E is not null then C end) as C_distinct_cnt 
from xxx group by D1,D2

那么优化方案可以是:


create table tmp1
as
select D1,D2,B,
count( case when A is not null then B end ) as B_cnt
from xxx
group by D1, D1, B

create table tmp1_1
as
select D1,D2,
sum(case when B_cnt > 0 then 1 else 0 end) as B_distinct_cnt
from tmp1
group by D1,D2

create table tmp2
as
select D1,D2,C,
count( case when E is not null then C end ) as C_cnt
from xxx
group by D1, D1, C

create table tmp2_1
as
select D1,D2,
sum(case when C_cnt > 0 then 1 else 0 end) as C_distinct_cnt
from tmp1
group by D1,D2

select 
t1.D1,t1.D2,
t1.B_distinct_cnt,
t2.C_distinct_cnt
from tmp1_1 t1
left outer join tmp2_1 t2
on t1.D1=t2.D1 and t1.D2=t2.D2
欢迎加入 MaxCompute 钉钉群讨论
42559c7dde62e4d333c90e02efdf416257a4be27


原文链接:
http://click.aliyun.com/m/13956/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值