1. 使用 explain 查看执行计划
explain extended select count(distinct(name)) from employees;
2. 调整limit 相关参数
一条查询语句如果有limit 限制, 他也会扫描整个表
3. 严格模式
修改hive-site.xml
<property>
<name>hive.mapred.mode</name>
<value>strict</value>
<description>Deprecated; use hive.strict.checks.* settings instead.</description>
</property>
效果:
1.where 的谓词中必须包含分区字段
select * from employees where country = "CHINA"
2. 有order by 的时候必须加limit, order by 最后会在一个reducer 上执行, 防止reducer 执行时间过长
select * from employees where country = "CHINA" order by name limit 3;
3. 限制笛卡尔集 上使用where 过滤
4. 并行执行
hive 查询会被分成若干阶段: MapReduce阶段, 抽样阶段, 合并阶段, limit 阶段。hive 每次执行一个阶段, 如果这些阶段并非完全依赖,这样可以并行执行
修改: hive-site.xml
<property>
<name>hive.exec.parallel</name>
<value>true</value>
<description>Whether to execute jobs in parallel</description>
</property>
5. 调整reducer 和 map 的个数
6. 若干常量用来调试定位异常数据位置,称为虚拟列
select INPUT__FILE__NAME, BLOCK__OFFSET__INSIDE__FILE, ROW__OFFSET__INSIDE__BLOCK from employees where country = "CHINA" and name = 'John Doe';
INPUT__FILE__NAME, BLOCK__OFFSET__INSIDE__FILE, ROW__OFFSET__INSIDE__BLOCK 都是虚拟列。