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

          軟件下載吧

          當前位置:軟件下載吧 > 技術開發 > 數據庫 > PostgreSQL 創建表分區

          PostgreSQL 創建表分區

          時間:2024-02-14 10:20作者:下載吧人氣:21

          創建表分區步驟如下:
          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啦!

          標簽[db:關鍵字]

          相關下載

          查看所有評論+

          網友評論

          網友
          您的評論需要經過審核才能顯示

          熱門閱覽

          最新排行

          公眾號

          主站蜘蛛池模板: 在线视频一区二区三区三区不卡| 在线观看一区二区三区视频| 亚洲一区二区三区深夜天堂| 国产伦精品一区二区三区精品| 精品爆乳一区二区三区无码av| 精品一区二区三区水蜜桃| 性色av一区二区三区夜夜嗨| 国产一区二区三精品久久久无广告| 欧亚精品一区三区免费| 成人免费一区二区三区在线观看| 精品国产一区二区三区色欲| 亚洲一区二区三区在线| 亚洲国产一区二区三区在线观看| 亚洲一区二区三区高清视频| 亚洲色精品VR一区区三区| 久久亚洲日韩精品一区二区三区| 亚洲va乱码一区二区三区| 国产精品揄拍一区二区| 国产一区二区三区日韩精品| 久久精品国产一区二区电影| 日韩人妻一区二区三区蜜桃视频 | 亚洲夜夜欢A∨一区二区三区| 国产高清在线精品一区二区三区 | 精品人妻少妇一区二区| 丰满人妻一区二区三区免费视频 | 人妻体内射精一区二区三区 | 一区二区三区人妻无码| 亚洲日韩激情无码一区| 亚洲福利一区二区三区| 精品一区二区三区四区在线播放| 精品一区二区三区视频 | 无码国产精品一区二区免费3p| 国产主播在线一区| 亲子乱av一区二区三区| 国产对白精品刺激一区二区| 日韩精品无码一区二区三区四区| 在线观看一区二区三区视频| 亚洲国产激情在线一区| 香蕉视频一区二区三区| 日本精品高清一区二区2021| 国产在线精品一区二区在线观看|