下載吧 - 綠色安全的游戲和軟件下載中心

軟件下載吧

當(dāng)前位置:軟件下載吧 > 技術(shù)開(kāi)發(fā) > 數(shù)據(jù)庫(kù) > PostgreSQL 創(chuàng)建表分區(qū)

PostgreSQL 創(chuàng)建表分區(qū)

時(shí)間:2024-02-14 10:20作者:下載吧人氣:22

創(chuàng)建表分區(qū)步驟如下:
1. 創(chuàng)建主表
CREATE TABLE users ( uid int not null primary key, name varchar(20));
2. 創(chuàng)建分區(qū)表(必須繼承上面的主表)
CREATE TABLE users_0 ( check (uid >= 0 and uid< 100) ) INHERITS (users);
CREATE TABLE users_1 ( check (uid >= 100)) INHERITS (users);
3. 在分區(qū)表上建立索引,其實(shí)這步可以省略的哦
CREATE INDEX users_0_uidindex on users_0(uid);
CREATE INDEX users_1_uidindex on users_1(uid);
4. 創(chuàng)建規(guī)則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);
下面就可以測(cè)試寫(xiě)入數(shù)據(jù)啦:
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 筆資料列)
到這里表分區(qū)已經(jīng)可以算完了,不過(guò)還有個(gè)地方需要修改下,先看count查詢(xún)把。
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 筆資料列)
按照本來(lái)想法,uid小于100,理論上應(yīng)該只是查詢(xún)users_0表,通過(guò)EXPLAIN可以看到其他他掃描了所有分區(qū)的表。
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 筆資料列)
到這里整個(gè)過(guò)程都OK啦!

標(biāo)簽[db:關(guān)鍵字]

相關(guān)下載

查看所有評(píng)論+

網(wǎng)友評(píng)論

網(wǎng)友
您的評(píng)論需要經(jīng)過(guò)審核才能顯示

熱門(mén)閱覽

最新排行

公眾號(hào)

主站蜘蛛池模板: 成人乱码一区二区三区AV| 日韩中文字幕视频在线| 国产午夜精品一区二区三区不卡| 中文字幕日韩视频| 爱情岛永久地址www成人| 国产福利一区二区| 中国黄色在线观看| 欧美精品免费观看二区| 国产人妖一区二区| 99在线视频免费| 日本边摸边吃奶边做很爽视频| 制服丝袜日韩欧美| sss欧美华人整片在线观看| 成在人线AV无码免费| 亚洲日本va在线观看| 色爱无码av综合区| 国产精品观看在线亚洲人成网| 久久99精品久久久久久水蜜桃 | 天天操天天射天天| 五月婷婷色综合| 男女免费观看在线爽爽爽视频| 国产精品一区二区在线观看| 一本色道久久综合狠狠躁篇| 欧洲熟妇色xxxx欧美老妇多毛网站| 动漫美女www网站免费看动漫| 1000部拍拍拍18勿入免费凤凰福利| 性色av一区二区三区| 亚洲aaa视频| 狠狠躁夜夜躁人人爽天天天天97 | 日韩精品一区二区三区老鸭窝| 免费人成网站7777视频| 香蕉视频黄在线观看| 国模精品一区二区三区| 中文字幕日韩一区二区三区不卡| 欧美性受xxxx| 免费无码不卡视频在线观看| 黑人系列合集h| 欧美jizz18欧美| 免费a级在线观看播放| 青青草国产免费国产| 国产精品精品自在线拍|