Hive优化(调优)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/liminghui4321/article/details/102763110

hive调优

参数调优
1、map阶段数据压缩
set hive.exec.compress.intermediate=true
set mapred.map.output.compression.codec= org.apache.hadoop.io.compress.SnappyCodec
set mapred.map.output.compression.codec=com.hadoop.compression.lzo.LzoCodec;
2、对最终生成的hive表数据压缩
1、通过参数设置
set hive.exec.compress.output=true
set mapred.output.compression.codec=org.apache.hadoop.io.compress.SnappyCodec
2、通过建表语句设置
create table tablename (
xxx,string
xxx, bigint
)
ROW FORMAT DELTMITED FIELDS TERMINATED BY ‘\t’
STORED AS orc tblproperties(“orc.compress” = “SNAPPY”)

***

3、分区(分区表相当于hive的索引,加快查询速度)


***4、分桶(两个表join的时候,如果两个表在相同列上有分桶操作,会减少join数据量【要求两个表的桶数量要相同或成倍数】)***
***5、并行计算,stage不依赖的时候可以并行计算***
	// 开启任务并行执行
	set hive.exec.parallel=true;
	 // 同一个sql允许并行任务的最大线程数 
	set hive.exec.parallel.thread.number=8;
***6、JVM重用***
	JVM重用对hive的性能具有非常大的 影响,特别是对于很难避免小文件的场景或者task特别多的场景,这类场景大多数执行时间都很短。jvm的启动过程可能会造成相当大的开销,尤其是执行的job包含有成千上万个task任务的情况。
	set mapred.job.reuse.jvm.num.tasks=10;
***7、调整reduce的个数***
	第一种方法:根据数据量调整reduce个数  hive.exec.reducers.bytes.per.reducer 【设置每个reduce处理的数据量,默认256M】
	第二种方法: 直接设置reduce的个数  set mapred.reduce.tasks = number
***8、推测执行***
	mapred.map.tasks.speculative.execution
	mapred.reduce.tasks.speculative.execution
***9、小文件合并***
	set hive.merge.mapfiles = true                   ##在 map only 的任务结束时合并小文件
	set hive.merge.mapredfiles = false               ## true 时在 MapReduce 的任务结束时合并小文件
	set hive.merge.size.per.task = 256*1000*1000     ##合并文件的大小
	set mapred.max.split.size=256000000;             ##每个 Map 最大分割大小
	set mapred.min.split.size.per.node=1;            ##一个节点上 split 的最少值
	set hive.input.format=org.apache.hadoop.hive.ql.io.CombineHiveInputFormat;    ##执行Map前进行小文件合并

SQL调优

**1、where语句优化**
	select m.cid,u.id from order m join customer u on( m.cid =u.id )where m.dt='20180808';
	可优化为
	select m.cid,u.id from (select * from order where dt='20180818') m join customer u on( m.cid =u.id);
**2、union优化**
	尽量不要使用union (union 去掉重复的记录)而是使用 union all 然后在用group by 去重
**3、count distinct优化**
	不要使用count (distinct   cloumn) ,而要使用子查询实现count(distinct)
	select count(1) from (select id from tablename group by id) tmp;
**4、如果需要根据一张表的字段约束另一个张表,用in代替join**
	select id,name from tb1  a join tb2 b on(a.id = b.id);
	可优化为
	select id,name from tb1 where id in(select id from tb2); in 要比join 快
**5、消灭子查询内的 group by 、 COUNT(DISTINCT),MAX,MIN。 可以减少job的数量。**
**6、join优化**
	map端join
	set hive.auto.convert.join = true; 默认为true
	set hive.mapjoin.smalltable.filesize=25000000; 设置小表的阈值
	
**7、本地模式**
	当 Hive 查询处理的数据量比较小时,其实没有必要启动分布式模式去执行,因为以分布式方式执行就涉及到跨网络传输、多节点协调 等,并且消耗资源。这个时间可以只使用本地模式来执行 mapreduce job,只在一台机器上执行,速度会很快
	set hive.exec.mode.local.auto=true 是打开 hive 自动判断是否启动本地模式的开关,但是只 是打开这个参数并不能保证启动本地模式,要当 map 任务数不超过
        hive.exec.mode.local.auto.input.files.max 的个数并且 map 输入文件大小不超过
        hive.exec.mode.local.auto.inputbytes.max 所指定的大小时,才能启动本地模式。

猜你喜欢

转载自blog.csdn.net/liminghui4321/article/details/102763110