国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > PostgreSQL > 正文

PostgreSQL 創建表分區

2020-03-12 23:55:17
字體:
來源:轉載
供稿:網友
在pg里表分區是通過表繼承來實現的,一般都是建立一個主表,里面是空,然后每個分區都去繼承它。
 
 
創建表分區步驟如下: 
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啦!
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 织金县| 长治市| 灵丘县| 宁安市| 泸溪县| 罗山县| 霍林郭勒市| 开鲁县| 浦城县| 贺州市| 延边| 平度市| 仁寿县| 玛纳斯县| 迁安市| 涞水县| 五常市| 襄汾县| 德安县| 金湖县| 南和县| 呼玛县| 华宁县| 宁安市| 长兴县| 沙湾县| 东海县| 商丘市| 永兴县| 米易县| 正宁县| 乐都县| 台东市| 泾阳县| 林周县| 搜索| 绿春县| 平泉县| 静乐县| 信丰县| 莱西市|