1)概念
Hive多表插入模式:根据某张表的查询结果向多张表插入数据。
2) 示例
hive (default)> from student0 --数据源表
insert overwrite table student1 partition(month='200807')
select id, name where month='200806' -- select子句1,id,name来自表student0
insert overwrite table student2 partition(month='200808')
select id, name, age where month='200807'; -- select子句2,id,name,age来自表student0
3) 总结
- 多个select子句的数据源都是student0,select子句后的字段都来自student0表中。因此,需要把查询的数据源表放在整个SQL的首行from student0。
- 多个select子句相对独立。
- 好处:
- 执行多次插入数据时,只对源数据表扫描一遍,就可以把不同的数据插入到不同的表中(或不同的分区中)。
- 类似于把多个SQL语句优化成一个SQL。减少表的扫描次数,从而减少Job中MapReduce的Stage数量,实现Hive SQL优化的目的。