hive group by join 优化

group by 查询:

1、遇到group by查询时,会按照group by的键进行分发。

2、distinct与group by实现原理类似。


group by优化:

1、map端数据聚合,执行sql前先执行set hive.map.aggr=true

2、针对倾斜的key做两道作业的聚合,set hive.groupby.skewindata=true。


join 查询:

1、将小表写在join的左边,大表写在右边,左边的表会读入内存

2、hive只支持等值join


join优化:

1、map join:适用于一个大表和一个小表做关联的场景,较新的版本hive自动开启mapjoin,可以使用set hive.auto.convert.join=true;

    设置小表hive.mapjoin.smalltable.filesize大小;默认是MB

2、skew join:解决数据倾斜:set hive.optimize.skewjoin = true;俗称万能胶

set hive.skewjoin.key = skew_key_threshold(default=100000)

3、也可先进行count统计,找出发现倾斜比较厉害的key.进行where id not in ();过滤掉即可

HiveJoin中,作为Join key的字段比较,null=null是有意义的,且返回值为true。检查以下查询:

select u.uid, count(u.uid) from t_weblog l join t_user u on (l.uid = u.uid) group by u.uid;

查询中,t_weblog表中uid为空的记录将和t_user表中uid为空的记录做连接,即l.uid = u.uid=null成立。

如果需要与标准一致的语义,我们需要改写查询手动过滤null值的情况:

select u.uid, count(u.uid)

from t_weblog l join t_user u

on (l.uid = u.uid and l.uid is not null and u.uid is not null)

group by u.uid;

实践中,这一语义区别也是经常导致数据倾斜的原因之一。


如想更深入了解hive 底层原理 可以查看 点击打开链接


猜你喜欢

转载自blog.csdn.net/qq_33283716/article/details/80453998
今日推荐