PostgreSQL 创建表分区

admin 发表于 [PostgreSQL] 分类,
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); 
下面就可以测试写入数据

发表我的评论