数据库环境
openGauss:2.0.0 - 数据库实训平台
学习目标
分区表是把逻辑上的一张表根据某种方案分成几张物理块进行存储,这张逻辑上的表称之为分区表,物理块称之为分区。分区表是一张逻辑表,不存储数据,数据实际是存储在分区上的。
学习笔记
- 创建分区表(范围分区表)
omm=# create table update_table
omm-# (
omm(# c1 int,
omm(#
omm-# c2 CHAR(2)
omm(# )partition by range (c1)
omm-# (
omm(# partition update_table_p0 values less than (50),
omm(# partition update_table_p1 values less than (100),
omm(# partition update_table_p2 values less than (150)
omm(# );
复制
- 查看分区表信息
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;
复制
课后作业
1.创建一个含有5个分区的范围分区表store,在每个分区中插入记录
omm=# create table store(store_id integer,store_name char(30))
partition by range(store_id)(
partition store_id_60 values less than(60),
partition store_id_70 values less than(70),
partition store_id_80 values less than(80),
partition store_id_90 values less than(90),
partition store_id_95 values less than(95));
CREATE TABLE
omm=# \d+ store;
Table "public.store"
Column | Type | Modifiers | Storage | Stats target | Description
------------+---------------+-----------+----------+--------------+-------------
store_id | integer | | plain | |
store_name | character(30) | | extended | |
Range partition by(store_id)
Number of partition: 5 (View pg_partition to check each partition range.)
Has OIDs: no
Options: orientation=row, compression=no
omm=# insert into store values (59,'lily'),(66,'rose'),(75,'tom'),(84,'jerry'),(94,'tim');
INSERT 0 5
omm=# select * from store;
store_id | store_name
----------+--------------------------------
59 | lily
66 | rose
75 | tom
84 | jerry
94 | tim
(5 rows)
复制
2.查看分区1上的数据
omm=# select * from store partition(store_id_60);
store_id | store_name
----------+--------------------------------
59 | lily
(1 row)
复制
3.重命名分区2
omm=# alter table store rename partition store_id_60 to store_id_60_new;
ALTER TABLE
omm=# select relname from pg_partition;
relname
-----------------
store
store_id_70
store_id_80
store_id_90
store_id_95
store_id_60_new
(6 rows)
复制
4.删除分区5
omm=# alter table store drop partition store_id_95;
ALTER TABLE
omm=# select relname from pg_partition;
relname
-----------------
store
store_id_70
store_id_80
store_id_90
store_id_60_new
(5 rows)
复制
5.增加分区6
omm=# alter table store add partition store_id_100 values less than(100);
ALTER TABLE
omm=# select relname from pg_partition;
relname
-----------------
store
store_id_70
store_id_80
store_id_90
store_id_60_new
store_id_100
(6 rows)
omm=# \d+ store
Table "public.store"
Column | Type | Modifiers | Storage | Stats target | Description
------------+---------------+-----------+----------+--------------+-------------
store_id | integer | | plain | |
store_name | character(30) | | extended | |
Range partition by(store_id)
Number of partition: 5 (View pg_partition to check each partition range.)
Has OIDs: no
Options: orientation=row, compression=no
复制
6.在系统表pg_partition中查看分区信息
omm=# select * from pg_partition;
relname | parttype | parentid | rangenum | intervalnum | partstrategy | relfilenode | reltablespace | relpages | reltuples | relallvisible | reltoast
relid | reltoastidxid | indextblid | indisusable | reldeltarelid | reldeltaidx | relcudescrelid | relcudescidx | relfrozenxid | intspnum | partkey | intervalt
ablespace | interval | boundaries | transit | reloptions | relfrozenxid64
-----------------+----------+----------+----------+-------------+--------------+-------------+---------------+----------+-----------+---------------+---------
------+---------------+------------+-------------+---------------+-------------+----------------+--------------+--------------+----------+---------+----------
----------+----------+------------+---------+---------------------------------------------------+----------------
store | r | 16404 | 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_id_70 | p | 16404 | 0 | 0 | r | 16409 | 0 | 0 | 0 | 0 |
0 | 0 | 0 | t | 0 | 0 | 0 | 0 | 9327 | | |
| | {70} | | {orientation=row,compression=no} | 9327
store_id_80 | p | 16404 | 0 | 0 | r | 16410 | 0 | 0 | 0 | 0 |
0 | 0 | 0 | t | 0 | 0 | 0 | 0 | 9327 | | |
store_id_90 | p | 16404 | 0 | 0 | r | 16411 | 0 | 0 | 0 | 0 |
0 | 0 | 0 | t | 0 | 0 | 0 | 0 | 9327 | | |
| | {80} | | {orientation=row,compression=no} | 9327
| | {90} | | {orientation=row,compression=no} | 9327
store_id_60_new | p | 16404 | 0 | 0 | r | 16408 | 0 | 0 | 0 | 0 |
0 | 0 | 0 | t | 0 | 0 | 0 | 0 | 9327 | | |
| | {60} | | {orientation=row,compression=no} | 9327
store_id_100 | p | 16404 | 0 | 0 | r | 16413 | 0 | 0 | 0 | 0 |
0 | 0 | 0 | t | 0 | 0 | 0 | 0 | 9332 | | |
| | {100} | | {orientation=row,compression=no} | 9332
(6 rows)
复制
7.删除分区表
omm=# drop table store;
DROP TABLE
omm=# select * from pg_partition;
relname | parttype | parentid | rangenum | intervalnum | partstrategy | relfilenode | reltablespace | relpages | reltuples | relallvisible | reltoastrelid | reltoastidxid | indextblid | indisusable | reldeltarelid | reldeltaidx | relcudescrelid | relcudescidx | relfrozenxid | intspnum | partkey | intervaltablespace | interval | boundaries | transit | reloptions | relfrozenxid64
---------+----------+----------+----------+-------------+--------------+-------------+---------------+----------+-----------+---------------+---------------+---------------+------------+-------------+---------------+-------------+----------------+--------------+--------------+----------+---------+--------------------+----------+------------+---------+------------+----------------
(0 rows)
复制
学习资源
- openGauss SQL学习参考资料
- 每日一练:openGauss数据库在线实训课程
- openGauss每日一练 | 21期养成好习惯,提升技术能力!
- 墨天轮Markdown编辑器使用介绍
- 墨天轮数据库在线实训平台V1.0操作手册
- 墨天轮数据社区
欢迎各位同学一起来交流学习心得!
最后修改时间:2021-12-16 18:37:38
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
评论
相关阅读
openGauss荣获中国软件行业协会多奖项,技术升级再创行业新高度
openGauss
456次阅读
2025-04-30 14:30:58
MogDB 发布更新,解决 openGauss 数据库在长事务情况下Ustore表膨胀问题
MogDB
300次阅读
2025-04-17 10:41:41
MogDB 发布更新,解决 openGauss 数据库在长事务情况下Ustore表膨胀问题
云和恩墨
198次阅读
2025-04-16 09:52:02
GitCode 成 openGauss 新归宿,国产开源数据库里程碑事件
严少安
155次阅读
2025-04-27 11:37:53
荣誉时刻!openGauss认证证书快递已发,快来看看谁榜上有名!
墨天轮小教习
150次阅读
2025-04-23 17:39:13
单个执行机并行执行MySQL到openGauss数据迁移子任务
Clipnosis
129次阅读
2025-04-30 16:39:58
openGauss6.0.0适配操作系统自带的软件,不依赖三方库
来杯拿铁
90次阅读
2025-04-18 10:49:53
opengauss使用gs_probackup进行增量备份恢复
进击的CJR
85次阅读
2025-04-09 16:11:58
Postgresql数据库单个Page最多存储多少行数据
maozicb
79次阅读
2025-04-23 16:02:19
openGauss新特性 | openGauss-DataVec向量数据库特性介绍
openGauss
49次阅读
2025-04-17 10:41:47