分享

关于distinct 和 group by的问题

zcfightings 发表于 2015-9-18 14:50:02 [显示全部楼层] 回帖奖励 阅读模式 关闭右栏 2 23379
问题1.     好像一直听说distinct()是一个reduce ,现在突然对这个观点表示质疑,因为distinct完全可以使用多个reduce呀 因为在partition的时候 相同的key肯定会进到相同的reduce中 就是说两个reduce中不可能存在相同的数据呀。
   问 distinct只能用一个reduce的观点是错的吗  如果用一个reduce 那么为什么他这么设计呢 他完全可以用多个reduce的呀 。如果他确实用的是多个reduce那么就有了问题2.

问题2  
一般会用group by代替distinct做优化 因为group by可以多个reduce,那既然distinct 也是多个reduce了  group by 实际比distinct 优化在哪里呢 ?

在线等




已有(2)人评论

跳转到指定楼层
tntzbzc 发表于 2015-9-18 16:35:05
至于是不是,主要还是看效率。实践是检验真理的唯一标准。

###################################################
下面说下个人看法:

下面是引用的一个例子
同事写了个hive的sql语句,执行效率特别慢,跑了一个多小时程序只是map完了,reduce进行到20%。
该Hive语句如下:
select count(distinct ip)
from (select ip as ip from comprehensive.f_client_boot_daily where year="2013" and month="10"  
union all
select pub_ip as ip from f_app_boot_daily where year="2013" and month="10"
union all select ip as ip from format_log.format_pv1 where year="2013" and month="10" and url_first_id=1
) d

       分析:select ip as ip from comprehensive.f_client_boot_daily where year="2013" and month="10"这个语句筛选出来的数据约有10亿条,select pub_ip as ip from f_app_boot_daily where year="2013" and month="10"约有10亿条条,select ip as ip from format_log.format_pv1 where year="2013" and month="10" and url_first_id=1 筛选出来的数据约有10亿条,总的数据量大约30亿条。这么大的数据量,使用disticnt函数,所有的数据只会shuffle到一个reducer上,导致reducer数据倾斜严重。
       解决办法:
       首先,通过使用groupby,按照ip进行分组。改写后的sql语句如下:
select count(*)
from
(select ip
from
(select ip as ip from comprehensive.f_client_boot_daily where year="2013" and month="10"
union all
select pub_ip as ip from f_app_boot_daily where year="2013" and month="10"
union all select ip as ip from format_log.format_pv1 where year="2013" and month="10" and url_first_id=1
) d
group by ip ) b
       然后,合理的设置reducer数量,将数据分散到多台机器上。set mapred.reduce.tasks=50;
       经过优化后,速度提高非常明显。整个作业跑完大约只需要20多分钟的时间。


如果是disticnt为什么会shuffle到一个reduce,这个跟分区函数有关系,至于具体是什么,这里还没有找到,但是就大胆推测下。
可能跟group by有关系, 这时候没有那么分区函数,可能就把他们往一个分区里灌数据

而一旦group by,这时候ip是有相同的,也就是ip相同的灌到同一个reduce里。这样达到数据分散的目的。





回复

使用道具 举报

zcfightings 发表于 2015-9-18 21:30:41
tntzbzc 发表于 2015-9-18 16:35
至于是不是,主要还是看效率。实践是检验真理的唯一标准。

########################################## ...

恩 首先谢谢回复。
你说的那个例子我也看过。我也实际测过 性能确实会好 但问题在于 为什么会好呢  这块还要继续探究呀
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

推荐上一条 /2 下一条