【原創】POSTGRESQL 分區表初次體驗

來源:互聯網
上載者:User

POSTGRESQL的分區和MYSQL不同,MYSQL是有專門的分區表, 而POSTGRESQL的分區則利用它本身的物件導向的特性來做。 下面我們來簡單的體驗下。


我們先建立一張父表。 記住,所有的分區表都得繼承他。

t_girl=# create table num_master (id int not null primary key);CREATE TABLE

接下來我們建立一個簡單的函數來動態建立分區表。

t_girl=# create or replace function create_partition_table () returns void as $$t_girl$# declare i int;t_girl$# declare cnt int;t_girl$# declare stmt text;t_girl$# begint_girl$# -- Created by ytt at 2013/12/15. Dynamic creating partition tables.t_girl$# i:= 0;t_girl$# cnt:=4;t_girl$# <<lable1>> while i < cnt loopt_girl$#   stmt := 'create table num_slave'||i+1||'(check(id >='||i*100||' and id <'||(i+1)*100||')) inherits(num_master)';t_girl$#   execute stmt;t_girl$#   i:=i + 1;t_girl$# end loop lable1;t_girl$# return;t_girl$# end;t_girl$# $$ language plpgsql;CREATE FUNCTIONt_girl=#

OK。 現在可以執行了。

t_girl=# select create_partition_table(); create_partition_table------------------------(1 row)

列出所有的表

t_girl=# \d           List of relations Schema |    Name    | Type  |  Owner  --------+------------+-------+---------- ytt    | num_master | table | postgres ytt    | num_slave1 | table | postgres ytt    | num_slave2 | table | postgres ytt    | num_slave3 | table | postgres ytt    | num_slave4 | table | postgres ytt    | t1         | table | t_girl(6 rows)


我們針對父表建立一個觸發器函數體,對應其分區表的資料分布。

t_girl=# create or replace function num_insert_trigger()t_girl-# returns trigger as $$t_girl$# begint_girl$# -- Created by ytt at 2013/12/15. Do how to distribute data.t_girl$# if (new.id >=0 and new.id <100) thent_girl$# insert into num_slave1 values (new.*);t_girl$# elsif (new.id >=100 and new.id <200) thent_girl$# insert into num_slave2 values(new.*);t_girl$# elsif (new.id >=200 and new.id <300) thent_girl$# insert into num_slave3 values (new.*);t_girl$# elsif (new.id >=300 and new.id <400) thent_girl$# insert into num_slave4 values (new.*);t_girl$# elset_girl$# raise exception 'Column id out of range.';t_girl$# end if;t_girl$# return null;t_girl$# end;t_girl$# $$t_girl-# language plpgsql;CREATE FUNCTION


我們看看已經建好的觸發器:

t_girl=# \d+ num_master                       Table "ytt.num_master" Column |  Type   | Modifiers | Storage | Stats target | Description--------+---------+-----------+---------+--------------+------------- id     | integer | not null  | plain   |              |Indexes:    "num_master_pkey" PRIMARY KEY, btree (id)Triggers:    insert_num_slave_trigger BEFORE INSERT ON num_master FOR EACH ROW EXECUTE PROCEDURE ytt.num_insert_trigger()Child tables: num_slave1,              num_slave2,              num_slave3,              num_slave4Has OIDs: no



我們現在產生簡單的測試資料。

t_girl=# select func_create_sample_data(); func_create_sample_data-------------------------(1 row)

上面的函數產生了大概400行的資料。



為了查看最佳化器是如何處理查詢的,我們來看看簡單的查詢

t_girl=# explain select * from num_master where id > 30 and id < 120;                           QUERY PLAN                           ----------------------------------------------------------------- Append  (cost=0.00..5.00 rows=91 width=4)   ->  Seq Scan on num_master  (cost=0.00..0.00 rows=1 width=4)         Filter: ((id > 30) AND (id < 120))   ->  Seq Scan on num_slave1  (cost=0.00..2.50 rows=70 width=4)         Filter: ((id > 30) AND (id < 120))   ->  Seq Scan on num_slave2  (cost=0.00..2.50 rows=20 width=4)         Filter: ((id > 30) AND (id < 120))(7 rows)t_girl=#




我也是今天剛剛接觸到POSTGRESQL的分區表,有問題,還希望提出。



本文出自 “上帝,咱們不見不散!” 部落格,請務必保留此出處http://yueliangdao0608.blog.51cto.com/397025/1340860

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.