首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >postgres分区表

postgres分区表

原创
作者头像
eeaters
发布2024-11-01 10:33:24
发布2024-11-01 10:33:24
38800
代码可运行
举报
文章被收录于专栏:阿杰阿杰
运行总次数:0
代码可运行

一、特性

postgres分区表是数据层层面的, 相对于普通表在内部实现复杂,但是用户无感知.

分区表是一种将大表拆分成多个小表的方式

  • Hash 分区:根据特定列的哈希值将数据均匀分布到多个分区中。这种方式适用于需要均匀分布数据的场景,能够避免某些分区过大而其他分区过小的情况。通常用于数值型或字符串型数据。
  • List 分区:根据特定列的值将数据分到不同的分区中。适用于分区的值是离散的、预定义的,例如状态码、类别等。每个分区包含特定的值集合。
  • Range分区: 表被分区为由分区键定义的“范围”,分配给不同分区的值范围之间没有重叠。
  • Multi-Level分区: 分区表被分成多个分区后,这些分区还可以继续被分区,这样的分区表被称之为多级分区。

二、建表&分区语句

背景是一个中间表的结果集太庞大, 但是数据是有租户隔离的, 因为基于List/Hash进行数据隔离测试

Hash分区建表&分区

代码语言:javascript
代码运行次数:0
运行
复制
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);

List分区建表&分区

代码语言:javascript
代码运行次数:0
运行
复制
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;

三、插入数据

代码语言:javascript
代码运行次数:0
运行
复制
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);

四、查询 SQL

主表查询

代码语言:javascript
代码运行次数:0
运行
复制
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

分区表查询

代码语言:javascript
代码运行次数:0
运行
复制
-- 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 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、特性
    • 二、建表&分区语句
      • Hash分区建表&分区
      • List分区建表&分区
    • 三、插入数据
    • 四、查询 SQL
      • 主表查询
      • 分区表查询
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档