pg 10 分区表举例

简介:

-- partition table

CREATE TABLE users(

     user_id    serial not null,

     user_name  varchar(20),

     logdate   timestamp(0) with time zone not null 

) PARTITION BY RANGE (logdate);


-- table partied by month, create pk

CREATE TABLE users_y2017m01 PARTITION OF users(user_id primary key, user_name, logdate) FOR VALUES FROM ('2017-01-01') TO ('2017-02-01') WITH (parallel_workers = 4);

CREATE TABLE users_y2017m02 PARTITION OF users(user_id primary key, user_name, logdate) FOR VALUES FROM ('2017-02-01') TO ('2017-03-01') WITH (parallel_workers = 4);

CREATE TABLE users_y2017m03 PARTITION OF users(user_id primary key, user_name, logdate) FOR VALUES FROM ('2017-03-01') TO ('2017-04-01') WITH (parallel_workers = 4);

CREATE TABLE users_y2017m04 PARTITION OF users(user_id primary key, user_name, logdate) FOR VALUES FROM ('2017-04-01') TO ('2017-05-01') WITH (parallel_workers = 4);

CREATE TABLE users_y2017m05 PARTITION OF users(user_id primary key, user_name, logdate) FOR VALUES FROM ('2017-05-01') TO ('2017-06-01') WITH (parallel_workers = 4);

CREATE TABLE users_y2017m06 PARTITION OF users(user_id primary key, user_name, logdate) FOR VALUES FROM ('2017-06-01') TO ('2017-07-01') WITH (parallel_workers = 4);

CREATE TABLE users_y2017m07 PARTITION OF users(user_id primary key, user_name, logdate) FOR VALUES FROM ('2017-07-01') TO ('2017-08-01') WITH (parallel_workers = 4);

CREATE TABLE users_y2017m08 PARTITION OF users(user_id primary key, user_name, logdate) FOR VALUES FROM ('2017-08-01') TO ('2017-09-01') WITH (parallel_workers = 4);

CREATE TABLE users_y2017m09 PARTITION OF users(user_id primary key, user_name, logdate) FOR VALUES FROM ('2017-09-01') TO ('2017-10-01') WITH (parallel_workers = 4);

CREATE TABLE users_y2017m10 PARTITION OF users(user_id primary key, user_name, logdate) FOR VALUES FROM ('2017-10-01') TO ('2017-11-01') WITH (parallel_workers = 4);

CREATE TABLE users_y2017m11 PARTITION OF users(user_id primary key, user_name, logdate) FOR VALUES FROM ('2017-11-01') TO ('2017-12-01') WITH (parallel_workers = 4);

CREATE TABLE users_y2017m12 PARTITION OF users(user_id primary key, user_name, logdate) FOR VALUES FROM ('2017-12-01') TO ('2018-01-01') WITH (parallel_workers = 4);


-- index on partied table

CREATE INDEX ON users_y2017m01 (logdate);

CREATE INDEX ON users_y2017m02 (logdate);

CREATE INDEX ON users_y2017m03 (logdate);

CREATE INDEX ON users_y2017m04 (logdate);

CREATE INDEX ON users_y2017m05 (logdate);

CREATE INDEX ON users_y2017m06 (logdate);

CREATE INDEX ON users_y2017m07 (logdate);

CREATE INDEX ON users_y2017m08 (logdate);

CREATE INDEX ON users_y2017m09 (logdate);

CREATE INDEX ON users_y2017m10 (logdate);

CREATE INDEX ON users_y2017m11 (logdate);

CREATE INDEX ON users_y2017m12 (logdate);



本文转自 pgmia 51CTO博客,原文链接:http://blog.51cto.com/heyiyi/1942403

相关文章
|
6月前
|
关系型数据库
Pg库增加&修改字段
Pg库增加&修改字段
116 0
|
SQL 前端开发 Oracle
mysql合并查询(多张表) union 和 union all
简介 小序 :今天写首页动态业务的时候,用到了两张表,还需要分页查询,刚开始以为需要关联查询,后来发现关联的话不会放到一个实体,然后我就上网找方法,然后发现了一个我没学过的sql语句union,union all,卧槽 还是得好好学习啊,前端我想学,mysql我想学,真的时间不够用啊,还得给学弟学妹拍趣味编程课看的视频,真的是烦啊! 如果我们需要将两个select语句的结果作为一个整体显示出来,我们就需要用到union或者union all关键字。union(或称为联合)的作用是将多个结果合并在一起显示出来。 UNION 操作符用于合并两个或多个 SELECT 语句的结果集。
984 0
mysql合并查询(多张表) union 和 union all
|
关系型数据库 PostgreSQL
|
5月前
|
存储 监控 关系型数据库
MySQL普通表转换为分区表实战指南
MySQL普通表转换为分区表实战指南
|
SQL 监控 关系型数据库
PostgreSQL普通表转换成分区表
如何使用pg_rewrite扩展将普遍表转换成分区表
|
传感器 关系型数据库 MySQL
php语句:MySQL指定分区表跨分区根据时间条件快速查询记录的封装函数
php语句:MySQL指定分区表跨分区根据时间条件快速查询记录的封装函数
133 0
|
关系型数据库 PostgreSQL
使用`pg_size_pretty()`函数来统计分区表的大小
使用`pg_size_pretty()`函数来统计分区表的大小
701 1
|
存储 SQL 缓存
【MySQL高级】查询缓存、合并表、分区表
【MySQL高级】查询缓存、合并表、分区表
218 0
【MySQL高级】查询缓存、合并表、分区表
|
存储 机器学习/深度学习 关系型数据库
一文带你理解mysql中的分区表和合并表(一个常见知识点)
分区表是mysql5.1之后的新特性,合并表已经存在很长时间了。这篇文章主要介绍这两个概念以及他们基本的操作。
435 0