PostgreSQL中的分区表是通过表继承来实现的(表继承博客http://www.cnblogs.com/NextAction/p/7366607.html)。创建分区表的步骤如下:

(1)创建“父表”,所有的分区表都从这张表继承。“父表”中不存数据,也不要定义约束和索引。

(2)创建“子表”,所有“子表”都是从“父表”中继承而来。这些“子表”就是所谓的分区,其实它们也是PostgreSQL表。

(3)给分区表创建约束。

(4)在分区表上创建索引。

(5)创建触发器,把对“父表”的插入重定向到分区表中。

(6)确保postgresql.conf中constraint_exclusion的配置参数是打开状态。打开后,可以确保查询智能的只查询分区表,而不会对其他分区表进行查询。

下面是创建分区表的例子:

--创建销售明细表,作为“父表”
create table sales_detail (
product_id int not null,
price numeric(12,2),
amount int not null,
sale_date date not null,
buyer varchar(40),
buyer_contact text
); --根据销售日期sale_date字段,每个季度作为一个分区,创建分区表
create table sales_detail_Y2017Q01(check (sale_date >= date '2017-01-01' and sale_date < date '2017-04-01') ) inherits (sales_detail); create table sales_detail_Y2017Q02(check (sale_date >= date '2017-04-01' and sale_date < date '2017-07-01') ) inherits (sales_detail); create table sales_detail_Y2017Q03(check (sale_date >= date '2017-07-01' and sale_date < date '2017-10-01') ) inherits (sales_detail); create table sales_detail_Y2017Q04(check (sale_date >= date '2017-10-01' and sale_date < date '2018-01-01') ) inherits (sales_detail); --在分区键sale_detail上创建索引
create index sales_detail_Y2017Q01_sale_date on sales_detail_Y2017Q01 (sale_date); create index sales_detail_Y2017Q02_sale_date on sales_detail_Y2017Q02 (sale_date); create index sales_detail_Y2017Q03_sale_date on sales_detail_Y2017Q03 (sale_date); create index sales_detail_Y2017Q04_sale_date on sales_detail_Y2017Q04 (sale_date); --创建触发器,当向sales_detail表中插入数据时,可以重定向插入到分区表中
create or replace function sales_detail_insert_trigger()
returns trigger as $$
begin
if (new.sale_date >= date '2017-01-01' and new.sale_date < date '2017-04-01') then
insert into sales_detail_Y2017Q01 values (new.*);
elsif (new.sale_date >= date '2017-04-01' and new.sale_date < date '2017-07-01') then
insert into sales_detail_Y2017Q02 values (new.*);
elsif (new.sale_date >= date '2017-07-01' and new.sale_date < date '2017-10-01') then
insert into sales_detail_Y2017Q03 values (new.*);
elsif (new.sale_date >= date '2017-10-01' and new.sale_date < date '2018-01-01') then
insert into sales_detail_Y2017Q04 values (new.*);
else
raise exception 'Date out of range.Fix the sales_detail_insert_trigger () function!';
end if;
return null;
end;
$$
language plpgsql; create trigger insert_sales_detail_trigger
before insert on sales_detail
for each row execute procedure sales_detail_insert_trigger (); --设置constraint_exclusion参数为“partition”状态。此参数默认为“partition”
set constrait_exclusion 'partition'

测试分区表:

--向“父表”中插入一条数据
test=# insert into sales_detail values (1,23.22,1,date'2017-08-16','zhaosi','xiangyashan222hao'); --数据已经插入到分区表中
test=# select * from sales_detail_Y2017Q03;
product_id | price | amount | sale_date | buyer | buyer_contact
------------+-------+--------+------------+--------+-------------------
1 | 23.22 | 1 | 2017-08-16 | zhaosi | xiangyashan222hao
(1 row) --并且查询“父表”也可以查到插入的数据
test=# select * from sales_detail;
product_id | price | amount | sale_date | buyer | buyer_contact
------------+-------+--------+------------+--------+-------------------
1 | 23.22 | 1 | 2017-08-16 | zhaosi | xiangyashan222hao
(1 row) --通过查看执行计划,可以看出当查询数据时,数据库会自动的去sales_detail_Y2017Q03分区表中查找,而不会扫描所有的分区表。
test=# explain select * from sales_detail where sale_date=date'2017-08-16';
QUERY PLAN
----------------------------------------------------------------------------------------------------
Append (cost=0.00..9.50 rows=3 width=158)
-> Seq Scan on sales_detail (cost=0.00..0.00 rows=1 width=158)
Filter: (sale_date = '2017-08-16'::date)
-> Bitmap Heap Scan on sales_detail_y2017q03 (cost=4.16..9.50 rows=2 width=158)
Recheck Cond: (sale_date = '2017-08-16'::date)
-> Bitmap Index Scan on sales_detail_y2017q03_sale_date (cost=0.00..4.16 rows=2 width=0)
Index Cond: (sale_date = '2017-08-16'::date)
(7 rows)

总结:

删除分区表中的子表,不会使触发器失效,只是当向被删除表中插入数据时会报错。

创建分区表过程中的触发器,可以用“规则”来代替,但触发器比“规则”更有优势,再此不再赘述。

The End!

2017-08-17

【PostgreSQL-9.6.3】分区表的更多相关文章

  1. PostgreSQL 与 Oracle 访问分区表执行计划差异

    熟悉Oracle 的DBA都知道,Oracle 访问分区表时,对于没有提供分区条件的,也就是在无法使用分区剪枝情况下,优化器会根据全局的统计信息制定执行计划,该执行计划针对所有分区适用.在分析利弊之前 ...

  2. PostgreSQL 传统 hash 分区方法和性能

    背景 除了传统的基于trigger和rule的分区,PostgreSQL 10开始已经内置了分区功能(目前仅支持list和range),使用pg_pathman则支持hash分区. 从性能角度,目前最 ...

  3. kettle介绍

    Kettle也叫PDI,在2006年Kettle加入了开源的BI组织Pentaho,正式命名为PDI,英文全称为Pentaho Data Integeration.Kettle是"Kettl ...

  4. postgre与mysql区别

    SQL兼容性 PostgreSQL 9.5 兼容 SQL:2011 子集 http://www.postgresql.org/docs/9.5/static/features-sql-standard ...

  5. postgresql 分区表

    1.普通方式建立主表 create table tbl_partition( id integer, name ), gender boolean, join_date date, dept ) ) ...

  6. [原创]PostgreSQL Plus Advanced Server批量创建分区表写入亿级别数据实例

    当前情况:大表的数据量已接近2亿条我的解决思路:为它创建n*100个分区表,将各个分区表放在不同的tablespace上这样做的优点:1.首先是对这个级别的数据表的性能会有所提升2.数据管理更科学3. ...

  7. postgresql分区表探索(pg_pathman)

    使用场景 许多系统在在使用几年之后数据量不断膨胀,这个时候单表数据量超过2000w+,数据库的查询也越来越慢,而随着时间的推移许多历史数据的重要性可能逐渐下降.这时候就可以考虑使用分区表来将冷热数据分 ...

  8. PostgreSQL PARTITION 分区表

    PostgreSQL 分区表,操作性相当便捷. 但只能在创建时决定是否为分区表,并决定分区条件字段,普通表创建后,不能在修改为分区表. Note:通过其他方法也可转化为分区表. 和其他数据库一样,分区 ...

  9. PostgreSQL分区表实现——pg_pathman分区表管理

    该博文用于自己学习记录,内容节选自: https://github.com/digoal/blog/blob/master/201610/20161024_01.md pg_pathman 创建分区表 ...

  10. PostgreSQL分区表实现——pg_pathman安装、配置

    近日由于系统运行时间太长,数据库库表中的数据也是越来越多,为了缩短库表的操作时间,所以对数据库中的部分库表进行分区的操作. 通过研究,决定采用pg_pathman插件对库表进行分区操作.pg_path ...

随机推荐

  1. 仪仗队(codevs 2296)

    题目描述 Description 作为体育委员,C君负责这次运动会仪仗队的训练.仪仗队是由学生组成的N * N的方阵,为了保证队伍在行进中整齐划一,C君会跟在仪仗队的左后方,根据其视线所及的学生人数来 ...

  2. 【CV论文阅读】YOLO:Unified, Real-Time Object Detection

    YOLO的一大特点就是快,在处理上可以达到完全的实时.原因在于它整个检测方法非常的简洁,使用回归的方法,直接在原图上进行目标检测与定位. 多任务检测: 网络把目标检测与定位统一到一个深度网络中,而且可 ...

  3. MySQL 入门(九)—— 查询数据

    查询数据就是从数据库中获取所须要的数据. 1.基本查询语句 即Select语句 当中.属性列表表示要查询的字段名.表名和视图列表表示从此处指定的表或者视图中查询数据.能够有多个:条件表达式1制定了查询 ...

  4. 架构师速成6.7-设计开发思路-uml

    uml是什么东西?统一建模语言.一门语言.是用来进行软件设计的一门语言. 事实上一门语言的诞生并不伟大,让大多数人都使用才足够伟大. uml就是一门伟大的语言.由于眼下软件设计的唯一语言就是它. UM ...

  5. LeetCode 168. Excel Sheet Column Title (Excel 表格列名称)

    Given a positive integer, return its corresponding column title as appear in an Excel sheet. For exa ...

  6. 从头认识java-13.7 什么时候使用泛型?

    这一章节我们来讨论一下什么时候使用泛型? 答案:当你希望代码能够跨多个类型(不同的类型,不包括继承关系)工作的时候. 1.当没有确切类型的时候 以下是错误的代码: package com.ray.ch ...

  7. es bulk 批量删除

    bulk [root@hadoop2 ~]# cat bulk.del.es.json {"delete":{"_index":"direct_vot ...

  8. js20170320

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  9. new (C# Reference)

    https://msdn.microsoft.com/en-us/library/51y09td4.aspx In C#, the new keyword can be used as an oper ...

  10. POJ Area of Simple Polygons 扫描线

    这个题lba等神犇说可以不用离散化,但是我就是要用. 题干: Description There are N, <= N <= , rectangles -D xy-plane. The ...