Hive分桶表&分区表
分区表
Hive存在的问题:hive中没有索引机制,每次查询的时候,hive会暴力扫描整张表.MySQL具有索引机制.
因为没有分区,所以hive搞了一个分区表的机制.在建表的时候,加上分区字段,然后会在表的目录下建立一个个的分区.如果按照天为分区,那么在表目录里就会有每天的目录.分区表的核心就是分目录.
分区表的建表
create table dept_partition(
deptno int, dname string, loc string
)
partitioned by (day string)
row format delimited fields terminated by '\t';
上面的语法中分区字段不能和分区表中的内容一样
分区表的加载:加载数据的时候要把分区字段名给指定
load data local inpath '/opt/module/hive/datas/dept_20200401.log' into table dept_partition partition(day='20200401');
分区表的查询
查询一个分区的方式:
select * from dept_partition where day = '20200401'
查询多个分区的方式一:
select * from dept_partition where day = '20200401' or day = '20200402';
方式二:
select * from dept_partition where day = '20200401'
union
select * from dept_partition where day = '20200402'