学习目标
学习openGauss分区表
分区表是把逻辑上的一张表根据某种方案分成几张物理块进行存储,这张逻辑上的表称之为分区表,物理块称之为分区。
分区表是一张逻辑表,不存储数据,数据实际是存储在分区上的。
常见的分区方案有范围分区(Range Partitioning)、间隔分区(Interval Partitioning)、哈希分区(Hash Partitioning)、列表分区(List Partitioning)、数值分区(Value Partition)等。
目前行存表支持范围分区、间隔分区、哈希分区、列表分区,列存表仅支持范围分区。
间隔分区
间隔分区是一种特殊的范围分区,相比范围分区,新增间隔值定义,当插入记录找不到匹配的分区时,可以根据间隔值自动创建分区。
间隔分区只支持基于表的一列分区,并且该列只支持TIMESTAMP[§] [WITHOUT TIME ZONE]、TIMESTAMP[§] [WITH TIME ZONE]、DATE数据类型。
间隔分区策略:根据分区键值将记录映射到已创建的某个分区上,如果可以映射到已创建的某一分区上,则把记录插入到对应的分区上,否则根据分区键值和表定义信息自动创建一个分区,然后将记录插入新分区中,新创建的分区数据范围等于间隔值。
注意事项
唯一约束和主键约束的约束键包含所有分区键将为约束创建 LOCAL 索引,否则创建 GLOBAL 索引。
目前哈希分区和列表分区仅支持单列构建分区键,暂不支持多列构建分区键。
只需要有间隔分区表的INSERT权限,往该表INSERT数据时就可以自动创建分区。
课程学习
👉openGauss CREATE TABLE PARTITION语法
CREATE TABLE [ IF NOT EXISTS ] partition_table_name
( [
{ column_name data_type [ COLLATE collation ] [ column_constraint [ ... ] ]
| table_constraint
| LIKE source_table [ like_option [...] ] }[, ... ]
] )
[ WITH ( {storage_parameter = value} [, ... ] ) ]
[ COMPRESS | NOCOMPRESS ]
[ TABLESPACE tablespace_name ]
PARTITION BY {
{RANGE (partition_key) [ INTERVAL ('interval_expr') [ STORE IN (tablespace_name [, ... ] ) ] ] ( partition_less_than_item [, ... ] )} |
{RANGE (partition_key) [ INTERVAL ('interval_expr') [ STORE IN (tablespace_name [, ... ] ) ] ] ( partition_start_end_item [, ... ] )} |
{LIST | HASH (partition_key) (PARTITION partition_name [VALUES (list_values_clause)] opt_table_space )}
} [ { ENABLE | DISABLE } ROW MOVEMENT ];
复制
连接openGauss
root@modb:~# su - omm
omm@modb:~$ gsql -r
复制
1.创建分区表
––范围分区表
create table update_table ( c1 int, c2 CHAR(2) )
partition by range (c1)
(
partition update_table_p0 values less than (50),
partition update_table_p1 values less than (100),
partition update_table_p2 values less than (150)
);
-–查看分区表信息
omm=# \d+ update_table;
Table "public.update_table"
Column | Type | Modifiers | Storage | Stats target | Description
--------+--------------+-----------+----------+--------------+-------------
c1 | integer | | plain | |
c2 | character(2) | | extended | |
Range partition by(c1)
Number of partition: 3 (View pg_partition to check each partition range.)
Has OIDs: no
Options: orientation=row, compression=no
omm=# select * from pg_partition; --pg 默认视图 pg_partitioned_table
relname | parttype | parentid | rangenum | intervalnum | partstrategy | relfilenode | reltablespace | relpages | reltuples | relallvisible | reltoastrelid |
reltoastidxid | indextblid | indisusable | reldeltarelid | reldeltaidx | relcudescrelid | relcudescidx | relfrozenxid | intspnum | partkey | intervaltablespace | in
terval | boundaries | transit | reloptions | relfrozenxid64
-----------------+----------+----------+----------+-------------+--------------+-------------+---------------+----------+-----------+---------------+---------------+
---------------+------------+-------------+---------------+-------------+----------------+--------------+--------------+----------+---------+--------------------+---
-------+------------+---------+---------------------------------------------------+----------------
update_table | r | 16389 | 0 | 0 | r | 0 | 0 | 0 | 0 | 0 | 0 |
0 | 0 | t | 0 | 0 | 0 | 0 | 0 | | 1 | |
| | | {orientation=row,compression=no,wait_clean_gpi=n} | 0
update_table_p0 | p | 16389 | 0 | 0 | r | 16393 | 0 | 0 | 0 | 0 | 0 |
0 | 0 | t | 0 | 0 | 0 | 0 | 9034 | | | |
| {50} | | {orientation=row,compression=no} | 9034
update_table_p1 | p | 16389 | 0 | 0 | r | 16394 | 0 | 0 | 0 | 0 | 0 |
0 | 0 | t | 0 | 0 | 0 | 0 | 9034 | | | |
| {100} | | {orientation=row,compression=no} | 9034
update_table_p2 | p | 16389 | 0 | 0 | r | 16395 | 0 | 0 | 0 | 0 | 0 |
0 | 0 | t | 0 | 0 | 0 | 0 | 9034 | | | |
| {150} | | {orientation=row,compression=no} | 9034
(4 rows)
omm=#
––插入数据
omm=# insert into update_table values (1, 'a'), (50, 'b'), (100, 'c');
INSERT 0 3
omm=#
-–超出范围的数据会报错
omm=# insert into update_table values (150, 'd');
ERROR: inserted partition key does not map to any table partition
omm=#
-–查看各区上的数据
omm=# select * from update_table partition(update_table_p0);
c1 | c2
----+----
1 | a
(1 row)
omm=#
复制
2.创建分区
omm=# alter table update_table add partition update_table_p3 values less than (200);
ALTER TABLE
omm=# insert into update_table values (150, 'd');
INSERT 0 1
omm=#
复制
3.修改分区属性
omm=# alter table update_table rename partition update_table_p1 to update_table_p1_1;
ALTER TABLE
omm=#
复制
4.删除分区
omm=# alter table update_table drop partition update_table_p0;
ALTER TABLE
omm=#
复制
5.删除分区表
omm=# drop table update_table;
DROP TABLE
omm=#
复制
课后作业
1.创建一个含有5个分区的范围分区表 store,在每个分区中插入记录
create table store ( c1 int, c2 CHAR(2) )
partition by range (c1)
(
partition store_p0 values less than (50),
partition store_p1 values less than (100),
partition store_p2 values less than (150),
partition store_p3 values less than (200),
partition store_p4 values less than (250)
);
insert into store values(1,'p0');
insert into store values(51,'p1');
insert into store values(101,'p2');
insert into store values(151,'p3');
insert into store values(201,'p4');
复制
2.查看分区1上的数据
omm=# select * from store;
c1 | c2
-----+----
1 | p0
51 | p1
101 | p2
151 | p3
201 | p4
(5 rows)
omm=# select * from store partition(store_p0);
c1 | c2
----+----
1 | p0
(1 row)
复制
3.重命名分区2
omm=# alter table store rename partition store_p1 to store_p1_1;
ALTER TABLE
omm=#
复制
4.删除分区5
omm=# alter table store drop partition store_p4;
ALTER TABLE
复制
5.增加分区6
omm=# alter table store add partition store_p5 values less than (500);
ALTER TABLE
omm=#
复制
6.在系统表 pg_partition 中查看分区信息
mm=# select * from pg_partition;
relname | parttype | parentid | rangenum | intervalnum | partstrategy | relfilenode | reltablespace | relpages | reltuples | relallvisible | reltoastrelid | relt
oastidxid | indextblid | indisusable | reldeltarelid | reldeltaidx | relcudescrelid | relcudescidx | relfrozenxid | intspnum | partkey | intervaltablespace | interva
l | boundaries | transit | reloptions | relfrozenxid64
------------+----------+----------+----------+-------------+--------------+-------------+---------------+----------+-----------+---------------+---------------+-----
----------+------------+-------------+---------------+-------------+----------------+--------------+--------------+----------+---------+--------------------+--------
--+------------+---------+---------------------------------------------------+----------------
store | r | 16397 | 0 | 0 | r | 0 | 0 | 0 | 0 | 0 | 0 |
0 | 0 | t | 0 | 0 | 0 | 0 | 0 | | 1 | |
| | | {orientation=row,compression=no,wait_clean_gpi=n} | 0
store_p0 | p | 16397 | 0 | 0 | r | 16401 | 0 | 0 | 0 | 0 | 0 |
0 | 0 | t | 0 | 0 | 0 | 0 | 9326 | | | |
| {50} | | {orientation=row,compression=no} | 9326
store_p2 | p | 16397 | 0 | 0 | r | 16403 | 0 | 0 | 0 | 0 | 0 |
0 | 0 | t | 0 | 0 | 0 | 0 | 9326 | | | |
| {150} | | {orientation=row,compression=no} | 9326
store_p3 | p | 16397 | 0 | 0 | r | 16404 | 0 | 0 | 0 | 0 | 0 |
0 | 0 | t | 0 | 0 | 0 | 0 | 9326 | | | |
| {200} | | {orientation=row,compression=no} | 9326
store_p1_1 | p | 16397 | 0 | 0 | r | 16402 | 0 | 0 | 0 | 0 | 0 |
0 | 0 | t | 0 | 0 | 0 | 0 | 9326 | | | |
| {100} | | {orientation=row,compression=no} | 9326
store_p5 | p | 16397 | 0 | 0 | r | 16406 | 0 | 0 | 0 | 0 | 0 |
0 | 0 | t | 0 | 0 | 0 | 0 | 9335 | | | |
| {500} | | {orientation=row,compression=no} | 9335
(6 rows)
omm=#
复制
7.删除分区表
omm=# drop table store;
DROP TABLE
omm=#
复制
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
评论
目录