PostgreSQL-分区表介绍

一、分区简介

表分区是解决一些因单表过大引用的性能问题的方式,比如某张表过大就会造成查询变慢,可能分区是一种解决方案。一般建议当单表大小超过内存就可以考虑表分区了。

表的分区就是将一个逻辑上的大表(主要指数据量大),切分为多个小的物理的分片。

1.分区的优点

1) 在某些情况下,尤其是当表中大多数被频繁访问的行位于单个分区或少量分区中时,查询性能可以得到显着提高。分区替代了索引的前几列,从而减小了索引的大小,并使索引中频繁使用的部分更有可能装入内存。

2) 当查询或更新访问单个分区的很大一部分时,可以通过对该分区进行顺序扫描而不是使用索引和分散在整个表中的随机访问读取,来提高性能。

直接从分区表查询数据比从一个大而全的全量数据表中读取数据效率更高。

3) 如果计划将这种需求计划到分区设计中,则可以通过添加或删除分区来完成批量加载和删除。使用ALTER TABLE DETACH PARTITION或使用DROP TABLE删除单个分区比批量操作要快得多。这些命令还完全避免了由批量DELETE引起的VACUUM开销。

数据维护成本降低。比如:某一部分数据失效,不需要执行命令来更新数据,可以直接解绑指定关系,解除绑定的数据和分区表都依然保留,需要时可以随时恢复绑定。通过 Flyway 等数据脚本管理能够方便的控制数据维护,避免人为直接操作数据。

4) 很少使用的数据可以迁移到更廉价、更迟缓的存储介质上。

一个表只能放在一个物理空间上,使用分区表之后可以将不同的表放置在不同的物理空间上,从而达到冷数据放在廉价的物理机器上,热点数据放置在性能强劲的机器上。

通常只有在表很大的情况下,这些好处才是值得的。表可以从分区中受益的确切时间取决于应用程序,尽管经验法则是表的大小应超过数据库服务器的物理内存。

2.分区的使用限制

1) 无法创建跨所有分区的排除约束。只能单独约束每个叶子分区。

2) 因为PostgreSQL只能在每个分区中单独进行唯一性约束;因此,分区表上的唯一约束必须包括所有分区键列。

3) 如果需要 BEFORE ROW 触发器,则必须定义在单个分区(而不是分区父表)。

4) 不允许在同一分区树中混合临时和永久关系。因此,如果父表是永久性的,则其分区也必须是永久性的;如果父表是临时的,则其分区也必须是临时的。当使用临时关系时,分区树的所有成员必须来自同一会话。

3.分区的三种方式

1) 范围(Range )分区:表被划分为由键列或列集定义的“范围”,分配给不同分区的值的范围之间没有重叠。例如:可以按日期范围或特定业务对象的标识符范围,来进行分区。
2) 列表(List)分区:通过显式列出哪些键值出现在每个分区中来对表进行分区。
3) 哈希(Hash)分区:(自PG11才提供HASH策略)通过为每个分区指定模数和余数来对表进行分区。每个分区将保存行,分区键的哈希值除以指定的模数将产生指定的余数。

 

二、三种分区方式

1.Range范围分区

先创建一张表带有年龄,然后我们根据年龄分段来进行分区,创建表语句如下:

CREATE TABLE test_person_r (

    age int not null,

    city varchar not null

) PARTITION BY RANGE (age);

这个语句已经指定了按age字段来分区了,接着创建分区表:

create table test_person_r1 partition of test_person_r for values from (MINVALUE) to (10);

create table test_person_r2 partition of test_person_r for values from (11) to (20);

create table test_person_r3 partition of test_person_r for values from (21) to (30);

create table test_person_r4 partition of test_person_r for values from (31) to (MAXVALUE);

这里创建了四张分区表,分别对应年龄是0到10岁、11到20岁、21到30岁、30岁以上。

接着我们插入一些数据:

insert into test_person_r(age, city) VALUES (1, 'GZ');

insert into test_person_r(age, city) VALUES (2, 'SZ');

insert into test_person_r(age, city) VALUES (21, 'SZ');

insert into test_person_r(age, city) VALUES (13, 'BJ');

insert into test_person_r(age, city) VALUES (43, 'SH');

insert into test_person_r(age, city) VALUES (28, 'HK');

可以看到这里的表名还是test_person_r,而不是具体的分区表,说明对于客户端是无感知的。

而且分区表与主表的字段是一致的。

 

2.List列表分区

类似的,列表分区是按特定的值来分区,比较某个城市的数据放在一个分区里。这里不再给出每一步的讲解,代码如下:

-- 创建主表

create table test_person_l (

                          age int not null,

                          city varchar not null

) partition by list (city);

 

-- 创建分区表

CREATE TABLE test_person_l1 PARTITION OF test_person_l FOR VALUES IN ('GZ');

CREATE TABLE test_person_l2 PARTITION OF test_person_l FOR VALUES IN ('BJ');

CREATE TABLE test_person_l3 PARTITION OF test_person_l DEFAULT;

 

-- 插入测试数据

insert into test_person_l(age, city) VALUES (1, 'GZ');

insert into test_person_l(age, city) VALUES (2, 'SZ');

insert into test_person_l(age, city) VALUES (21, 'SZ');

insert into test_person_l(age, city) VALUES (13, 'BJ');

insert into test_person_l(age, city) VALUES (43, 'SH');

insert into test_person_l(age, city) VALUES (28, 'HK');

insert into test_person_l(age, city) VALUES (28, 'GZ');

 

3.Hash哈希分区

哈希分区是指按字段取哈希值后再分区。具体的语句如下:

-- 创建主表

create table test_person_h (

                          age int not null,

                          city varchar not null

) partition by hash (city);

 

-- 创建分区表

create table test_person_h1 partition of test_person_h for values with (modulus 4, remainder 0);

create table test_person_h2 partition of test_person_h for values with (modulus 4, remainder 1);

create table test_person_h3 partition of test_person_h for values with (modulus 4, remainder 2);

create table test_person_h4 partition of test_person_h for values with (modulus 4, remainder 3);

 

-- 插入测试数据

insert into test_person_h(age, city) VALUES (1, 'GZ');

insert into test_person_h(age, city) VALUES (2, 'SZ');

insert into test_person_h(age, city) VALUES (21, 'SZ');

insert into test_person_h(age, city) VALUES (13, 'BJ');

insert into test_person_h(age, city) VALUES (43, 'SH');

insert into test_person_h(age, city) VALUES (28, 'HK');

可以看到创建分区表的时候,我们用了取模的方式,所以如果要创建N个分区表,就要取N取模。

posted @ 2023-09-14 17:45  业余砖家  阅读(479)  评论(0编辑  收藏  举报