设为首页 加入收藏

TOP

PostgreSQL的分区表建立(一)
2014-11-24 07:45:10 来源: 作者: 【 】 浏览:4
Tags:PostgreSQL 分区表 建立
PostgreSQL的分区表建立
数据库日渐庞大的时候,为了方便对数据库数据的管理,比如按时间,按地区去统计一些数据时,基数过于庞大,多有不便。很多商业数据库都提供分区的概念,按不同的维度去存放数据,便于后期的管理,PG也不例外。下面是分区表创建步骤:
www.2cto.com
1.建立主表
create table parent_table(
id int,
name character varying(20),
create_time timestamp without time zone);
2.建立子表,继承于主表
create table parent_table_2012_01(
check (create_time>=date '2012-01-01' and create_time
inherits(parent_table); www.2cto.com
create table parent_table_2012_02(
check (create_time>=date '2012-02-01' and create_time
inherits(parent_table);
create table parent_table_2012_03(
check (create_time>=date '2012-03-01' and create_time
inherits(parent_table);
create table parent_table_2012_04(
check (create_time>=date '2012-04-01' and create_time
inherits(parent_table);
create table parent_table_2012_05(
check (create_time>=date '2012-05-01' and create_time
inherits(parent_table);
create table parent_table_2012_06(
check (create_time>=date '2012-06-01' and create_time
inherits(parent_table);
create table parent_table_2012_07(
check (create_time>=date '2012-07-01' and create_time
inherits(parent_table);
create table parent_table_2012_08(
check (create_time>=date '2012-08-01' and create_time
inherits(parent_table);
create table parent_table_2012_09(
check (create_time>=date '2012-09-01' and create_time
inherits(parent_table);
create table parent_table_2012_10(
check (create_time>=date '2012-10-01' and create_time
inherits(parent_table); www.2cto.com
create table parent_table_2012_11(
check (create_time>=date '2012-11-01' and create_time
inherits(parent_table);
create table parent_table_2012_12(
check (create_time>=date '2012-12-01' and create_time
inherits(parent_table);
3.创建触发器函数
CREATE OR REPLACE FUNCTION test.tri_parent_tab_insert()
RETURNS TRIGGER AS $$
--author: kenyon
--created:2012-05-24
BEGIN
IF ( NEW.create_time >= DATE '2012-01-01' AND
NEW.create_time < DATE '2012-02-01' ) THEN
INSERT INTO test.parent_table_2012_01 VALUES (NEW.id,NEW.name,NEW.create_time);
ELSIF ( NEW.create_time >= DATE '2012-02-01' AND
NEW.create_time < DATE '2012-03-01' ) THEN
INSERT INTO test.parent_table_2012_02 VALUES (NEW.id,NEW.name,NEW.create_time); www.2cto.com
ELSIF ( NEW.create_time >= DATE '2012-03-01' AND
NEW.create_time < DATE '2012-04-01' ) THEN
INSERT INTO test.parent_table_2012_03 VALUES (NEW.id,NEW.name,NEW.create_time);
ELSIF ( NEW.create_time >= DATE '2012-04-01' AND
NEW.create_time < DATE '2012-05-01' ) THEN
INSERT INTO test.parent_table_2012_04 VALUES (NEW.id,NEW.name,NEW.create_time);
ELSIF ( NEW.create_time >= DATE '2012-05-01' AND
NEW.create_time < DATE '2012-06-01' ) THEN
INSERT INTO test.parent_table_2012_05 VALUES (NEW.id,NEW.name,NEW.create_time);
ELSIF ( NEW.create_time >= DATE '2012-06-01' AND
NEW.create_time < DATE '2012-07-01' ) THEN
INSERT INTO test.parent_table_2012_06 VALUES (NEW.id,NEW.name,NEW.create_time);
ELSIF ( NEW.create_time >= DATE '2012-07-01' AND
NEW.create_time < DATE '2012-08-01' ) THEN
INSERT INTO tes
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇PostgreSQL学习手册(客户端命令&l.. 下一篇Postgresql连接简析

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·请问c语言刚入门,该 (2025-12-26 10:21:04)
·python 编程怎么定义 (2025-12-26 10:21:01)
·09-指 针 (一)-c语言 (2025-12-26 10:20:58)
·About - Redis (2025-12-26 08:20:56)
·Redis: A Comprehens (2025-12-26 08:20:53)