PgSQL数据库分区
:分区的意思就是把逻辑上的一个大表分割成物理上的几个小块
分区建立规则:
1.建立主表:
2.创建分区继承(inherits):
3.定义约束(或者定义索引):
4 定义tigger(触发器):
举例说明:
1。create table
create table articles (
id serial not null,
topic varchar(255),
content text,
poster varchar(100),
posttime timestamp
);
2。创建分区
create table articles_q1 (
check ( posttime >= timestamp '2007-01-01 00:00:01' and postgime < timestamp '2007-04-01 00:00:01')
) inherits (articles);
create table articles_q2 (
check ( posttime >= timestamp '2007-04-01 00:00:01' and posttime < timestamp '2007-07-01 00:00:01')
) inherits (articles);
create table articles_q3 (
check ( posttime >= timestamp '2007-07-01 00:00:01' and posttime < timestamp '2007-10-01 00:00:01')
) inherits (articles);
create table articles_q4 (
check ( posttime >= timestamp '2007-10-01 00:00:01' and posttime < timestamp '2008-01-01 00:00:01')
) inherits (articles);
3。定义主键索引
alter table articles_q1 add primary key (id);
alter table articles_q2 add primary key (id);
alter table articles_q3 add primary key (id);
alter table articles_q4 add primary key (id);
4。创建触发器函数
create function insert_articles() returns trigger as $$
declare
cur_timestamp timestamp;
begin
raise exception 'yes';
exception
when raise_exception then
begin
cur_timestamp := now();
if cur_timestamp >= timestamp '2007-01-01 00:00:01' and cur_timestamp < timestamp '2007-04-01 00:00:01' then
insert into articles_q1 (topic,content,poster,posttime) select NEW.topic,NEW.content,NEW.poster,now();
elsif cur_timestamp >= timestamp '2007-04-01 00:00:01' and cur_timestamp < timestamp '2007-07-01 00:00:01' then
insert into articles_q2 (topic,content,poster,posttime) select NEW.topic,NEW.content,NEW.poster,now();
elsif cur_timestamp >= timestamp '2007-07-01 00:00:01' and cur_timestamp < timestamp '2007-10-01 00:00:01' then
insert into articles_q3 (topic,content,poster,posttime) select NEW.topic,NEW.content,NEW.poster,now();
else
insert into articles_q4 (topic,content,poster,posttime) select NEW.topic,NEW.content,NEW.poster,now();
end if;
end;
return null;
end;
$$ language plpgsql;
create trigger tg_insert_articles before insert on articles
for each row execute procedure insert_articles();
5.测试
insert into articles (topic,content,poster,posttime) values ('topic','content','wo','2007-09-15 00:00:00');
返回:
INSERT 0 0
查看结果:
select * from articles;
显示有一条记录
select * from only articles;
没有任何记录
select * from articles_q3;
有一条记录,表示成功(注意:这里是表示添加数据时是在2007第三季度,如果不是第三季的话注意改变表名)
6.开启约束排除
set constraint_exclusion = on;
explain select count(id) from articles where posttime >= timestamp '2007-09-14 00:00:00';
这样它只会查找q3与q4两个表,关闭后它会查找q1~q4四个表。
本文示例参考:http://www.phpchina.com/2145/viewspace_15265.html
分享到:
相关推荐
PostgreSQL表分区和子表及删除所有的数据库表 最近需求要求统计DNS近7天每天的解析情况。数据量相对大,所以我这边对表进行分区。 对每天的数据进行分区存储。主表只存储近7天的数据,7天之前的数据删掉。所以我...
pgsql创建自增ID,建表,创建索引,创建分区表
官方给出的指导意见是:当表的大小超过了数据库服务器的物理内存大小则应当使用分区表,接下来结合一个例子具体记录一下创建分区表的详细过程。 创建分区表 首先看一下需求,现在有一张日志表,现在需要按表中的操作...
PostgreSQL 默认分区 可选 - BRIN 与 Btree 索引 准备 PostgreSQL 数据库 安装 PostgreSQL v11 准备 Zabbix 数据库 创建空的历史*和趋势*表格 PostgreSQL 分区管理器扩展 (pg_partman) 安装 pg_partman 配置文件 ...
实现轻松创建PostgreSQL的分区表DDL
PostgreSQL获取数据库中所有table名及所有字段名,以及获取指定table的所有字段信息
PostgreSQL PostgreSQL PostgreSQL学习手册 学习手册 学习手册 (表的继承和分区 表的继承和分区 表的继承和分区 表的继承和分区 ) 10 一、表的继承: 一、表的继承: 一、表的继承: .10 PostgreSQL PostgreSQL ...
PostgreSQL学习手册(表的继承和分区) PostgreSQL学习手册(常用数据类型) PostgreSQL学习手册(函数和操作符<一>) PostgreSQL学习手册(函数和操作符<二>) PostgreSQL学习手册(函数和操作符<三>) PostgreSQL学习手册...
支持水平数据库分片(分区/访问逻辑位于应用程序内),可通过注释轻松使用pg_advisory_lock以确保单个Java节点执行,每个存储过程可配置的语句超时以及PostgreSQL类型(包括枚举和hstore)。用法要使用SProcWrapper...
什么是窗口函数? 一个窗口函数在一系列与当前行有某种关联的表行上执行一种计算。这与一个聚集函数所完成的计算有可比之处。...可以包含分区 (PARTITION BY) 和排序 (ORDER BY) 指令, 这二者都是可选的。
Slony让我们可以复制数据(有时候也可以分区),而pgbouncer为我们解决了保持链接和连接池的问题。 另外,让我们看看他们的语言:我这个星期很高兴能够学会如何在PGSQL8.4中使用递归查询,他们实在太强大了。这就是...