1、尽早尽量过滤数据,减少每个阶段数据量
a.列裁剪:
hive在查询过程中,可以只读取查询中只需要的列,忽略其他列,从而避免全表扫描。
如:
select x.a, y.b from (select a from table1 where e < 10) x
left join
(select a,b from table2 where e< 10) y
on x.a = y.a
b.分区裁剪:
分区表需要卡分区使用
c.善用mult-insert:
对于同一个表从不同维度查询插入到不同表可以采用只扫描表一次的方法。
如:
from A insert overwrite table temp_a select col1, col2, … where 条件1
insert overwrite table temp_b select col1, col2, … where 条件2
d.数据过滤:
原始sql:
select x.a, y.b from A x left join B y on x.key = y.key
where x.dt = '2018-06-18' and y.dt = '2018-06-18' //(全部数据)
优化后的sql:
select x.a, y.b from
(select a, key from A where dt = '2018-06-18') x
left join
(select b, key from B where dt = '2018-06-18') y
on x.key = y.key
e.join优化
#join左边放小表减少OOM
#合理使用map join,当小表与大表关联时在map端进行join
如:
select /*+MAPJOIN(b)+*/ a.key, a.value from a join b on a.key = b.key
2、减少job数 a.join优化:
无论是外关联还是内关联,如果join的key相同,不管关联多少个表都是生成一个job,
所以当业务场景允许外,三个以及三个表以上关联的时候,尽量选择同一个key
b.union all优化:
原始sql:
select * from (select c1, c2 from A group by c1, c2)
union all
(select c1, c2 from B group by c1, c2) t; // --------(2个job)
优化后的sql:
select * from
(select c1, c2 from A
union all
select c1, c2 from B) t
group by c1, c2; // --------(1个job)
3.set hive.fortech.task.conversion=more
如果是简单查询,没有函数,orderby等,
语句会从hdfs直接查询不会转成mapreduce