环境方面
可以修改相关服务器的配置、容器的配置、环境搭建
explain 和 explain extended
explain : 只有对hql语句的解释。
explain extended:对hql语句的解释,以及抽象表达式树的生成。
stage
相当于一个job
,一个stage可以是limit、也可以是一个子查询、也可以是group by等。
hive
默认一次只执行一个stage
,但是如果stage之间没有相互依赖,将可以并行执行。
任务越复杂,hql
代码越复杂,stage
越多,运行的时间一般越长。
explain select * from emptest1;
结果
Explain |
---|
STAGE DEPENDENCIES: |
Stage-0 is a root stage |
STAGE PLANS: |
Stage: Stage-0 |
Fetch Operator |
limit: -1 |
Processor Tree: |
TableScan |
alias: emptest1 |
Statistics: Num rows: 4 Data size: 192 Basic stats: COMPLETE Column stats: NONE |
Select Operator |
expressions: empno (type: int), ename (type: string), job (type: string), mgr (type: int), hiredate (type: date), sal (type: double), comm (type: double), deptno (type: int) |
outputColumnNames: _col0, _col1, _col2, _col3, _col4, _col5, _col6, _col7 |
Statistics: Num rows: 4 Data size: 192 Basic stats: COMPLETE Column stats: NONE |
ListSink |
join
hive的查询永远是小表(结果集)驱动大表(结果集)
hive中的on
的条件只能是等值连接
注意hive是否配置普通join转换成map端join、以及mapjoin小表文件大小的阀值
新版本是默认开启的,老版本才需要考虑这些问题
hive> set hive.mapjoin.smalltable.filesize;
hive.mapjoin.smalltable.filesize=25000000
hive> set hive.auto.convert.join;
hive.auto.convert.join=true
limit的优化
hive.limit.row.max.size=100000
hive.limit.optimize.limit.file=10
hive.limit.optimize.enable=false (如果limit较多时建议开启) hive.limit.optimize.fetch.max=50000
本地模式
hive.exec.mode.local.auto=false (建议打开)
hive.exec.mode.local.auto.inputbytes.max=134217728
hive.exec.mode.local.auto.input.files.max=4
并行执行:
hive.exec.parallel=false (建议开启)
hive.exec.parallel.thread.number=8
严格模式:
严格模式下用order by
之类的就要求搭配limit使用,否则会报错,因为order by全局排序,只有一个reducer
,数据量过大时运行时间会特别长.
hive.mapred.mode=nonstrict
mapper和reducer的个数:
不是mapper和redcuer个数越多越好,也不是越少越好。
将小文件合并处理(将输入类设置为:CombineTextInputFormat) 通过配置将小文件合并:
mapred.max.split.size=256000000
mapred.min.split.size.per.node=1
mapred.min.split.size.per.rack=1
hive.input.format=org.apache.hadoop.hive.ql.io.CombineHiveInputFormat
set mapreduce.job.reduces=10
hive.exec.reducers.max=1009
配置jvm重用:
set mapreduce.job.jvm.numtasks=1
set mapred.job.reuse.jvm.num.tasks=1
数据倾斜:
数据倾斜:由于key分布不均匀造成的数据向一个方向偏离的现象。
本身数据就倾斜
join语句容易造成
count(distinct col) 很容易造成倾斜
group by 也可能会造成
找到造成数据倾斜的key,然后再通过hql语句避免。
hive.map.aggr=true
hive.groupby.skewindata=false (建议开启)
hive.optimize.skewjoin=false
Whether to enable skew join optimization.
The algorithm is as follows: At runtime, detect the keys with a large skew. Instead of
processing those keys, store them temporarily in an HDFS directory. In a follow-up map-reduce
job, process those skewed keys. The same key need not be skewed for all the tables, and so,
the follow-up map-reduce job (for the skewed keys) would be much faster, since it would be a
map-join.
索引是一种hive的优化:
想要索引在查询时生效,还得设置使用索引:默认是不使用的。
SET hive.input.format=org.apache.hadoop.hive.ql.io.HiveInputFormat;
SET hive.optimize.index.filter=true;
SET hive.optimize.index.filter.compact.minsize=0;
分区本身就是hive的一种优化
job的数量:
一般是一个查询产生一个job,然后通常情况一个job、可以是一个子查询、一个join、一个group by 、一个limit等一些操作。