GaussDB 实验篇+openGauss的4种1级分区案例

✔ 范围分区/range分区

sql 复制代码
-- 创建表
drop table if exists zzt.par_range;
create table if not exists zzt.par_range (
    empno integer,
    ename char(10),
    job char(9),
    mgr integer(4),
    hiredate date,
    sal numeric(7,2),
    comm numeric(7,2),
    deptno integer,
    constraint pk_par_emp primary key (empno,deptno)
) tablespace ts_zzt
partition by range(deptno) --分区键必须在主键中
(
PARTITION P1 VALUES LESS THAN(11),
PARTITION P2 VALUES LESS THAN(21),
PARTITION P3 VALUES LESS THAN(31)
)
ENABLE ROW MOVEMENT;
-- 插入数据
insert into zzt.par_range select * from zzt.emp;
-- 查验
select * from zzt.par_range partition(p1);
select * from pg_catalog.pg_partition p order by p.parentid,p.relfilenode;

✔ 间隔分区/interval分区

sql 复制代码
-- 创建表
drop table if exists zzt.par_inter;
create table if not exists zzt.par_inter 
  ( id        serial, 
    time      DATE,
    others   varchar(20)
  ) 
PARTITION BY RANGE (time) 
INTERVAL('1 day')
  ( PARTITION p1 VALUES LESS THAN (TO_DATE('2021-1-2', 'YYYY-MM-DD')),
    PARTITION p2 VALUES LESS THAN (TO_DATE('2021-1-3', 'YYYY-MM-DD')),
    PARTITION p3 VALUES LESS THAN (TO_DATE('2021-1-4', 'YYYY-MM-DD')));
-- 插入数据
insert into zzt.par_inter values(1,to_date('2021-01-01 10:00:00','yyyy-mm-dd hh24:mi:ss'),'a'); --由于是less than所以分区大1天
insert into zzt.par_inter values(2,to_date('2021-01-20 20:00:00','yyyy-mm-dd hh24:mi:ss'),'a'); --测试自动分区的跨天能力
insert into zzt.par_inter values(3,to_date('2021-01-20 10:00:00','yyyy-mm-dd hh24:mi:ss'),'a'); --测试自动分区的跨天能力
insert into zzt.par_inter values(4,to_date('2022-02-02 10:00:00','yyyy-mm-dd hh24:mi:ss'),'a'); --测试自动分区的跨年能力
insert into zzt.par_inter values(5,to_date('2023-03-03 10:00:00','yyyy-mm-dd hh24:mi:ss'),'a'); --测试自动分区的跨年能力
commit;
-- 查验
select * from zzt.par_inter partition(p1);
select * from pg_catalog.pg_partition p order by p.parentid,p.relfilenode;

✔ 哈希分区/hash分区

sql 复制代码
-- 创建表
drop table if exists zzt.par_hash;
create table if not exists zzt.par_hash (
    empno integer,
    ename char(10),
    job char(9),
    mgr integer(4),
    hiredate date,
    sal numeric(7,2),
    comm numeric(7,2),
    deptno integer
) tablespace ts_zzt
partition by hash(deptno)
(
PARTITION p1,
PARTITION p2,
PARTITION p3
);
-- 插入数据
insert into zzt.par_hash select * from zzt.emp;
-- 查验
select * from zzt.par_hash partition(p1);
select * from pg_catalog.pg_partition p order by p.parentid,p.relfilenode;

✔ 列表分区/list分区

sql 复制代码
-- 创建表
drop table if exists zzt.par_list;
create table if not exists zzt.par_list (
    empno integer,
    ename char(10),
    job char(9),
    mgr integer(4),
    hiredate date,
    sal numeric(7,2),
    comm numeric(7,2),
    deptno integer
) tablespace ts_zzt
partition by list(deptno)
(
PARTITION p1 values(10),
PARTITION p2 values(20),
PARTITION p3 values(30)
);
-- 插入数据
insert into zzt.par_list select * from zzt.emp;
-- 查验
select * from zzt.par_list partition(p1);
select * from pg_catalog.pg_partition p order by p.parentid,p.relfilenode;

※ 如果您觉得文章写的还不错, 别忘了在文末给作者点个赞哦 ~

相关推荐
三次握手四次挥手5 天前
GaussDB使用指南
gaussdb
robinson19886 天前
PostGreSQL/openGauss表膨胀处理
opengauss·表膨胀·postgres sql·vacuum full·不生效
Gauss松鼠会7 天前
GaussDB回调机制深度实践:从事件驱动到系统集成
开发语言·javascript·数据库·sql·gaussdb
Gauss松鼠会8 天前
GaussDB Plan Hint调优实战:从执行计划控制到性能优化
数据库·sql·性能优化·database·gaussdb
Gauss松鼠会9 天前
GaussDB性能调优:从根因分析到优化落地
数据库·性能优化·database·gaussdb
Mr.洛 白13 天前
OpenEuler/CentOS一键部署OpenGauss数据库教程(脚本+视频)
数据库·opengauss·gaussdb·国产数据库安装·安装脚本
爱的叹息13 天前
华为高斯(GaussDB)数据库中 Range、List、Hash三种分区方式 的完整SQL示例及增删改查操作,并附上总结对比表格
数据库·哈希算法·gaussdb
爱的叹息16 天前
华为高斯(GaussDB) 集中式数据库 的开发技术手册,涵盖核心功能、开发流程、优化技巧及常见问题解决方案
数据库·gaussdb
runfarther17 天前
华为GaussDB数据库的手动备份与还原操作介绍
gaussdb
笑远23 天前
GaussDB 内存结构详解
数据库·gaussdb