PostgreSQL 創(chuàng)建表分區(qū)
更新時(shí)間:2009年09月06日 01:22:24 作者:
在pg里表分區(qū)是通過表繼承來實(shí)現(xiàn)的,一般都是建立一個(gè)主表,里面是空,然后每個(gè)分區(qū)都去繼承它。
創(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);
下面就可以測試寫入數(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)可以算完了,不過還有個(gè)地方需要修改下,先看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,理論上應(yīng)該只是查詢users_0表,通過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è)過程都OK啦!
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);
下面就可以測試寫入數(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)可以算完了,不過還有個(gè)地方需要修改下,先看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,理論上應(yīng)該只是查詢users_0表,通過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è)過程都OK啦!
您可能感興趣的文章:
- PostgreSQL LIST、RANGE 表分區(qū)的實(shí)現(xiàn)方案
- 淺析postgresql 數(shù)據(jù)庫 TimescaleDB 修改分區(qū)時(shí)間范圍
- 利用python為PostgreSQL的表自動(dòng)添加分區(qū)
- 如何為PostgreSQL的表自動(dòng)添加分區(qū)
- 淺談PostgreSQL 11 新特性之默認(rèn)分區(qū)
- PostgreSQL之分區(qū)表(partitioning)
- PostgreSQL分區(qū)表(partitioning)應(yīng)用實(shí)例詳解
- PostgreSQL教程(三):表的繼承和分區(qū)表詳解
- 淺談PostgreSQL表分區(qū)的三種方式
相關(guān)文章
PostgreSQL實(shí)現(xiàn)一個(gè)通用標(biāo)簽系統(tǒng)
這篇文章主要給大家介紹了關(guān)于利用PostgreSQL實(shí)現(xiàn)一個(gè)通用標(biāo)簽系統(tǒng)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01postgreSQL數(shù)據(jù)庫默認(rèn)用戶postgres常用命令分享
這篇文章主要介紹了postgreSQL數(shù)據(jù)庫默認(rèn)用戶postgres常用命令分享,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01PostgreSQL 流復(fù)制異步轉(zhuǎn)同步的操作
這篇文章主要介紹了PostgreSQL 流復(fù)制異步轉(zhuǎn)同步的操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12PostgreSQL基礎(chǔ)知識之SQL操作符實(shí)踐指南
這篇文章主要給大家介紹了關(guān)于PostgreSQL基礎(chǔ)知識之SQL操作符實(shí)踐的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用PostgreSQL具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05

PostgreSQL查找并刪除重復(fù)數(shù)據(jù)的方法總結(jié)
這篇文章主要給大家介紹了PostgreSQL查找并刪除重復(fù)數(shù)據(jù)的方法,文章通過代碼示例介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作有一點(diǎn)的幫助,需要的朋友可以參考下
2023-10-10 
postgreSQL 使用timestamp轉(zhuǎn)成date格式
這篇文章主要介紹了postgreSQL 使用timestamp轉(zhuǎn)成date格式,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
2021-01-01