hive语句优化-通过groupby实现distinct
2014-01-09 14:31
204 查看
同事写了个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多分钟的时间。
该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多分钟的时间。
相关文章推荐
- hive语句优化-通过groupby实现distinct
- hive语句优化-通过groupby实现distinct(数据量特别大的时候,使用distinct去重容易导致数据倾斜)
- hive语句优化-通过groupby实现distinct
- Hive的multi-distinct可能带来性能恶化之案例优化
- sql语句like的用法 有些正则表达式可以通过like实现
- 大数据Spark “蘑菇云”行动第89课:Hive中GroupBy优化、Join的多种类型实战及性能优化、OrderBy和SortBy、UnionAll等实战和优化
- 通过委托实现方法数组来代替switch分支语句
- 通过分析SQL语句的执行计划优化SQL
- 通过分析SQL语句的执行计划优化SQL(总结)
- 通过分析SQL语句的执行计划优化SQL(五)
- 通过分析SQL语句的执行计划优化SQL
- [转]通过分析SQL语句的执行计划优化SQL (七)[2]
- Hive针对distinct的优化(一)
- Spark性能优化之通过YDB实现比原生Spark性能高100倍的多表关联
- hive 底层模块实现-distinct
- php 通过file_put_contents 实现记录sql语句
- 通过SQL语句直接实现Excel与数据库的导入导出
- LINQ技巧:如何通过多次调用GroupBy实现分组嵌套
- sql中解决 distinct与groupby语句的不唯一性
- Mysql语句执行效率检查和通过索引优化_实例