Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4158262
  • 博文数量: 240
  • 博客积分: 11504
  • 博客等级: 上将
  • 技术积分: 4277
  • 用 户 组: 普通用户
  • 注册时间: 2006-12-28 14:24
文章分类

全部博文(240)

分类: Mysql/postgreSQL

2013-12-15 18:13:51

POSTGRESQL的分区和MYSQL不同,MYSQL是有专门的分区表, 而POSTGRESQL的分区则利用它本身的面向对象的特性来做。 下面我们来简单的体验下。


我们先创建一张父表。 记住,所有的分区表都得继承他。

点击(此处)折叠或打开

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



接下来我们创建一个简单的函数来动态创建分区表。

点击(此处)折叠或打开

  1. t_girl=# create or replace function create_partition_table () returns void as $$
  2. t_girl$# declare i int;
  3. t_girl$# declare cnt int;
  4. t_girl$# declare stmt text;
  5. t_girl$# begin
  6. t_girl$# -- Created by ytt at 2013/12/15. Dynamic creating partition tables.
  7. t_girl$# i:= 0;
  8. t_girl$# cnt:=4;
  9. t_girl$# <<lable1>> while i < cnt loop
  10. t_girl$# stmt := 'create table num_slave'||i+1||'(check(id >='||i*100||' and id <'||(i+1)*100||')) inherits(num_master)';
  11. t_girl$# execute stmt;
  12. t_girl$# i:=i + 1;
  13. t_girl$# end loop lable1;
  14. t_girl$# return;
  15. t_girl$# end;
  16. t_girl$# $$ language plpgsql;
  17. CREATE FUNCTION
  18. t_girl=#



OK。 现在可以执行了。

点击(此处)折叠或打开

  1. t_girl=# select create_partition_table();
  2.  create_partition_table
  3. ------------------------
  4.  
  5. (1 row)




列出所有的表

点击(此处)折叠或打开

  1. t_girl=# \d
  2.            List of relations
  3.  Schema | Name | Type | Owner
  4. --------+------------+-------+----------
  5.  ytt | num_master | table | postgres
  6.  ytt | num_slave1 | table | postgres
  7.  ytt | num_slave2 | table | postgres
  8.  ytt | num_slave3 | table | postgres
  9.  ytt | num_slave4 | table | postgres
  10.  ytt | t1 | table | t_girl
  11. (6 rows)




我们针对父表建立一个触发器函数体,对应其分区表的数据分布。

点击(此处)折叠或打开

  1. t_girl=# create or replace function num_insert_trigger()
  2. t_girl-# returns trigger as $$
  3. t_girl$# begin
  4. t_girl$# -- Created by ytt at 2013/12/15. Do how to distribute data.
  5. t_girl$# if (new.id >=0 and new.id <100) then
  6. t_girl$# insert into num_slave1 values (new.*);
  7. t_girl$# elsif (new.id >=100 and new.id <200) then
  8. t_girl$# insert into num_slave2 values(new.*);
  9. t_girl$# elsif (new.id >=200 and new.id <300) then
  10. t_girl$# insert into num_slave3 values (new.*);
  11. t_girl$# elsif (new.id >=300 and new.id <400) then
  12. t_girl$# insert into num_slave4 values (new.*);
  13. t_girl$# else
  14. t_girl$# raise exception 'Column id out of range.';
  15. t_girl$# end if;
  16. t_girl$# return null;
  17. t_girl$# end;
  18. t_girl$# $$
  19. t_girl-# language plpgsql;
  20. CREATE FUNCTION




我们看看已经建好的触发器:

点击(此处)折叠或打开

  1. t_girl=# \d+ num_master
  2.                        Table "ytt.num_master"
  3.  Column | Type | Modifiers | Storage | Stats target | Description
  4. --------+---------+-----------+---------+--------------+-------------
  5.  id | integer | not null | plain | |
  6. Indexes:
  7.     "num_master_pkey" PRIMARY KEY, btree (id)
  8. Triggers:
  9.     insert_num_slave_trigger BEFORE INSERT ON num_master FOR EACH ROW EXECUTE PROCEDURE ytt.num_insert_trigger()
  10. Child tables: num_slave1,
  11.               num_slave2,
  12.               num_slave3,
  13.               num_slave4
  14. Has OIDs: no






我们现在生成简单的测试数据。

点击(此处)折叠或打开

  1. t_girl=# select func_create_sample_data();
  2.  func_create_sample_data
  3. -------------------------
  4.  
  5. (1 row)




上面的函数生成了大概400行的数据。




为了查看优化器是如何处理查询的,我们来看看简单的查询

点击(此处)折叠或打开

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


  12. t_girl=#






我也是今天刚刚接触到POSTGRESQL的分区表,有问题,还希望提出。




阅读(4306) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~