当前位置: 首页 > news >正文

PostgreSQL的分区表建立

为什么80%的码农都做不了架构师?>>>   hot3.png

在数据库日渐庞大的时候,为了方便对数据库数据的管理,比如按时间,按地区去统计一些数据时,基数过于庞大,多有不便。很多商业数据库都提供分区的概念,按不同的维度去存放数据,便于后期的管理,PG也不例外。下面是分区表创建步骤:

1.建立主表

create table parent_table( id int, name character varying(20), create_time timestamp without time zone);

2.建立子表,继承于主表

create table parent_table_2012_01( check (create_time>=date '2012-01-01' and create_time<date '2012-02-01')) inherits(parent_table);

create table parent_table_2012_02( check (create_time>=date '2012-02-01' and create_time<date '2012-03-01')) inherits(parent_table);

create table parent_table_2012_03( check (create_time>=date '2012-03-01' and create_time<date '2012-04-01')) inherits(parent_table);

create table parent_table_2012_04( check (create_time>=date '2012-04-01' and create_time<date '2012-05-01')) inherits(parent_table);

create table parent_table_2012_05( check (create_time>=date '2012-05-01' and create_time<date '2012-06-01')) inherits(parent_table);

create table parent_table_2012_06( check (create_time>=date '2012-06-01' and create_time<date '2012-07-01')) inherits(parent_table);

create table parent_table_2012_07( check (create_time>=date '2012-07-01' and create_time<date '2012-08-01')) inherits(parent_table);

create table parent_table_2012_08( check (create_time>=date '2012-08-01' and create_time<date '2012-09-01')) inherits(parent_table);

create table parent_table_2012_09( check (create_time>=date '2012-09-01' and create_time<date '2012-10-01')) inherits(parent_table);

create table parent_table_2012_10( check (create_time>=date '2012-10-01' and create_time<date '2012-11-01')) inherits(parent_table);

create table parent_table_2012_11( check (create_time>=date '2012-11-01' and create_time<date '2012-12-01')) inherits(parent_table);

create table parent_table_2012_12( check (create_time>=date '2012-12-01' and create_time<date '2013-01-01')) inherits(parent_table);

3.创建触发器函数

CREATE OR REPLACE FUNCTION test.tri_parent_tab_insert()

RETURNS TRIGGER AS $$

--author: kenyon

--created:2012-05-24

BEGIN

IF ( NEW.create_time >= DATE '2012-01-01' AND  

     NEW.create_time < DATE '2012-02-01' ) THEN  

    INSERT INTO test.parent_table_2012_01 VALUES (NEW.id,NEW.name,NEW.create_time);   

ELSIF ( NEW.create_time >= DATE '2012-02-01' AND  

        NEW.create_time < DATE '2012-03-01' ) THEN  

    INSERT INTO test.parent_table_2012_02 VALUES (NEW.id,NEW.name,NEW.create_time);   

ELSIF ( NEW.create_time >= DATE '2012-03-01' AND  

        NEW.create_time < DATE '2012-04-01' ) THEN  

    INSERT INTO test.parent_table_2012_03 VALUES (NEW.id,NEW.name,NEW.create_time);   

ELSIF ( NEW.create_time >= DATE '2012-04-01' AND  

        NEW.create_time < DATE '2012-05-01' ) THEN  

    INSERT INTO test.parent_table_2012_04 VALUES (NEW.id,NEW.name,NEW.create_time);   

ELSIF ( NEW.create_time >= DATE '2012-05-01' AND  

        NEW.create_time < DATE '2012-06-01' ) THEN  

    INSERT INTO test.parent_table_2012_05 VALUES (NEW.id,NEW.name,NEW.create_time);   

ELSIF ( NEW.create_time >= DATE '2012-06-01' AND  

        NEW.create_time < DATE '2012-07-01' ) THEN  

    INSERT INTO test.parent_table_2012_06 VALUES (NEW.id,NEW.name,NEW.create_time);   

ELSIF ( NEW.create_time >= DATE '2012-07-01' AND  

        NEW.create_time < DATE '2012-08-01' ) THEN  

    INSERT INTO test.parent_table_2012_07 VALUES (NEW.id,NEW.name,NEW.create_time);   

ELSIF ( NEW.create_time >= DATE '2012-08-01' AND  

        NEW.create_time < DATE '2012-09-01' ) THEN  

    INSERT INTO test.parent_table_2012_08 VALUES (NEW.id,NEW.name,NEW.create_time);   

ELSIF ( NEW.create_time >= DATE '2012-09-01' AND  

        NEW.create_time < DATE '2012-10-01' ) THEN  

    INSERT INTO test.parent_table_2012_09 VALUES (NEW.id,NEW.name,NEW.create_time);   

ELSIF ( NEW.create_time >= DATE '2012-10-01' AND  

        NEW.create_time < DATE '2012-11-01' ) THEN  

    INSERT INTO test.parent_table_2012_10 VALUES (NEW.id,NEW.name,NEW.create_time);   

ELSIF ( NEW.create_time >= DATE '2012-11-01' AND  

        NEW.create_time < DATE '2012-12-01' ) THEN  

    INSERT INTO test.parent_table_2012_11 VALUES (NEW.id,NEW.name,NEW.create_time);   

ELSIF ( NEW.create_time >= DATE '2012-12-01' AND  

        NEW.create_time < DATE '2013-01-01' ) THEN  

    INSERT INTO test.parent_table_2012_12 VALUES (NEW.id,NEW.name,NEW.create_time);   

ELSE  

    RAISE EXCEPTION 'Date out of range.Fix the test.parent_table_insert_trigger() function!';   

END IF;   

RETURN NULL;   

END;

$$

LANGUAGE plpgsql;

4.创建触发器

CREATE TRIGGER tri_insert_parent_table

BEFORE INSERT ON test.parent_table   

FOR EACH ROW EXECUTE PROCEDURE test.tri_parent_tab_insert(); 

5.测试 至此就OK了。前端插入时只要插入主表就可以自动将数据按时间分类分插到子表里去。 插入一定的测试数据,来看看效果

kenyon=# select count(1) from test.parent_table_2012_03;

count


2293760

(1 row)

kenyon=# select count(1) from test.parent_table;

count


2293761

(1 row)

kenyon=# select pg_size_pretty(pg_relation_size('test.parent_table_2012_03'));

pg_size_pretty


106 MB

(1 row)

kenyon=# select pg_size_pretty(pg_relation_size('test.parent_table'));

pg_size_pretty


8192 bytes

(1 row)

6.总结: a.可以看到实际的数据是存放在子表里去了,父表是没数据的。 b.这么做前端开发会省去不少工作,但是后端DB会增加不少压力,可以后端建好分区表,前端直接按时间插入分区表中去,可减少因触发器带来的DB压力。 c.可以单独对分区表进行DML或者DDL操作,如truncate。 d.通过explain查看查询是否走得分区,如果未走分区,检查SQL语法和与之相关的系统参数,如constraint_exclusion是否是partition的

转载于:https://my.oschina.net/nway/blog/356943

相关文章:

  • python的tab自动补全
  • 使用 NuGet 更新套件時將 jQuery 升級到 2.0.2 應該如何降級
  • jquery radio取值,checkbox取值,select取值,radio选中,checkbox选中,select选中,及其相关...
  • 微软职位内部推荐-SW Engineer II for Embedded System
  • JS模块化
  • jQuery事件绑定 bind、 live、delegate和on的区别
  • 注册表检测 ms14-058 CVE-2014-4113
  • 通过java的Runtime.getRuntime()和System.getProperties()来获取系统的信息
  • myeclipse新建工程时的选择
  • Android自定义横向的ProgressBar
  • 真正高效的SQLSERVER分页查询(多种方案)
  • KVM虚拟化技术简介
  • 【Spark亚太研究院系列丛书】Spark实战高手之路-第3章Spark架构设计与编程模型第3节②...
  • 我对于 setWindow, setViewPort 与 translate 的理解
  • DG_Oracle DataGuard Failover主备节点切换(案例)
  • [译] 理解数组在 PHP 内部的实现(给PHP开发者的PHP源码-第四部分)
  • 《网管员必读——网络组建》(第2版)电子课件下载
  • 3.7、@ResponseBody 和 @RestController
  • Angular6错误 Service: No provider for Renderer2
  • dva中组件的懒加载
  • Electron入门介绍
  • gcc介绍及安装
  • interface和setter,getter
  • java多线程
  • Mac转Windows的拯救指南
  • nodejs:开发并发布一个nodejs包
  • Windows Containers 大冒险: 容器网络
  • 构造函数(constructor)与原型链(prototype)关系
  • 基于HAProxy的高性能缓存服务器nuster
  • 马上搞懂 GeoJSON
  • 猫头鹰的深夜翻译:Java 2D Graphics, 简单的仿射变换
  • 面试题:给你个id,去拿到name,多叉树遍历
  • 微信小程序--------语音识别(前端自己也能玩)
  • 用Canvas画一棵二叉树
  • 用mpvue开发微信小程序
  • 在Docker Swarm上部署Apache Storm:第1部分
  • ​如何在iOS手机上查看应用日志
  • #etcd#安装时出错
  • (20)目标检测算法之YOLOv5计算预选框、详解anchor计算
  • (3)选择元素——(17)练习(Exercises)
  • (ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY)讲解
  • (二)Eureka服务搭建,服务注册,服务发现
  • (十五)使用Nexus创建Maven私服
  • (转)Java socket中关闭IO流后,发生什么事?(以关闭输出流为例) .
  • .bat批处理(七):PC端从手机内复制文件到本地
  • .Net core 6.0 升8.0
  • .NET DataGridView数据绑定说明
  • .net web项目 调用webService
  • .net开发引用程序集提示没有强名称的解决办法
  • .Net小白的大学四年,内含面经
  • @RequestParam,@RequestBody和@PathVariable 区别
  • @require_PUTNameError: name ‘require_PUT‘ is not defined 解决方法
  • @WebService和@WebMethod注解的用法
  • [ vulhub漏洞复现篇 ] JBOSS AS 4.x以下反序列化远程代码执行漏洞CVE-2017-7504
  • [Django 0-1] Core.Handlers 模块