pgsql使用积累系列_表分区(基于partition)

写在前面:这不是一个知识系列博客,这只是对本人使用过知识的一个记录,最好的资源应该是官方文档。

官网doc: https://www.postgresql.org/docs/current/static/plpgsql.html

-- 测试结果为不如用pg继承实现的分区

-- 表分区 关键字(PARTITION BY) 分区方法(RANGE/list) 分区key(logdate)
-- Ensure that the constraint_exclusion configuration parameter is not disabled in postgresql.conf
CREATE TABLE measurement (
    city_id         int not null,
    logdate         date not null,
    peaktemp        int,
    unitsales       int
) PARTITION BY RANGE (logdate);
-- 指定表空间
CREATE TABLE measurement_y2018m02 PARTITION OF measurement
    FOR VALUES FROM ('2018-02-01') TO ('2018-03-01')
TABLESPACE pg_default;

CREATE TABLE measurement_y2018m03 PARTITION OF measurement
    FOR VALUES FROM ('2018-02-01') TO ('2018-03-01') 
PARTITION BY RANGE (peaktemp);
-- 取消分区表与子表关系
ALTER TABLE measurement DETACH PARTITION measurement_y2018m02;
-- 添加分区表 方式一灵活,方式二简单
-- 方式一:
CREATE TABLE measurement_y2018m02(
city_id         int not null,
  logdate         date not null,
  peaktemp        int,
  unitsales       int
)
ALTER TABLE measurement_y2018m02 ADD CONSTRAINT y2018m02  -- 非必须操作。加了会免去添加分区时扫描分区验证
   CHECK ( logdate >= DATE '2018-02-01' AND logdate < DATE '2018-03-01' );
ALTER TABLE measurement ATTACH PARTITION measurement_y2018m02
FOR VALUES FROM ('2018-02-01') TO ('2018-03-01' );
-- 方式二
CREATE TABLE measurement_y2018m02 PARTITION OF measurement
    FOR VALUES FROM ('2018-02-01') TO ('2018-03-01')
TABLESPACE pg_default;


-- 创建表空间
create tablespace fast_tb location 'D:/environment/pgsql-10.0-1/newdata/mytablespace';


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值