HIve优化
–分区
SET hive.exec.dynamic.partition=true; – 开启动态分区,默认为false
SET hive.exec.dynamic.partition.mode=nonstrict;
– 设置nonstrict使所有的分区都被设定为动态,否则至少需要指定一个分区值
set hive.exec.max.dynamic.partitions.pernode=10000;
– 每个mapper或reduce创建的最大动态分区值,如果大于这个值,则报错
set hive.exec.max.dynamic.partitions=100000;
– 一条带有动态分区的SQL语句所能创建的动态分区总量,如果大于该值,这报错
set hive.exec.max.created.files=150000;
– 全局能够被创建文件数据的最大值,有专门的一个hadoop计数器来跟踪该值,如果超出则报错
–hive压缩
set hive.exec.compress.intermediate=true;
set hive.exec.compress.output=true;
–写入时压缩生效
set hive.exec.orc.compression.strategy=COMPRESSION;
–分桶
set hive.enforce.bucketing=true;
set hive.enforce.sorting=true;
set hive.optimize.bucketmapjoin = true;
set hive.auto.convert.sortmerge.join=true;
set hive.auto.convert.sortmerge.join.noconditionaltask=true;
–并行执行
set hive.exec.parallel=true; – 默认为false
set hive.exec.parallel.thread.number=8; – 同一个sql允许的最大并行度,默认为8
– Hive会将一个查询转化为一个或多个阶段,默认情况下,一次只执行一个阶段。如果某些阶段不是相互依赖,则可以并行执行
–矢量化查询
set hive.vectorized.execution.enabled=true;
– hive的默认查询执行引擎一次处理一行,矢量化查询执行是一种hive特性,按照每批1024行读取数据,一次性对整个记录整合(而不是对单条记录)应用操作
–读取零拷贝
set hive.exec.orc.zerocopy=true;
– ORC可以使用新的HDFS缓存API和ZeroCopy读取器来避免在扫描文件时将额外的数据复制到内存中。
两表join时:MapJoin:
- 适用场景: 大表 join 小表
- 解决问题: 解决大小表join的性能优化, 同时解决数据的倾斜
- 普通mapjoin 对 表类型没有要求
*执行:
set hive.auto.convert.join=true; --开启map join 默认为true
set hive.auto.convert.join.noconditionaltask.size=20971520 (20M) – 如何判断是否为小表
也可以通过调整map和reduce的数量
Bucket-MapJoin:
*适用场景: 大表 join 中型表
*使用条件:
- 两个表必须是桶表: set hive.enforce.bucketing=true;
2. 分桶字段必须是两个表join的字段
3. 两个表的分桶数量必须倍数
4. 开启 bucket map join : set hive.optimize.bucketmapjoin = true;
SMB join(重点,面试闪光点):
*适用场景: 大表与大表join(空KEY过滤,
空key转换(表a中key为空的字段赋一个随机的值,使得数据随机均匀地分不到不同的reducer上))
*使用条件:
1. 两个表必须是桶表: set hive.enforce.bucketing=true;
2. 分桶字段必须是两个表join的字段 , 同时要保证按照分桶字段进行排序
3. 两个表的分桶数量必须一致
4. 开启 bucket map join: set hive.optimize.bucketmapjoin = true;
5. 额外开启以下四项内容:
set hive.enforce.sorting=true; (强制要求排序)
开启SMB join:
set hive.auto.convert.sortmerge.join=true;
set hive.auto.convert.sortmerge.join.noconditionaltask=true;
set hive.optimize.bucketmapjoin.sortedmerge = true; (自动尝试是否可以使用SMB join)
SQL优化:
1.可以只读取查询中所需要用到的列,而忽略其他列
2.分区裁剪:可以在查询的过程中减少不必要的分区
3.Group By:先在Map端进行部分聚合,最后在Reduce端得出最终结果
动态分区调整
开启功能
set hive.exec.dynamic.partition=true;
设置为非严格模式
set hive.exec.dynamic.partition.mode=nonstrict;
在所有执行MR的节点上,最大一共可以创建多少个动态分区。
set hive.exec.max.dynamic.partitions=1000;
数据倾斜(hive优化)
原因:
1.key分布不均匀
2.业务数据本身的特性
3.建表时考虑不周
4.某些SQL语句本身就有数据倾斜