PostgreSQL 创建表分区

所属分类: 数据库 / PostgreSQL 阅读数: 167
收藏 0 赞 0 分享
创建表分区步骤如下:
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教程(七):函数和操作符详解(3)

这篇文章主要介绍了PostgreSQL教程(七):函数和操作符详解(3),本文讲解了序列操作函数、条件表达式、数组函数和操作符、系统信息函数、系统管理函数等内容,需要的朋友可以参考下
收藏 0 赞 0 分享

PostgreSQL教程(八):索引详解

这篇文章主要介绍了PostgreSQL教程(八):索引详解,本文讲解了索引的类型、复合索引、组合多个索引、唯一索引、表达式索引、部分索引等内容,需要的朋友可以参考下
收藏 0 赞 0 分享

PostgreSQL教程(九):事物隔离介绍

这篇文章主要介绍了PostgreSQL教程(九):事物隔离介绍,本文主要针对读已提交和可串行化事物隔离级别进行说明和比较,需要的朋友可以参考下
收藏 0 赞 0 分享

PostgreSQL教程(十):性能提升技巧

这篇文章主要介绍了PostgreSQL教程(十):性能提升技巧,本文讲解了使用EXPLAIN、批量数据插入、关闭自动提交、使用COPY、 删除索引、删除外键约束等技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

PostgreSQL教程(十一):服务器配置

这篇文章主要介绍了PostgreSQL教程(十一):服务器配置,本文讲解了服务器进程的启动和关闭、服务器配置、内存相关的参数配置等内容,需要的朋友可以参考下
收藏 0 赞 0 分享

PostgreSQL教程(十二):角色和权限管理介绍

这篇文章主要介绍了PostgreSQL教程(十二):角色和权限管理介绍,本文讲解了数据库角色、角色属性、权限、角色成员,需要的朋友可以参考下
收藏 0 赞 0 分享

PostgreSQL教程(十四):数据库维护

这篇文章主要介绍了PostgreSQL教程(十四):数据库维护,本文讲解了恢复磁盘空间、更新规划器统计、VACUUM和ANALYZE的示例、定期重建索引等内容,需要的朋友可以参考下
收藏 0 赞 0 分享

PostgreSQL教程(十五):系统表详解

这篇文章主要介绍了PostgreSQL教程(十五):系统表详解,本文讲解了pg_class、pg_attribute、pg_attrdef、pg_authid、pg_auth_members、pg_constraint、pg_tablespace、pg_namespace、pg_
收藏 0 赞 0 分享

PostgreSQL教程(十六):系统视图详解

这篇文章主要介绍了PostgreSQL教程(十六):系统视图详解,本文讲解了pg_tables、pg_indexes、pg_views、pg_user、pg_roles、pg_rules、pg_settings等视图的作用和字段含义等内容,需要的朋友可以参考下
收藏 0 赞 0 分享

PostgreSQL教程(十八):客户端命令(2)

这篇文章主要介绍了PostgreSQL教程(十八):客户端命令(2),本文讲解了pg_dump、pg_restore、psql、内置命令应用示例等内容,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多