创建表分区步骤如下:
1. 创建主表
CREATE TABLE users ( uid int not null primary key, name varchar(20));
2. 创建分区表(必须继承上面的主表)
CREATE TABLE users_0 ( check (uid >= 0 and uid< 100) ) INHERITS (users);
CREATE TABLE users_1 ( check (uid >= 100)) INHERITS (users);
3. 在分区表上建立索引,其实这步可以省略的哦
CREATE INDEX users_0_uidindex on users_0(uid);
CREATE INDEX users_1_uidindex on users_1(uid);
4. 创建规则RULE
CREATE RULE users_insert_0 AS
ON INSERT TO users WHERE
(uid >= 0 and uid < 100)
DO INSTEAD
INSERT INTO users_0 VALUES (NEW.uid,NEW.name);
CREATE RULE users_insert_1 AS
ON INSERT TO users WHERE
(uid >= 100)
DO INSTEAD
INSERT INTO users_1 VALUES (NEW.uid,NEW.name);
下面就可以测试写入数据啦:
postgres=# INSERT INTO users VALUES (100,'smallfish');
INSERT 0 0
postgres=# INSERT INTO users VALUES (20,'aaaaa');
INSERT 0 0
postgres=# select * from users;
uid | name
-----+-----------
20 | aaaaa
100 | smallfish
(2 笔资料列)
postgres=# select * from users_0;
uid | name
-----+-------
20 | aaaaa
(1 笔资料列)
postgres=# select * from users_1;
uid | name
-----+-----------
100 | smallfish
(1 笔资料列)
到这里表分区已经可以算完了,不过还有个地方需要修改下,先看count查询把。
postgres=# EXPLAIN SELECT count(*) FROM users where uid<100;
QUERY PLAN
---------------------------------------------------------------------------------------------
Aggregate (cost=62.75..62.76 rows=1 width=0)
-> Append (cost=6.52..60.55 rows=879 width=0)
-> Bitmap Heap Scan on users (cost=6.52..20.18 rows=293 width=0)
Recheck Cond: (uid < 100)
-> Bitmap Index Scan on users_pkey (cost=0.00..6.45 rows=293 width=0)
Index Cond: (uid < 100)
-> Bitmap Heap Scan on users_0 users (cost=6.52..20.18 rows=293 width=0)
Recheck Cond: (uid < 100)
-> Bitmap Index Scan on users_0_uidindex (cost=0.00..6.45 rows=293 width=0)
Index Cond: (uid < 100)
-> Bitmap Heap Scan on users_1 users (cost=6.52..20.18 rows=293 width=0)
Recheck Cond: (uid < 100)
-> Bitmap Index Scan on users_1_uidindex (cost=0.00..6.45 rows=293 width=0)
Index Cond: (uid < 100)
(14 笔资料列)
按照本来想法,uid小于100,理论上应该只是查询users_0表,通过EXPLAIN可以看到其他他扫描了所有分区的表。
postgres=# SET constraint_exclusion = on;
SET
postgres=# EXPLAIN SELECT count(*) FROM users where uid<100;
QUERY PLAN
---------------------------------------------------------------------------------------------
Aggregate (cost=41.83..41.84 rows=1 width=0)
-> Append (cost=6.52..40.37 rows=586 width=0)
-> Bitmap Heap Scan on users (cost=6.52..20.18 rows=293 width=0)
Recheck Cond: (uid < 100)
-> Bitmap Index Scan on users_pkey (cost=0.00..6.45 rows=293 width=0)
Index Cond: (uid < 100)
-> Bitmap Heap Scan on users_0 users (cost=6.52..20.18 rows=293 width=0)
Recheck Cond: (uid < 100)
-> Bitmap Index Scan on users_0_uidindex (cost=0.00..6.45 rows=293 width=0)
Index Cond: (uid < 100)
(10 笔资料列)
到这里整个过程都OK啦!
PostgreSQL 创建表分区
2020-04-22 15:48PostgreSQL教程网 PostgreSQL
在pg里表分区是通过表继承来实现的,一般都是建立一个主表,里面是空,然后每个分区都去继承它。
延伸 · 阅读
- 2022-04-26使用 Bitnami/Postgresql-Repmgr 镜像快速设置 PostgreSQ
- 2022-03-08分布式 PostgreSQL之Citus 架构
- 2022-03-01目前好用的五大PostgreSQL GUI 可视化工具
- 2022-03-01三大流行数据库(MongoDB、PostgreSQL和Harper)大比拼
- 2022-03-01在 Kubernetes 上运行 Pgpool-Il 实现 PostgreSQL 查询(读
- 2022-02-28详解Linux中PostgreSQL和PostGIS的安装和使用
- PostgreSQL
pgsql查询优化之模糊查询实例详解
这篇文章主要给大家介绍了关于pgsql查询优化之模糊查询的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用pgsql具有一定的参考学习价值...
- PostgreSQL
PostgreSQL将数据加载到buffer cache中操作方法
这篇文章主要介绍了PostgreSQL将数据加载到buffer cache中,我们可以使用pg_prewarm插件来将指定的表加载到OS Buffer或者pg shared buffer中,具体操作方法跟随小编一...
- PostgreSQL
postgresql 切换 log、xlog日志的实现
这篇文章主要介绍了postgresql 切换 log、xlog日志的实现方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...
- PostgreSQL
PostgreSQL 更新视图脚本的注意事项说明
这篇文章主要介绍了PostgreSQL 更新视图脚本的注意事项说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...
- PostgreSQL
postgreSQL数据库 实现向表中快速插入1000000条数据
这篇文章主要介绍了postgreSQL数据库 实现向表中快速插入1000000条数据,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...
- PostgreSQL
pgsql 如何删除仍有活动链接的数据库
这篇文章主要介绍了pgsql 删除仍有活动链接的数据库操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...
- PostgreSQL
postgresql的jsonb数据查询和修改的方法
这篇文章主要介绍了postgresql的jsonb数据查询和修改的方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考...
- PostgreSQL
PostgreSQL时间线(timeline)和History File的用法
这篇文章主要介绍了PostgreSQL时间线(timeline)和History File的用法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...