pgsql 分区表:

--主表
create table test(id integer, name varchar(32));
create index idx_test_id on test using btree(id); --分表
create table test_b (like test including constraints including defaults including indexes) inherits(test);
create table test_c (like test including constraints including defaults including indexes) inherits(test); alter table test_b add constraint con_test_c check(id >=1001 and id <= 2000);
alter table test_c add constraint con_test_b check(id >=2001 and id <= 3000); create table test_d( check(id >=3001 and id <= 4000))inherits (test); --插数据
insert into test_b select generate_series(1001,2000),'bbb';
insert into test_c select generate_series(2001,3000),'ccc';
insert into test_d select generate_series(3001,4000),'ddd'; --分析表,加入计划
analyze test_a; analyze test_b; analyze test_c;
analyze test_d; --测试查询计划 constraint_exclusion
set constraint_exclusion = off/partition;
show constraint_exclusion; explain select * from test where id = 1; --测试Insert
insert into test values (11,'aaaaa'); select * from test where name = 'aaaaa' select * from test_a; insert into test values(1,'a'),(1111,'v'),(2222,'vv'); select * from test_c; --创建触发器函数
create function tb_partition_insert()
returns trigger as
$$
begin
if (id >=1 and id <= 1000) then
insert into test_a values(NEW.*);
elseif (id >=1001 and id <= 2000) then
insert into test_b values(NEW.*);
elseif (id >=2001 and id <= 3000) then
insert into test_c values (new.*);
else
raise exception 'Date out of range. Fix the tbl_partition_insert_trigger() function!';
end if ;
return null;
end
$$
language plpgsql; --触发器
create trigger insert_test_parition
before insert on test
for each row execute procedure tb_partition_insert(); --测试Insert
insert into test values(1,'a'),(1111,'v'),(2222,'vv'); select pg_size_pretty(pg_relation_size('test')); insert into test select generate_series(1,1000),'aa';
select count(1) from test_a; select pg_size_pretty(pg_relation_size('test')); select pg_table_size('test')

参考:

  1. 为主:http://www.cnblogs.com/mchina/archive/2013/04/09/2973427.html
  2. 为辅:http://my.oschina.net/Kenyon/blog/59455 ,http://www.postgres.cn/docs/9.4/ddl-partitioning.html#DDL-PARTITIONING-CONSTRAINT-EXCLUSION
  3. 例子参考:http://francs3.blog.163.com/blog/static/4057672720112422436937/
  4. 结合:http://www.cnblogs.com/stephen-liu74/archive/2012/04/27/2291814.html
  5. 查看表大小等操作:http://www.cnblogs.com/liuyuanyuanGOGO/p/3224554.html
  6. 分区表通过继承实现,在对表进行清除数据操作时,要注意是否会同时清除父表和字表:
  7. http://francs3.blog.163.com/blog/static/40576727201011203725668/

PostgreSQL建立分区表示例的更多相关文章

  1. ubuntu server下建立分区表/分区/格式化/自动挂载(转)

    link:http://www.thxopen.com/linux/2014/03/30/Linux_parted.html 流程为:新建分区-->格式化分区-->挂载分区 首先弄明白分区 ...

  2. MSSQL中建立分区表(转载备忘)

    转载自CSDN地址:http://bbs.csdn.net/topics/330087045 SQL Server 2005 分区表实践——建立分区表(partition table) 问题:有一个订 ...

  3. oracle_根据ID(字符型)建立分区表

    方案思路:有一张暴增的数据表(10亿级别),以后需求需要提高单条查询性能, 这个表有个唯一ID, 假设是UUID,采用区分首字母的方法,不同字母的数据入到不同的物理文件中. 第一步: 查找数据库服务器 ...

  4. hive之建立分区表和分区

    1. 建立分区表 create table 单分区表:其中分区字段是partdate,注意分区字段不能和表字段一样,否则会报重复的错 create table test_t2(words string ...

  5. PostgreSQL PARTITION 分区表

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

  6. SQL Server 2008 建立分区表 脚本

    /*第一步:创建分区函数*/Create partition function Part_func_Bag(varchar(20)) as range right /*正式区间for values(N ...

  7. Shell脚本运行hive语句 | hive以日期建立分区表 | linux schedule程序 | sed替换文件字符串 | shell推断hdfs文件文件夹是否存在

    #!/bin/bash source /etc/profile; ################################################## # Author: ouyang ...

  8. SQL分区表示例

    -- Create tablecreate table TT_FVP_OCR_ADDRESS( id NUMBER not null, waybill_no VARCHAR2(32) not null ...

  9. jchdl - 初次使用建立项目示例

    https://mp.weixin.qq.com/s/HaarKjpHan08RUTlEX0XHg   一. 下载并安装JDK 8   下载链接:https://www.oracle.com/tech ...

随机推荐

  1. MemberwiseClone和DeepClone

    文章转自:http://www.cnblogs.com/zhangji/archive/2011/02/23/1961897.html MemberwiseClone 方法创建一个浅表副本,具体来说就 ...

  2. FZU 1894 志愿者选拔(单调队列)

    传送门 Description 世博会马上就要开幕了,福州大学组织了一次志愿者选拔活动.参加志愿者选拔的同学们排队接受面试官们的面试.参加面试的同学们按照先来先面试并且先结束的原则接受面试官们的考查. ...

  3. JBoss AS7 快速配置

    作者:MinUnix  原文出处:http://www.minunix.com/2013/08/jboss-as7-01/  如需转载请注明出处! 文档下载:http://www.minunix.co ...

  4. 我所了解的meta

    https://github.com/hoosin/mobile-web-favorites 总况 meta 标签分两大部分:HTTP 标题信息(http-equiv)和页面描述信息(name). h ...

  5. iOS - 全屏滑动

    取经地址 1.使用关联 关联是指把两个对象相互关联起来,使得其中的一个对象作为另一个对象的一部分. 使用关联,是基于关键字的,因此,我们可以为任意对象增加任意多的关联,但是关键字是唯一的.关联可以保证 ...

  6. iOS内存管理个人总结

    一.变量,本质代表一段可以操作的内存,她使用方式无非就是内存符号化+数据类型 1.保存变量有三个区域: 1>静态存储区 2>stack 3>heap 2.变量又根据声明的位置有两种称 ...

  7. BZOJ4446: [Scoi2015]小凸玩密室

    用ui,j表示走完i的子树后走到i的深度为j的祖先的兄弟的最小代价: 用vi,j表示走完i的子树后走到i的深度为j的祖先的最小代价,用u算出v. 枚举起点,计算答案. #include<bits ...

  8. NXP Mifare S50标准IC卡- 访问位(Access Bits) 分析

    Mifare S50 标准IC卡有1K 字节的EEPROM,主要用来存储数据和控制信息.1K 字节的EEPROM分成16 个区,每区又分成4 段,每1段中有16 个字节.每个区的最后一个段叫“尾部&q ...

  9. Python2.7安装(win7)

    Python可在官方网站直接下,或者百度一下Python2.7下载,这里推荐使用2.7而不是3.3,比较适合初学者 工具/原料 win7系统 python2.7安装包 方法/步骤 1.从官网下载最新的 ...

  10. ubuntu下安装wine1.8和阿里旺旺

    参考:http://www.linuxidc.com/Linux/2015-12/126722.htm和http://www.linuxidc.com/Linux/2016-05/131131.htm ...