You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

34 lines
1.3 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

CREATE TABLE test_table (
id serial4,
student_name varchar(128),
birthday date,
CONSTRAINT "test_table_pkey" PRIMARY KEY (id,birthday)
) PARTITION BY RANGE (birthday);
##上表是将birthday字段进行声明式分区注意如果表中已有主键那么分区用的字段必须和已有主键一起做一个联合主键。除非表中没有主键。
##创建分区表,以每个月进行分区
CREATE TABLE test_table_01 PARTITION OF test_table
FOR VALUES FROM ('2023-01-01') TO ('2023-02-01');
CREATE TABLE test_table_02 PARTITION OF test_table
FOR VALUES FROM ('2023-02-01') TO ('2023-03-01');
CREATE TABLE test_table_03 PARTITION OF test_table
FOR VALUES FROM ('2023-03-01') TO ('2023-04-01');
CREATE TABLE test_table_04 PARTITION OF test_table
FOR VALUES FROM ('2023-04-01') TO ('2023-05-01');
CREATE TABLE test_table_05 PARTITION OF test_table
FOR VALUES FROM ('2023-05-01') TO ('2023-06-01');
CREATE TABLE test_table_06 PARTITION OF test_table
FOR VALUES FROM ('2023-06-01') TO ('2023-07-01');
##查询主表 test_table 可以查所有数据
##查询分区表,例如 test_table_01 只能查到1月份的数据
##分区表类似于虚拟表在Navicat中是看不到的。
##官方文档https://www.postgresql.org/docs/16/ddl-partitioning.html#DDL-PARTITIONING
##将不同的分区表放到不同的磁盘中
https://blog.csdn.net/songyundong1993/article/details/123357556