標籤:ar io os for java on 資料 art cti
在資料庫日漸龐大的時候,為了方便對資料庫資料的管理,比如按時間,按地區去統計一些資料時,基數過於龐大,多有不便。很多商務資料庫都提供分區的概念,按不同的維度去存放資料,便於後期的管理,PG也不例外。下面是分區表建立步驟:
1.建立主表
create table parent_table(
id int, name character varying(20), create_time timestamp without time zone);
2.建立子表,繼承於主表
create table parent_table_2012_01(
check (create_time>=date ‘2012-01-01‘ and create_time inherits(parent_table);
create table parent_table_2012_02(
check (create_time>=date ‘2012-02-01‘ and create_time inherits(parent_table);
create table parent_table_2012_03(
check (create_time>=date ‘2012-03-01‘ and create_time inherits(parent_table);
create table parent_table_2012_04(
check (create_time>=date ‘2012-04-01‘ and create_time inherits(parent_table);
create table parent_table_2012_05(
check (create_time>=date ‘2012-05-01‘ and create_time inherits(parent_table);
create table parent_table_2012_06(
check (create_time>=date ‘2012-06-01‘ and create_time inherits(parent_table);
create table parent_table_2012_07(
check (create_time>=date ‘2012-07-01‘ and create_time inherits(parent_table);
create table parent_table_2012_08(
check (create_time>=date ‘2012-08-01‘ and create_time inherits(parent_table);
create table parent_table_2012_09(
check (create_time>=date ‘2012-09-01‘ and create_time inherits(parent_table);
create table parent_table_2012_10(
check (create_time>=date ‘2012-10-01‘ and create_time inherits(parent_table);
create table parent_table_2012_11(
check (create_time>=date ‘2012-11-01‘ and create_time inherits(parent_table);
create table parent_table_2012_12(
check (create_time>=date ‘2012-12-01‘ and create_time inherits(parent_table);
3.建立觸發器函數
CREATE OR REPLACE FUNCTION test.tri_parent_tab_insert()
RETURNS TRIGGER AS $$
–author: kenyon
–created:2012-05-24
BEGIN
IF ( NEW.create_time >= DATE ‘2012-01-01‘ AND NEW.create_time < DATE ‘2012-02-01‘ ) THEN INSERT INTO test.parent_table_2012_01 VALUES (NEW.id,NEW.name,NEW.create_time); ELSIF ( NEW.create_time >= DATE ‘2012-02-01‘ AND NEW.create_time < DATE ‘2012-03-01‘ ) THEN INSERT INTO test.parent_table_2012_02 VALUES (NEW.id,NEW.name,NEW.create_time); ELSIF ( NEW.create_time >= DATE ‘2012-03-01‘ AND NEW.create_time < DATE ‘2012-04-01‘ ) THEN INSERT INTO test.parent_table_2012_03 VALUES (NEW.id,NEW.name,NEW.create_time); ELSIF ( NEW.create_time >= DATE ‘2012-04-01‘ AND NEW.create_time < DATE ‘2012-05-01‘ ) THEN INSERT INTO test.parent_table_2012_04 VALUES (NEW.id,NEW.name,NEW.create_time); ELSIF ( NEW.create_time >= DATE ‘2012-05-01‘ AND NEW.create_time < DATE ‘2012-06-01‘ ) THEN INSERT INTO test.parent_table_2012_05 VALUES (NEW.id,NEW.name,NEW.create_time); ELSIF ( NEW.create_time >= DATE ‘2012-06-01‘ AND NEW.create_time < DATE ‘2012-07-01‘ ) THEN INSERT INTO test.parent_table_2012_06 VALUES (NEW.id,NEW.name,NEW.create_time); ELSIF ( NEW.create_time >= DATE ‘2012-07-01‘ AND NEW.create_time < DATE ‘2012-08-01‘ ) THEN INSERT INTO test.parent_table_2012_07 VALUES (NEW.id,NEW.name,NEW.create_time); ELSIF ( NEW.create_time >= DATE ‘2012-08-01‘ AND NEW.create_time < DATE ‘2012-09-01‘ ) THEN INSERT INTO test.parent_table_2012_08 VALUES (NEW.id,NEW.name,NEW.create_time); ELSIF ( NEW.create_time >= DATE ‘2012-09-01‘ AND NEW.create_time < DATE ‘2012-10-01‘ ) THEN INSERT INTO test.parent_table_2012_09 VALUES (NEW.id,NEW.name,NEW.create_time); ELSIF ( NEW.create_time >= DATE ‘2012-10-01‘ AND NEW.create_time < DATE ‘2012-11-01‘ ) THEN INSERT INTO test.parent_table_2012_10 VALUES (NEW.id,NEW.name,NEW.create_time); ELSIF ( NEW.create_time >= DATE ‘2012-11-01‘ AND NEW.create_time < DATE ‘2012-12-01‘ ) THEN INSERT INTO test.parent_table_2012_11 VALUES (NEW.id,NEW.name,NEW.create_time); ELSIF ( NEW.create_time >= DATE ‘2012-12-01‘ AND NEW.create_time < DATE ‘2013-01-01‘ ) THEN INSERT INTO test.parent_table_2012_12 VALUES (NEW.id,NEW.name,NEW.create_time); ELSE RAISE EXCEPTION ‘Date out of range.Fix the test.parent_table_insert_trigger() function!‘; END IF; RETURN NULL;
END;
$$
LANGUAGE plpgsql;
4.建立觸發器
CREATE TRIGGER tri_insert_parent_table
BEFORE INSERT ON test.parent_table FOR EACH ROW EXECUTE PROCEDURE test.tri_parent_tab_insert();
5.測試
至此就OK了。前端插入時只要插入主表就可以自動將資料按時間分類分插到子表裡去。
插入一定的測試資料,來看看效果
kenyon=# select count(1) from test.parent_table_2012_03;
count
2293760
(1 row)
kenyon=# select count(1) from test.parent_table;
count
2293761
(1 row)
kenyon=# select pg_size_pretty(pg_relation_size(‘test.parent_table_2012_03‘));
pg_size_pretty
106 MB
(1 row)
kenyon=# select pg_size_pretty(pg_relation_size(‘test.parent_table‘));
pg_size_pretty
8192 bytes
(1 row)
6.總結:
a.可以看到實際的資料是存放在子表裡去了,父表是沒資料的。
b.這麼做前端開發會省去不少工作,但是後端DB會增加不少壓力,可以後端建好分區表,前端直接按時間插入分區表中去,可減少因觸發器帶來的DB壓力。
c.可以單獨對分區表進行DML或者DDL操作,如truncate。
d.通過explain查看查詢是否走得分區,如果未走分區,檢查SQL文法和與之相關的系統參數,如constraint_exclusion是否是partition的
PostgreSQL的分區表建立