postgres分区表是数据层层面的, 相对于普通表在内部实现复杂,但是用户无感知.
分区表是一种将大表拆分成多个小表的方式
背景是一个中间表的结果集太庞大, 但是数据是有租户隔离的, 因为基于List/Hash进行数据隔离测试
CREATE TABLE temp (
id bigint not null,
partner_id bigint not null,
customer_code varchar(225) not null,
sku_code varchar(225) not null,
sku_supplier varchar(255) not null,
priority bigint not null,
quantity_type smallint not null,
quantity numeric(16, 2) not null,
PRIMARY KEY (id, partner_id) -- 包含 partner_id 在主键中
) PARTITION BY HASH (partner_id);
-- 创建4个分区,partner_id取余数
CREATE TABLE temp_p1 PARTITION OF temp FOR VALUES WITH (MODULUS 4, REMAINDER 0);
CREATE TABLE temp_p2 PARTITION OF temp FOR VALUES WITH (MODULUS 4, REMAINDER 1);
CREATE TABLE temp_p3 PARTITION OF temp FOR VALUES WITH (MODULUS 4, REMAINDER 2);
CREATE TABLE temp_p4 PARTITION OF temp FOR VALUES WITH (MODULUS 4, REMAINDER 3);
CREATE TABLE temp (
id bigint not null,
partner_id varchar(225) not null,
customer_code varchar(225) not null,
sku_code varchar(225) not null,
sku_supplier varchar(255) not null,
priority bigint not null,
quantity_type smallint not null,
quantity numeric(16, 2) not null,
PRIMARY KEY (id,partner_id)
) PARTITION BY LIST (partner_id);
COMMENT ON TABLE temp IS '控销结果';
-- 添加列注释
COMMENT ON COLUMN temp.id IS '主键';
COMMENT ON COLUMN temp.partner_id IS '商户id';
COMMENT ON COLUMN temp.customer_code IS '客户';
COMMENT ON COLUMN temp.sku_code IS '控销商品';
COMMENT ON COLUMN temp.sku_supplier IS 'SKU供应商';
COMMENT ON COLUMN temp.priority IS '优先级: 越小越高,按照 10、20、30';
COMMENT ON COLUMN temp.quantity_type IS '数量类型';
COMMENT ON COLUMN temp.quantity IS '数量';
-- 为 partner_id 1864 创建分区
CREATE TABLE temp_1864 PARTITION OF temp
FOR VALUES IN ('1864');
-- 为 partner_id 1925 创建分区
CREATE TABLE temp_1925 PARTITION OF temp
FOR VALUES IN ('1925');
-- 没有匹配到时候的默认分区
CREATE TABLE temp_default PARTITION OF temp DEFAULT;
INSERT INTO temp (id, partner_id, customer_code, sku_code, sku_supplier, priority, quantity_type, quantity)
VALUES
(1, '1864', 'customer_code_1', 'sku_code_1', 'sku_supplier_1', 10, 1, 100.00);
INSERT INTO temp (id, partner_id, customer_code, sku_code, sku_supplier, priority, quantity_type, quantity)
VALUES
(2, '1925', 'customer_code_2', 'sku_code_2', 'sku_supplier_2', 20, 2, 200.00);
select * from temp
id | partner_id | customer_code | sku_code | sku_supplier | priority | quantity_type | quantity |
---|---|---|---|---|---|---|---|
1 | 1864 | customer_code_1 | sku_code_1 | sku_supplier_1 | 10 | 1 | 100.00 |
2 | 1925 | customer_code_2 | sku_code_2 | sku_supplier_2 | 20 | 2 | 200.00 |
-- List的分区表
select * from temp_1864
-- Hash的分区表
select * from temp_p4
id | partner_id | customer_code | sku_code | sku_supplier | priority | quantity_type | quantity |
---|---|---|---|---|---|---|---|
1 | 1864 | customer_code_1 | sku_code_1 | sku_supplier_1 | 10 | 1 | 100.00 |
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。