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

          軟件下載吧

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

          PostgreSQL 創建表分區

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

          創建表分區步驟如下:
          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一区二区三区| 精品国产日韩亚洲一区91| 国产精品高清一区二区人妖| 国产一区二区免费| 亚洲一区二区三区香蕉| 国产精品一区在线观看你懂的| 国产在线观看91精品一区| 国产品无码一区二区三区在线| 亚洲一区二区三区国产精品无码| 久久久91精品国产一区二区| 综合久久久久久中文字幕亚洲国产国产综合一区首 | 久久久国产精品无码一区二区三区| 精品视频一区二区三区四区五区| 国产AV天堂无码一区二区三区| 精品久久久久久中文字幕一区| 国产av一区最新精品| 精品久久久久久无码中文字幕一区 | 精品国产一区二区三区av片| 国产一区二区好的精华液 | 麻豆高清免费国产一区| 亚洲午夜精品一区二区| 亚洲第一区视频在线观看| 成人免费观看一区二区| 手机福利视频一区二区| 福利片免费一区二区三区| 一区二区三区福利视频| 亚洲夜夜欢A∨一区二区三区| 中文国产成人精品久久一区| 伊人久久精品无码av一区| 日韩一区二区久久久久久| 97精品国产一区二区三区 | 亚洲一区中文字幕久久| 国产日韩一区二区三区在线播放| 亚洲av综合av一区二区三区| 国产裸体舞一区二区三区|