创建分区表,按日期范围分区
sql
CREATE TABLE sales (
id serial,
sale_date DATE,
amount NUMERIC,
PRIMARY KEY(id, sale_date)
) PARTITION BY RANGE (sale_date);
创建分区
sql
CREATE TABLE sales_2019 PARTITION OF sales FOR VALUES FROM ('2019-01-01') TO ('2020-01-01');
CREATE TABLE sales_2020 PARTITION OF sales FOR VALUES FROM ('2020-01-01') TO ('2021-01-01');
CREATE TABLE sales_2021 PARTITION OF sales FOR VALUES FROM ('2021-01-01') TO ('2022-01-01');
CREATE TABLE sales_2022 PARTITION OF sales FOR VALUES FROM ('2022-01-01') TO ('2023-01-01');
CREATE TABLE sales_2023 PARTITION OF sales FOR VALUES FROM ('2023-01-01') TO ('2024-01-01');
插入随机日期和随机金额数据,每次插入2190001条数据
sql
INSERT INTO sales (sale_date, amount)
SELECT generate_series(to_date('2019-01-01','yyyy-mm-dd'), to_date('2023-12-31','yyyy-mm-dd'), '0.02 hours'), (random()*100.)::numeric(6,2);
测试性能
sql
select count(1) from sales;
插入数据sql参考:https://pgfans.cn/a/1726
原文中有中类型的随机数据插入可供参考。