创建范围分区表
# 创建分区表 主表
CREATE TABLE person_1
(
id SERIAL NOT NULL,
name VARCHAR NOT NULL,
begin_time TIMESTAMP,
end_time TIMESTAMP
) PARTITION BY RANGE (begin_time);
# 创建分区表 子表
create table person_1_2024_06_23
partition of person_1 for values from ('2024-06-23 00:00:00') to ('2024-06-24 00:00:00');
create table person_1_2024_06_24
partition of person_1 for values from ('2024-06-24 00:00:00') to ('2024-06-25 00:00:00');
# 往主表插入数据,PostgreSQL内部会自动管理数据插入的路由
insert into person_1 (id, "name", begin_time, end_time)
values(1,'张三','2024-06-23 01:00:00','2024-06-23 01:00:00'),
(2,'李四','2024-06-24 01:00:00','2024-06-24 01:00:00');
创建列表分区表
CREATE TABLE person_2
(
id SERIAL NOT NULL,
name VARCHAR NOT NULL,
begin_time TIMESTAMP,
end_time TIMESTAMP
) PARTITION BY LIST (name);
create table person_2_1
partition of person_2 for values in ('张三','李四');
create table person_2_2
partition of person_2 for values in ('王五','赵六');
insert into person_2 (id, "name", begin_time, end_time)
values(1,'张三','2024-06-23 01:00:00','2024-06-23 01:00:00'),
(2,'李四','2024-06-24 01:00:00','2024-06-24 01:00:00'),
(3,'王五','2024-06-23 01:00:00','2024-06-23 01:00:00'),
(4,'赵六','2024-06-23 01:00:00','2024-06-23 01:00:00');
创建哈希分区表
CREATE TABLE person_3
(
id SERIAL NOT NULL,
name VARCHAR NOT NULL,
begin_time TIMESTAMP,
end_time TIMESTAMP
) PARTITION BY hash (name);
create table person_3_1
partition of person_3 for values with (modulus 2, remainder 0);
create table person_3_2
partition of person_3 for values with (modulus 2, remainder 1);
insert into person_3 (id, "name", begin_time, end_time)
values(1,'张三','2024-06-23 01:00:00','2024-06-23 01:00:00'),
(2,'李四','2024-06-24 01:00:00','2024-06-24 01:00:00'),
(3,'王五','2024-06-23 01:00:00','2024-06-23 01:00:00'),
(4,'赵六','2024-06-23 01:00:00','2024-06-23 01:00:00');
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本