【PostgreSQL-9.6.3】分区表
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】分区表的更多相关文章
- PostgreSQL 与 Oracle 访问分区表执行计划差异
熟悉Oracle 的DBA都知道,Oracle 访问分区表时,对于没有提供分区条件的,也就是在无法使用分区剪枝情况下,优化器会根据全局的统计信息制定执行计划,该执行计划针对所有分区适用.在分析利弊之前 ...
- PostgreSQL 传统 hash 分区方法和性能
背景 除了传统的基于trigger和rule的分区,PostgreSQL 10开始已经内置了分区功能(目前仅支持list和range),使用pg_pathman则支持hash分区. 从性能角度,目前最 ...
- kettle介绍
Kettle也叫PDI,在2006年Kettle加入了开源的BI组织Pentaho,正式命名为PDI,英文全称为Pentaho Data Integeration.Kettle是"Kettl ...
- postgre与mysql区别
SQL兼容性 PostgreSQL 9.5 兼容 SQL:2011 子集 http://www.postgresql.org/docs/9.5/static/features-sql-standard ...
- postgresql 分区表
1.普通方式建立主表 create table tbl_partition( id integer, name ), gender boolean, join_date date, dept ) ) ...
- [原创]PostgreSQL Plus Advanced Server批量创建分区表写入亿级别数据实例
当前情况:大表的数据量已接近2亿条我的解决思路:为它创建n*100个分区表,将各个分区表放在不同的tablespace上这样做的优点:1.首先是对这个级别的数据表的性能会有所提升2.数据管理更科学3. ...
- postgresql分区表探索(pg_pathman)
使用场景 许多系统在在使用几年之后数据量不断膨胀,这个时候单表数据量超过2000w+,数据库的查询也越来越慢,而随着时间的推移许多历史数据的重要性可能逐渐下降.这时候就可以考虑使用分区表来将冷热数据分 ...
- PostgreSQL PARTITION 分区表
PostgreSQL 分区表,操作性相当便捷. 但只能在创建时决定是否为分区表,并决定分区条件字段,普通表创建后,不能在修改为分区表. Note:通过其他方法也可转化为分区表. 和其他数据库一样,分区 ...
- PostgreSQL分区表实现——pg_pathman分区表管理
该博文用于自己学习记录,内容节选自: https://github.com/digoal/blog/blob/master/201610/20161024_01.md pg_pathman 创建分区表 ...
- PostgreSQL分区表实现——pg_pathman安装、配置
近日由于系统运行时间太长,数据库库表中的数据也是越来越多,为了缩短库表的操作时间,所以对数据库中的部分库表进行分区的操作. 通过研究,决定采用pg_pathman插件对库表进行分区操作.pg_path ...
随机推荐
- [bzoj4033][HAOI2015]树上染色_树形dp
树上染色 bzoj-4033 HAOI-2015 题目大意:给定一棵n个点的树,让你在其中选出k个作为黑点,其余的是白点,收益为任意两个同色点之间距离的和.求最大收益. 注释:$1\le n\le 2 ...
- HDU——1133 Buy the Ticket
Buy the Ticket Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- Unix stat
Linux 下有stat命令,可以非常方便的得到一个文件的inode等信息.但是今天在Solaris下使用stat居然没有这个命令.不过没关系,我们可以自己写这个命令,比如: #include < ...
- Unity uGui RawImage 渲染小地图
制作类似 RPG 游戏时,可能会须要显示小地图. 小地图的制作一种方式是用还有一个摄像机来渲染到一张纹理上.实时显示到UI界面. 以Unity 5.0 的 UI 系统为例: 在地图正上方放置一个摄像机 ...
- 关于new和malloc以及delete和free能否够混用
/* *1>当申请的空间是内置类型时,delete和free能够混用 *2>当申请的空间是自己定义类型时, * 1>若没有析构函数.delete和malloc能够混用.有 ...
- configure: error: mysql configure failed. Please check config.log for more information.
为php添加mysql模块时报错 configure: error: mysql configure failed. Please check config.log for more informat ...
- OpenStack二三事(2)
使用devstack在virtualbox上安装openstack还真是比較麻烦,到处都是坑.近期碰到的坑是在tempest上,在执行verify-tempest-config时,代码中import了 ...
- B. Case of Fake Numbers( Codeforces Round #310 (Div. 2) 简单题)
B. Case of Fake Numbers time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- Apache Kafka-0.8.1.1源代码编译
作者:过往记忆 | 新浪微博:左手牵右手TEL | 能够转载, 但必须以超链接形式标明文章原始出处和作者信息及版权声明博客地址:http://www.iteblog.com/文章标题:<Apac ...
- mp3播放时间
import os os_sep = os.sep this_file_abspath = os.path.abspath(__file__) this_file_dirname, this_file ...