Postgresql 分区表测试

创建分区表与触发器实现高效数据管理
本文介绍如何通过创建分区表和触发器来优化数据存储与查询效率,详细阐述了创建父表、生成分区表函数、执行分区表创建、创建触发器函数以及触发器的使用,同时展示了插入测试数据并查看查询优化器选择的过程。
--创建父表
drop table  num_master;
create table num_master (id int not null primary key); 


--创建生成分区表的函数
create or replace function create_partition_table () returns void as $$
declare i int;
declare cnt int;
declare stmt text;
begin
i:= 0;
cnt:=4;
<<lable1>> while i < cnt loop
  stmt := 'create table num_slave'||i+1||'(check(id >='||i*100||' and id <'||(i+1)*100||')) inherits(num_master)';
  execute stmt;
  i:=i + 1;
end loop lable1;
return;
end;
$$ language plpgsql;


--执行分区表的创建
select create_partition_table ();


--创建触发器函数
create or replace function num_insert_trigger()
returns trigger as $$
begin
if (new.id >=0 and new.id <100) then
insert into num_slave1 values (new.*);
elsif (new.id >=100 and new.id <200) then
insert into num_slave2 values(new.*);
elsif (new.id >=200 and new.id <300) then
insert into num_slave3 values (new.*);
elsif (new.id >=300 and new.id <400) then
insert into num_slave4 values (new.*);
else
raise exception 'Column id out of range.';
end if;
return null;
end;
$$ language plpgsql;


--创建触发器
CREATE TRIGGER insert_num_master_trigger
BEFORE INSERT ON num_master 
FOR EACH ROW EXECUTE PROCEDURE num_insert_trigger();




test=# \d num_master
  Table "public.num_master"
 Column |  Type   | Modifiers 
--------+---------+-----------
 id     | integer | not null
Indexes:
    "num_master_pkey" PRIMARY KEY, btree (id)
Number of child tables: 4 (Use \d+ to list them.)


test=#  \d+ num_master
                      Table "public.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_master_trigger BEFORE INSERT ON num_master FOR EACH ROW EXECUTE PROCEDURE num_insert_trigger()
Child tables: num_slave1,
              num_slave2,
              num_slave3,
              num_slave4
Has OIDs: no


--插入测试数据
\timing on
insert into num_master select id from (select generate_series(1,399))ids(id);


--查看测试的数据
test=# select count(*) from num_master;
 count 
-------
   399
(1 row)


Time: 0.577 ms
test=# select count(*) from num_slave1;
 count 
-------
    99
(1 row)


Time: 0.327 ms
test=# select count(*) from num_slave2;
 count 
-------
   100
(1 row)


Time: 0.304 ms
test=# select count(*) from num_slave3;
 count 
-------
   100
(1 row)


Time: 0.424 ms
test=# select count(*) from num_slave4;
 count 
-------
   100
(1 row)


--查看查询下优化器的选择
test=# explain select * from num_master where id > 30 and id < 120; 
                           QUERY PLAN                            
-----------------------------------------------------------------
 Append  (cost=0.00..4.98 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.48 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)
可以看到,选择了分区表进行查询
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值