Greenplum 分区表介绍

来源:互联网 发布:网络言情小说作家 编辑:程序博客网 时间:2024/05/21 15:03
Greenplum 分区表
Greenplum分区表的原理和postgresql的原理相同,都是把一张大表进行按照适合的维度进行分割,通过表的继承,规则,约束实现的。
在Greenplum中,一个表是否分区表保存在pg_partition中.

GP在建表的时候会有一个DISTRIBUTED BY选项,这个是表进行物理拆分,理解一下分区和分布:
1.分区,按照字段逻辑进行逻辑划分的区域,比如,时间按天,按月等等
2.分布,按照字段进行物理分区,会分散到每个segment
分布式为了并行查询效率,充分利用每个segment节点的资源,分区是为了减少查询时的数据扫描,对大表维护更加方便。

支持的分区类型:
范围分区
列表分区
组合分区

实例:
create table test_partition (id int,sex varchar(1)) 
distributed  by (id) 
partition by  list(sex)
(partition man  values ('M'),
 partition woman values ('W'),
 default partition other);

NOTICE:  CREATE TABLE will create partition "test_partition_1_prt_man" for table "test_partition"
NOTICE:  CREATE TABLE will create partition "test_partition_1_prt_woman" for table "test_partition"
NOTICE:  CREATE TABLE will create partition "test_partition_1_prt_other" for table "test_partition"

insert into test_partition values(1,'M');
insert into test_partition values(2,'M'); 
insert into test_partition values(3,'M'); 
insert into test_partition values(4,'W');
insert into test_partition values(5,'W'); 
insert into test_partition values(6,'W'); 
insert into test_partition values(7,'W'); 
insert into test_partition values(8,'O');
insert into test_partition values(9,'O');  



SELECT gp_segment_id,count(*) from test_partition group  by 1; 
 gp_segment_id | count 
---------------+-------
             8 |     1
            62 |     1
            47 |     1
            57 |     1
            52 |     1
            13 |     1
             3 |     1
            42 |     1
            18 |     1

可见分布在不同的segment

查看分区:
gpadmin=# select * from test_partition_1_prt_man;
 id | sex 
----+-----
  1 | M
  2 | M
  3 | M
(3 rows)

gpadmin=# select * from test_partition_1_prt_woman;
 id | sex 
----+-----
  7 | W
  4 | W
  5 | W
  6 | W
(4 rows)

gpadmin=# select * from test_partition_1_prt_other;
 id | sex 
----+-----
  8 | O
  9 | O
(2 rows)

参考:
http://gpdb.docs.pivotal.io/500/admin_guide/ddl/ddl-partition.html#topic_tvx_nsz_bt
可见分区表存储了相关分区的记录
原创粉丝点击