postgresql 分表

本贴最后更新于 2436 天前,其中的信息可能已经东海扬尘

一:概述
    在客户数据积累到一定程度后,尤其是单表数据达到千万级别的时候,随着查询速度越来越慢以及数据管理等需求,就要考虑分表,pg 的分表把逻辑上的一个大表分割成物理上的几块,分表后,不仅带来查询速度的提升,数据管理与维护也方便了许多(把逻辑上很大的一块按照某个维度分成了 N 个小块)。
二:分表
    2.1 创建主表
    create table t_student(
     student_id serial8 primary key,
     name varchar(255),
     age int4,
     create_date date
    )
    2.2 创建分区表
    create table t_student_1_1000(check (student_id >=1 and student_id <1000)) inherits(t_student);
    create table t_student_1000_2000(check (student_id >=1000 and student_id <2000)) inherits(t_student);
    create table t_student_2000_3000(check (student_id >=2000 and student_id <3000)) inherits(t_student);
    2.3 创建触发器函数
    create or replace function t_student_insert_trigger()
    returns trigger
    language plpgsql
    as

&nbsp;&nbsp;&nbsp;&nbsp;begin &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(new.student_id >=1 and new.student_id < 1000) then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;insert into t_student_1_1000 values(new.*); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elsif(new.student_id >=1000 and new.student_id < 2000) then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;insert into t_student_1000_2000 values(new.*); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elsif(new.student_id >=2000 and new.student_id < 3000) then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;insert into t_student_2000_3000 values(new.*); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;raise exception 'student_id out of range.Fix the t_student_insert_trigger() function!'; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end if; &nbsp;&nbsp;&nbsp;&nbsp;return null; &nbsp;&nbsp;&nbsp;&nbsp;end; &nbsp;&nbsp;&nbsp;&nbsp;

    2.4 创建触发器
    create trigger insert_t_student_parttion_trigger before insert on t_student for each row execute procedure t_student_insert_trigger();
三:测试
    3.1 插入数据
    insert into t_student (student_id,name,age,create_date) values(1,'zhangsan',21,now());
    insert into t_student (student_id,name,age,create_date) values(2,'lisi',21,now());
    insert into t_student (student_id,name,age,create_date) values(1000,'wangwu',21,now());
    insert into t_student (student_id,name,age,create_date) values(1001,'zhaoliu',21,now());
    insert into t_student (student_id,name,age,create_date) values(2000,'zhaoqi',21,now());
    insert into t_student (student_id,name,age,create_date) values(2001,'wangba`',21,now());
    3.2 查看数据
    3.2.1 查看主表数据
    postgres=# select * from t_student;
    student_id | name | age | create_date
    ------------+----------+-----+-------------
     1 | zhangsan | 21 | 2018-09-11

     2 | lisi | 21 | 2018-09-11

     1000 | wangwu | 21 | 2018-09-11

     1001 | zhaoliu | 21 | 2018-09-11

     2000 | zhaoqi | 21 | 2018-09-11

     2001 | wangba` | 21 | 2018-09-11
    3.2.2 查看分区表数据
    postgres=# select * from t_student_1_1000 ;

    student_id | name | age | create_date

    ------------+----------+-----+-------------

     1 | zhangsan | 21 | 2018-09-11

     2 | lisi | 21 | 2018-09-11

    (2 rows)

    postgres=# select * from t_student_1000_2000 ;

     student_id | name | age | create_date

    ------------+---------+-----+-------------

     1000 | wangwu | 21 | 2018-09-11

     1001 | zhaoliu | 21 | 2018-09-11

    (2 rows)

    postgres=# select * from t_student_2000_3000 ;

     student_id | name | age | create_date

    ------------+---------+-----+-------------

     2000 | zhaoqi | 21 | 2018-09-11

     2001 | wangba` | 21 | 2018-09-11
    3.3 执行
    postgres=# explain select * from t_student where student_id between 1 and 10;

     QUERY PLAN

    -------------------------------------------------------------------------

     Append (cost=0.00..12.10 rows=2 width=532)

     -> Seq Scan on t_student (cost=0.00..0.00 rows=1 width=532)

     Filter: ((student_id >= 1) AND (student_id <= 10))

     -> Seq Scan on t_student_1_1000 (cost=0.00..12.10 rows=1 width=532)

     Filter: ((student_id >= 1) AND (student_id <= 10))

    (5 rows)
四:注意事项
    4.1 主表一定不能有数据,如果在已有数据的表上做分表操作的话,先对原来的表数据进行备份,然后进行表数据删除处理,等分表跟触发器都创建完毕后,再从备份恢复数据,数据就会插入到各自的分区表里
    4.2 一定要注意触发器函数的 range 范围,最好把未来几年的全部创建好,否则插入数据超出范围的话会报 exception,如果插入新的分区表,只需要创建新的分区表跟更新触发器函数即可
    4.3 每个业务的 range 定义字段都有所不同,id 分的话,每个分区表的数据都会比较均匀,也可以按照时间字段等去划分
    4.4 where 查询如果字段包含分区字段的话,会去相关的分区表里去检索数据,如果有其他常用的检索字段的话,请自行在分区表里增加索引字段提高更快速的检索速度
五:参考资料
    pg 官方文档:https://www.postgresql.org/docs/10/static/ddl-partitioning.html

  • 数据库

    据说 99% 的性能瓶颈都在数据库。

    345 引用 • 747 回帖
  • PostgreSQL

    PostgreSQL 是一款功能强大的企业级数据库系统,在 BSD 开源许可证下发布。

    22 引用 • 22 回帖 • 2 关注
  • 分区表
    1 引用

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...

推荐标签 标签

  • CSDN

    CSDN (Chinese Software Developer Network) 创立于 1999 年,是中国的 IT 社区和服务平台,为中国的软件开发者和 IT 从业者提供知识传播、职业发展、软件开发等全生命周期服务,满足他们在职业发展中学习及共享知识和信息、建立职业发展社交圈、通过软件开发实现技术商业化等刚性需求。

    14 引用 • 155 回帖 • 1 关注
  • TGIF

    Thank God It's Friday! 感谢老天,总算到星期五啦!

    290 引用 • 4494 回帖 • 651 关注
  • Firefox

    Mozilla Firefox 中文俗称“火狐”(正式缩写为 Fx 或 fx,非正式缩写为 FF),是一个开源的网页浏览器,使用 Gecko 排版引擎,支持多种操作系统,如 Windows、OSX 及 Linux 等。

    7 引用 • 30 回帖 • 385 关注
  • BND

    BND(Baidu Netdisk Downloader)是一款图形界面的百度网盘不限速下载器,支持 Windows、Linux 和 Mac,详细介绍请看这里

    107 引用 • 1281 回帖 • 34 关注
  • SendCloud

    SendCloud 由搜狐武汉研发中心孵化的项目,是致力于为开发者提供高质量的触发邮件服务的云端邮件发送平台,为开发者提供便利的 API 接口来调用服务,让邮件准确迅速到达用户收件箱并获得强大的追踪数据。

    2 引用 • 8 回帖 • 499 关注
  • Postman

    Postman 是一款简单好用的 HTTP API 调试工具。

    4 引用 • 3 回帖
  • Logseq

    Logseq 是一个隐私优先、开源的知识库工具。

    Logseq is a joyful, open-source outliner that works on top of local plain-text Markdown and Org-mode files. Use it to write, organize and share your thoughts, keep your to-do list, and build your own digital garden.

    7 引用 • 69 回帖 • 1 关注
  • wolai

    我来 wolai:不仅仅是未来的云端笔记!

    2 引用 • 14 回帖 • 5 关注
  • Kubernetes

    Kubernetes 是 Google 开源的一个容器编排引擎,它支持自动化部署、大规模可伸缩、应用容器化管理。

    118 引用 • 54 回帖 • 5 关注
  • 旅游

    希望你我能在旅途中找到人生的下一站。

    96 引用 • 901 回帖
  • Notion

    Notion - The all-in-one workspace for your notes, tasks, wikis, and databases.

    10 引用 • 77 回帖
  • 大疆创新

    深圳市大疆创新科技有限公司(DJI-Innovations,简称 DJI),成立于 2006 年,是全球领先的无人飞行器控制系统及无人机解决方案的研发和生产商,客户遍布全球 100 多个国家。通过持续的创新,大疆致力于为无人机工业、行业用户以及专业航拍应用提供性能最强、体验最佳的革命性智能飞控产品和解决方案。

    2 引用 • 14 回帖 • 1 关注
  • IPFS

    IPFS(InterPlanetary File System,星际文件系统)是永久的、去中心化保存和共享文件的方法,这是一种内容可寻址、版本化、点对点超媒体的分布式协议。请浏览 IPFS 入门笔记了解更多细节。

    21 引用 • 245 回帖 • 228 关注
  • Flume

    Flume 是一套分布式的、可靠的,可用于有效地收集、聚合和搬运大量日志数据的服务架构。

    9 引用 • 6 回帖 • 655 关注
  • BookxNote

    BookxNote 是一款全新的电子书学习工具,助力您的学习与思考,让您的大脑更高效的记忆。

    笔记整理交给我,一心只读圣贤书。

    1 引用 • 1 回帖
  • MySQL

    MySQL 是一个关系型数据库管理系统,由瑞典 MySQL AB 公司开发,目前属于 Oracle 公司。MySQL 是最流行的关系型数据库管理系统之一。

    693 引用 • 537 回帖 • 1 关注
  • Bug

    Bug 本意是指臭虫、缺陷、损坏、犯贫、窃听器、小虫等。现在人们把在程序中一些缺陷或问题统称为 bug(漏洞)。

    76 引用 • 1742 回帖 • 7 关注
  • 生活

    生活是指人类生存过程中的各项活动的总和,范畴较广,一般指为幸福的意义而存在。生活实际上是对人生的一种诠释。生活包括人类在社会中与自己息息相关的日常活动和心理影射。

    230 引用 • 1454 回帖 • 1 关注
  • BAE

    百度应用引擎(Baidu App Engine)提供了 PHP、Java、Python 的执行环境,以及云存储、消息服务、云数据库等全面的云服务。它可以让开发者实现自动地部署和管理应用,并且提供动态扩容和负载均衡的运行环境,让开发者不用考虑高成本的运维工作,只需专注于业务逻辑,大大降低了开发者学习和迁移的成本。

    19 引用 • 75 回帖 • 668 关注
  • Rust

    Rust 是一门赋予每个人构建可靠且高效软件能力的语言。Rust 由 Mozilla 开发,最早发布于 2014 年 9 月。

    58 引用 • 22 回帖 • 6 关注
  • flomo

    flomo 是新一代 「卡片笔记」 ,专注在碎片化时代,促进你的记录,帮你积累更多知识资产。

    6 引用 • 143 回帖 • 3 关注
  • Solo

    Solo 是一款小而美的开源博客系统,专为程序员设计。Solo 有着非常活跃的社区,可将文章作为帖子推送到社区,来自社区的回帖将作为博客评论进行联动(具体细节请浏览 B3log 构思 - 分布式社区网络)。

    这是一种全新的网络社区体验,让热爱记录和分享的你不再感到孤单!

    1441 引用 • 10069 回帖 • 495 关注
  • abitmean

    有点意思就行了

    34 关注
  • GitBook

    GitBook 使您的团队可以轻松编写和维护高质量的文档。 分享知识,提高团队的工作效率,让用户满意。

    3 引用 • 8 回帖
  • 微软

    微软是一家美国跨国科技公司,也是世界 PC 软件开发的先导,由比尔·盖茨与保罗·艾伦创办于 1975 年,公司总部设立在华盛顿州的雷德蒙德(Redmond,邻近西雅图)。以研发、制造、授权和提供广泛的电脑软件服务业务为主。

    8 引用 • 44 回帖
  • Outlook
    1 引用 • 5 回帖 • 1 关注
  • CodeMirror
    2 引用 • 17 回帖 • 162 关注