一:概述
在客户数据积累到一定程度后,尤其是单表数据达到千万级别的时候,随着查询速度越来越慢以及数据管理等需求,就要考虑分表,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
begin if(new.stu