分区、分桶和索引

1.分区apache

分区是以字段的形式在表结构中存在,经过describe table命令能够查看到字段存在, 可是该字段不存放实际的数据内容,仅仅是分区的表示(伪列)。ide

(1)静态分区 create table if not exists sopdm.wyp2(id int,name string,tel string) partitioned by(age int) row format delimited fields terminated by ',' stored as textfile;oop

--overwrite是覆盖,into是追加 insert into table sopdm.wyp2 partition(age='25') select id,name,tel from sopdm.wyp;ui

(2)动态分区 --设置为true表示开启动态分区功能(默认为false) set hive.exec.dynamic.partition=true; --设置为nonstrict,表示容许全部分区都是动态的(默认为strict) set hive.exec.dynamic.partition.mode=nonstrict;orm

--insert overwrite是覆盖,insert into是追加 set hive.exec.dynamic.partition.mode=nonstrict; insert overwrite table sopdm.wyp2 partition(age) select id,name,tel,age from sopdm.wyp;索引

  1. 分桶

CREATE TABLE bucketed_user (id INT) name STRING) CLUSTERED BY (id) INTO 4 BUCKETS;hadoop

对于每个表(table)或者分区, Hive能够进一步组织成桶,也就是说桶是更为细粒度的数据范围划分。Hive也是 针对某一列进行桶的组织。Hive采用对列值哈希,而后除以桶的个数求余的方式决定该条记录存放在哪一个桶当中。 把表(或者分区)组织成桶(Bucket)有两个理由: (1)得到更高的查询处理效率。桶为表加上了额外的结构,Hive 在处理有些查询时能利用这个结构。具体而言,链接两个在(包含链接列的)相同列上划分了桶的表,能够使用 Map 端链接 (Map-side join)高效的实现。好比JOIN操做。对于JOIN操做两个表有一个相同的列,若是对这两个表都进行了桶操做。那么将保存相同列值的桶进行JOIN操做就能够,能够大大较少JOIN的数据量。 (2)使取样(sampling)更高效。在处理大规模数据集时,在开发和修改查询的阶段,若是能在数据集的一小部分数据上试运行查询,会带来不少方便。开发

3.索引string

索引能够加快含有group by语句的查询的计算速度it

create index employees_index on table employees(country) as 'org.apache.hadoop.hive.ql.index.compact.CompactIndexHandler' with deferred rebuild in table employees_index_table ;

相关文章
相关标签/搜索