POSTGRESQL的分区和MYSQL不同,MYSQL是有专门的分区表, 而POSTGRESQL的分区则利用它本身的面向对象的特性来做。 下面我们来简单的体验下。

我们先创建一张父表。 记住,所有的分区表都得继承他。

t_girl=# create table num_master (id int not null primary key);
CREATE TABLE

接下来我们创建一个简单的函数来动态创建分区表。

t_girl=# create or replace function create_partition_table () returns void as $$
t_girl$# declare i int;
t_girl$# declare cnt int;
t_girl$# declare stmt text;
t_girl$# begin
t_girl$# -- Created by ytt at 2013/12/15. Dynamic creating partition tables.
t_girl$# i:= 0;
t_girl$# cnt:=4;
t_girl$# <<lable1>> while i < cnt loop
t_girl$#   stmt := 'create table num_slave'||i+1||'(check(id >='||i*100||' and id <'||(i+1)*100||')) inherits(num_master)';
t_girl$#   execute stmt;
t_girl$#   i:=i + 1;
t_girl$# end loop lable1;
t_girl$# return;
t_girl$# end;
t_girl$# $$ language plpgsql;
CREATE FUNCTION
t_girl=#

OK。 现在可以执行了。

t_girl=# select create_partition_table();create_partition_table
------------------------
(1 row)

列出所有的表

t_girl=# \dList of relationsSchema |    Name    | Type  |  Owner
--------+------------+-------+----------ytt    | num_master | table | postgresytt    | num_slave1 | table | postgresytt    | num_slave2 | table | postgresytt    | num_slave3 | table | postgresytt    | num_slave4 | table | postgresytt    | t1         | table | t_girl
(6 rows)

我们针对父表建立一个触发器函数体,对应其分区表的数据分布。

t_girl=# create or replace function num_insert_trigger()
t_girl-# returns trigger as $$
t_girl$# begin
t_girl$# -- Created by ytt at 2013/12/15. Do how to distribute data.
t_girl$# if (new.id >=0 and new.id <100) then
t_girl$# insert into num_slave1 values (new.*);
t_girl$# elsif (new.id >=100 and new.id <200) then
t_girl$# insert into num_slave2 values(new.*);
t_girl$# elsif (new.id >=200 and new.id <300) then
t_girl$# insert into num_slave3 values (new.*);
t_girl$# elsif (new.id >=300 and new.id <400) then
t_girl$# insert into num_slave4 values (new.*);
t_girl$# else
t_girl$# raise exception 'Column id out of range.';
t_girl$# end if;
t_girl$# return null;
t_girl$# end;
t_girl$# $$
t_girl-# language plpgsql;
CREATE FUNCTION

我们看看已经建好的触发器:

t_girl=# \d+ num_masterTable "ytt.num_master"Column |  Type   | Modifiers | Storage | Stats target | Description
--------+---------+-----------+---------+--------------+-------------id     | integer | not null  | plain   |              |
Indexes:"num_master_pkey" PRIMARY KEY, btree (id)
Triggers:insert_num_slave_trigger BEFORE INSERT ON num_master FOR EACH ROW EXECUTE PROCEDURE ytt.num_insert_trigger()
Child tables: num_slave1,num_slave2,num_slave3,num_slave4
Has OIDs: no

我们现在生成简单的测试数据。

t_girl=# select func_create_sample_data();func_create_sample_data
-------------------------
(1 row)

上面的函数生成了大概400行的数据。

为了查看优化器是如何处理查询的,我们来看看简单的查询

t_girl=# explain select * from num_master where id > 30 and id < 120;QUERY PLAN
-----------------------------------------------------------------Append  (cost=0.00..5.00 rows=91 width=4)->  Seq Scan on num_master  (cost=0.00..0.00 rows=1 width=4)Filter: ((id > 30) AND (id < 120))->  Seq Scan on num_slave1  (cost=0.00..2.50 rows=70 width=4)Filter: ((id > 30) AND (id < 120))->  Seq Scan on num_slave2  (cost=0.00..2.50 rows=20 width=4)Filter: ((id > 30) AND (id < 120))
(7 rows)
t_girl=#

我也是今天刚刚接触到POSTGRESQL的分区表,有问题,还希望提出。

转载于:https://blog.51cto.com/yueliangdao0608/1340860

【原创】POSTGRESQL 分区表初次体验相关推荐

  1. ASP.NET Core Identity 实战(1)——Identity 初次体验

    ASP.NET Core Identity是用于构建ASP.NET Core Web应用程序的成员资格系统,包括成员资格.登录和用户数据存储 这是来自于 ASP.NET Core Identity 仓 ...

  2. jQuery课程介绍、Query的介绍、Query初次体验、jQuery再次体验、jQuery中的顶级对象

    jQuery课程介绍 <!DOCTYPE html> <html lang="en"> <head><meta charset=" ...

  3. Django初次体验

    Django初次体验 关于django的安装,宝宝们可以参考django简介以及安装 Django框架的搭建 在终端中进入需要建立项目的目录 执行: django-admin startproject ...

  4. 【RT-Thread Smart】ART-Pi Smart 开发板开箱及爱之初次体验

    ART-Pi Smart 开发板开箱及爱之初次体验 首先, 感谢RT=Thread给的试用机会, 感谢熊大的指导. 本文ART-Pi Smart开箱及初次体验内置应用 标题开箱 收到有几天了.我们一步 ...

  5. PostgreSQL 分区表教程

    由于数据库中存储了大量数据,性能和可伸缩性是受到影响的两个主要因素.随着加载表数据的增加,需要更多的数据扫描.页交换到内存,以及其他表操作成本也会增加.分区可能是一个很好的解决方案,因为它可以将一个大 ...

  6. deepin win10字体_购机指南 | Deepin 初次体验,国产OS还有很长路要走

    2020版购机指南目录 购机指南 | 剁手前一分钟的挣扎,最终还是下船了​mp.weixin.qq.com 购机指南 | 入手Magicbook 14 Linux版,冲着多屏协同​mp.weixin. ...

  7. Xxl-Job 初次体验

    Xxl-Job 初次体验 一.定时任务-前置知识 二.演变机制 三.xxl-Job 设计思想 四.xxl-job 实战 1. 调度中心部署 2. 编写执行器简单使用一下 2.1. 让执行器run起来! ...

  8. PostgreSQL分区表(partitioning)应用实例详解

    https://www.jb51.net/article/97937.htm PostgreSQL分区表(partitioning)应用实例详解 更新时间:2016年11月22日 10:25:58   ...

  9. PostgreSQL 分区表, pg_pathman ,PostgreSQL 10介绍及性能对比(转载)

    转载自:https://my.oschina.net/yonj1e/blog/868402 PostgreSQL 分区表, pg_pathman ,PostgreSQL 10介绍及性能对比 原 yon ...

最新文章

  1. R语言中if else、which、%in%的用法
  2. Wireshark数据抓包教程之认识捕获分析数据包
  3. oracle中类似indexof用法_instr函数
  4. UPC个人训练赛第十五场(AtCoder Grand Contest 031)
  5. MenuItem创建注意事项
  6. android开发之shape详解
  7. Z-Stack Home Developer's Guide—3. The Home Automation Profile and the Sample Applications中文翻译
  8. C# 在类文件自动添加文件注释的方法
  9. basys2数码管共阳还是共阴_如何判断PLC使用接近开关是PNP还是NPN?
  10. nginx——rewrite模块
  11. AdventureWorks2012.mdf的使用
  12. android启动序列帧动画,关于 Lottie 动画的说明及应用
  13. 公司装了cofax传真服务器真的解决了收发传真时存在的诸多不便
  14. 【C++ Builder 11】选择文件夹的三种方式
  15. O(lg m + lgn)时间复杂度求两个有序序列合并后第K大的数
  16. linux编译lame,linux 下安装lame以及tritonus-mp3enc
  17. windows下安装禅道
  18. 二十六 .ajax登录 认证 验证码(session)
  19. SEO关键词排名优化做到百度首页的核心操作
  20. 2022最新软件测试面试题

热门文章

  1. 软件测试2019:第七次作业—— 用户体验测试
  2. python-day22(序列化)
  3. Stanford cs224n 第三课: GloVe 代码解读
  4. Lua------------------unity与lua的热更新
  5. C#判断奇偶数的函数
  6. spring 找不到applicationContext.xml解决方法
  7. String.Format使用方法
  8. HTML5和CSS3响应式WEB设计指南译者序
  9. 从数据库中存取二进制数据并显示
  10. 解决win8.1企业版安装WP8 SDK出现“根据当前系统时钟或签名文件中的时间戳验证时要求的证书不在有效期内”的问题