hive的数据倾斜
主要是reduce端产生的
reducetask–0.95*datanode个数
group 如果和聚合函数一起使用,默认在map端开始combiner,不会产生数据倾斜
产生数据倾斜的根本原因就是key分配不均匀
常见场景:
一.null值过多
解决:1.null值不参与连接
select a.,b. from log a join user b on a.userid=b.userid and a.userid is not null;
2.连接的时候不加null,之后另外拼接
select a.,b. from log a join user b on a.userid=b.userid and a.userid is not null
union all
select * from log where userid is null;
union all 和union的区别
union 会去重
3.给null值加随机数,或者将null转化为随机数,相当于把null值打散了
nvl(userid,rand()) 可以给这个随机数一个范围,一个不在正常useid的范围,方便以后查询
二.参与连接的数据类型不统一
select a.,b. from log a join user b on a.userid=b.userid
log里的string类型,user里是int类型, 345+空格 转化为int就会变成null
关联的时候会将string类型转化为int类型
select a.,b. from log a join user b on cast(trim(a.userid) as int)=b.userid
三.大小表关联查询产生的数据倾斜
1.将reduce端jion转化为map端join
hive默认join
取决于小表的大小,如果小表超过限制则不走mapjoin
hive.smalltable.filesize 默认25m
强制执行mapjoin
/+mapjoin(表名)+/
select /+mapjoin(user)+/ a.,b. from log a join user b on a.userid=b.userid
2.大大表关联
将一个表切分,进行分桶
hive的优化就是mr的优化
1.合理选择排序
order by 全局排序
sort by 局部排序,随机选择字段分区
distribute by 分桶
cluster by 先分区再排序,不能desc
2.笛卡尔积问题
避免使用笛卡尔积,没有关连建,性能低
如果要要用我们可以加一列,进行关联
3.用left semi join 取代in/exists
4.小文件合并的问题
5.合理设计maptask的并行度
jvm重用
6.合理设计reducetask
datanode*0.95
7.合理设计分区
提升查询性能,减少扫描范围
把需要经常查的字段设为分区字段
8.合理设计分桶
对分区更加细粒度的划分
9.合并mr操作
from放在前面,insert放在后面
insert overwrite 和insert into
将原有的数据覆盖 追加插入
10.join 优化
原则:
先过滤再进行join,减少join的数据量
能用mapjoin尽量不用reducejoin
多表join时,先小表join再大表join