PostgreSQL 创建表分区

所属分类: 数据库 / PostgreSQL 阅读数: 155
收藏 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啦!
更多精彩内容其他人还在看

Mac系统重置PostgreSQL密码的方法示例代码

PostgreSQL 是一个免费的对象-关系数据库服务器(ORDBMS),在灵活的BSD许可证下发行。这篇文章主要介绍了Mac系统重置PostgreSQL密码的方法示例代码,需要的朋友可以参考下
收藏 0 赞 0 分享

PostgreSQL基础知识之SQL操作符实践指南

这篇文章主要给大家介绍了关于PostgreSQL基础知识之SQL操作符实践的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用PostgreSQL具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

PostgreSQL新手入门教程

本文介绍PostgreSQL的安装和基本用法,供初次使用者上手。以下内容基于Debian操作系统,其他操作系统实在没有精力兼顾,但是大部分内容应该普遍适用
收藏 0 赞 0 分享

深入理解PostgreSQL的MVCC并发处理方式

这篇文章主要介绍了深入理解PostgreSQL的MVCC并发处理方式,文中同时介绍了MVCC的缺点,需要的朋友可以参考下
收藏 0 赞 0 分享

15个postgresql数据库实用命令分享

这篇文章主要介绍了15个实用的postgresql数据库命令分享,都是一些技巧性的postgresql命令,需要的朋友可以参考下
收藏 0 赞 0 分享

PostgreSQL ERROR: invalid escape string 解决办法

这篇文章主要介绍了PostgreSQL ERROR: invalid escape string 解决办法,本文环境是JDBC+PostgreSQL,需要的朋友可以参考下
收藏 0 赞 0 分享

CentOS中运行PostgreSQL需要修改的内核参数及配置脚本分享

这篇文章主要介绍了CentOS中运行PostgreSQL需要修改的内核参数及配置脚本分享,本文从系统资源限制类和内存参数优化类来进行说明,需要的朋友可以参考下
收藏 0 赞 0 分享

在windows下手动初始化PostgreSQL数据库教程

在windows下手动初始化PG,是一件比较麻烦的事,下面我具体写一下过程,大家做一下参考。
收藏 0 赞 0 分享

SQL Server数据迁移至PostgreSQL出错的解释以及解决方案

最近对SQL Server到PostgreSQL的数据迁移时出现了问题,返回的错误为:invalid byte sequence for encoding "UTF8": 0x00。经查证pg源代码,该问题引起的原因是sql server的字符类型字段中含有空
收藏 0 赞 0 分享

初识PostgreSQL存储过程

这篇文章主要介绍了初识PostgreSQL存储过程,本文讲解了PostgreSQL中存储过程的语法,并给出了一个操作实例,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多