无需MapReduce
在hive-default.xml中hive.fetch.task.conversion默认是more,老版本是minimal,该属性改为more后,在全局查找、字段查找、limit查找等都不走mapreduce。

Expects one of [none, minimal, more].
    Some select queries can be converted to single FETCH task minimizing latency.
    Currently the query should be single sourced not having any subquery and should not have
    any aggregations or distincts (which incurs RS), lateral views and joins.
    0. none : disable hive.fetch.task.conversion
    1. minimal : SELECT STAR, FILTER on partition columns, LIMIT only
    2. more    : SELECT, FILTER, LIMIT only (support TABLESAMPLE and virtual columns)

空Key过滤(非inner join)
方式:添加子查询where id is not null。
不适用情况:inner join会自动过滤。不需要字段为NULL的
空Key转换
通过如自定义函数等手段将空Key转换位其他散列数值。注意新Key与老Key要完全不重合,否则可能出现关联行为。数据倾斜 - 将Null分散到每个reducer,nvl(n.id, rand()) = o.id
数据倾斜
当出现数据倾斜的时候可以设置参数:set hive.groupby.skewindata = true,此时生成的查询计划会有两个MR Job。第一个MR Job增加Key,为了结果随机分布到Reduce中,每个Reduce做部分聚合并输出结果,达到负载均衡的目的;第二个MR Job在根据预处理的数据按照GROUP By Key分布到Reduce中(这个过程可以保证相容的Key被分布到同一个Reduce中),最后完成最终的聚合。
Count(Distinct)www.meimeitu8.com去重统计
此场景下只会有一个Reduce。优化方式:select count(*) from (select id from table group by id) t1;。原理是通过group by 去重,但此方法可以并行执行提高效率。
行列过滤
在关联查询前先通过子查询缩小表。如:
直接进行关联查询,则关联之后再做全局条件行裁剪。
select o.id from table b
join table o on o.id = b.id
where o.id < 10

先通过子查询,极大减少了关联结果
select b.id from table b
join (select id from table where id < 10) o
 on b.id = o.id

通过执行计划查看到,1、2两种方式在fetch数据时都带有filter条件,这是底层做了经典的谓词下推优化。关联条件与过滤条件相同会触发更深优化,如方式1中o,b两表都会触发谓词下推。SQL过于复杂可能导致方式1的自动优化失效,故推荐方式2.

笛卡尔积
一般生产环境开启此检查:set hive.strict.checks.cartesian.product=true