PostgreSQL的分區表建立

jopen 12年前發布 | 1K 次閱讀 思維方式

在數據庫日漸龐大的時候,為了方便對數據庫數據的管理,比如按時間,按地區去統計一些數據時,基數過于龐大,多有不便。很多商業數據庫都提供分區的概念,按不同的維度去存放數據,便于后期的管理,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<date '2012-02-01'))
inherits(parent_table);

create table parent_table_2012_02( check (create_time>=date '2012-02-01' and create_time<date '2012-03-01')) inherits(parent_table);

create table parent_table_2012_03( check (create_time>=date '2012-03-01' and create_time<date '2012-04-01')) inherits(parent_table);

create table parent_table_2012_04( check (create_time>=date '2012-04-01' and create_time<date '2012-05-01')) inherits(parent_table);

create table parent_table_2012_05( check (create_time>=date '2012-05-01' and create_time<date '2012-06-01')) inherits(parent_table);

create table parent_table_2012_06( check (create_time>=date '2012-06-01' and create_time<date '2012-07-01')) inherits(parent_table);

create table parent_table_2012_07( check (create_time>=date '2012-07-01' and create_time<date '2012-08-01')) inherits(parent_table);

create table parent_table_2012_08( check (create_time>=date '2012-08-01' and create_time<date '2012-09-01')) inherits(parent_table);

create table parent_table_2012_09( check (create_time>=date '2012-09-01' and create_time<date '2012-10-01')) inherits(parent_table);

create table parent_table_2012_10( check (create_time>=date '2012-10-01' and create_time<date '2012-11-01')) inherits(parent_table);

create table parent_table_2012_11( check (create_time>=date '2012-11-01' and create_time<date '2012-12-01')) inherits(parent_table);

create table parent_table_2012_12( check (create_time>=date '2012-12-01' and create_time<date '2013-01-01')) 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)

PS:可以看到實際的數據是存放在子表里去了,父表是沒數據的。這么做前端開發會省去不少工作,但是后端DB會增加不少壓力,可以后端建好分區表,前端直接按時間插入分區表中去,可減少因觸發器帶來的DB壓力。
可以單獨對分區表進行DML或者DDL操作,如truncate

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!