hive优化之distinct转group by实战

说明:一个公司有多个业务biz_id,每个业务收款的时候通过不同或者相同的account去收款

业务需要 需要计算出该公司有过滤重复后biz_count 和过滤重复后的account_count

1先来个distinct ,占用内存,容易oom,所以一般都会把distinct改为group by

select company_name,count(distinct  biz_id),count(distinct account_id)
from  odsiadata.ia_fdw_b_profile_account_info
where company_id ='1001000000113619'
group by company_name

2不用distinct,想到了 collect_set ,个人感觉应该和distinct差不多也会占用内存,有大佬知道哪里有distinct占内存或者collect_set占内存出处留言让我学习下

select company_name,size(collect_set(biz_id)),size(collect_set(account_id))
from  odsiadata.ia_fdw_b_profile_account_info
where company_id ='1001000000113619'
group by company_name

 3用group by  借鉴https://blog.csdn.net/henrrywan/article/details/86597468  但是他的sql不完整感觉像复制的。。。。不管

select company_name ,
		sum(IF(t_type='biz',1,0)) biz_count, --记住用sum。。。之前用count浪费了会时间
		sum(if(t_type='account',1,0)) account_num , --记住用sum。。。之前用count浪费了会时间
		MIN(created_time) 
from(
select company_name,biz_id as col ,"biz" as t_type ,min(created_time) created_time from  odsiadata.ia_fdw_b_profile_account_info 
group by company_name,biz_id 
union
select company_name,account_id as col ,"account" as t_type,min(created_time) created_time from odsiadata.ia_fdw_b_profile_account_info 
group by company_name,account_id 
)t
GROUP by company_name 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值