--下面的描述不记得在哪里抄来的了?!

表分区就是把逻辑上一个大表分割成物理上的多个小块,表分区可提供如下若干好处:

1.某些类型的查询性能可以得到极大提升。

2.更新的性能可以得到提升,因为表的每块索引要比整个数据集上的索引要小,如果索引不能全部放在内存里,那么在索引上的读写都会产生磁盘访问。

3.批量删除可以用简单的删除某个分区

4.将很少使用的数据移动到便宜的慢一些的存储介质上。

示例1.

1.创建主表

create table tbl_inherits_test
(
a int,
b timestamp without time zone
);
create index idx_tbl_inherits_test_b on tbl_inherits_test using btree (b);

2.创建触发器函数,在INSERT父表时根据时间字段b写入时间b的分表,如果分表b不存在,则创建分表b,然后再INSERT分表

create or replace function f_insert_tbl_inherits_test() returns trigger as
$body$
declare tablename varchar(32) default '';
begin
tablename='tbl_inherits_test_'||to_char(NEW.b,'YYYY_MM_DD'); execute 'insert into '||tablename||'(a,b) values('||NEW.a||','''||NEW.b||''')';
    return null;
    EXCEPTION
when undefined_table then
execute 'create table '||tablename||'() inherits (tbl_inherits_test)';
execute 'create index idx_'||tablename||'_b on '||tablename||' using btree(b)';
execute 'insert into '||tablename||'(a,b) values('||NEW.a||','''||NEW.b||''')';
return null;
end;
$body$
language plpgsql;

3.创建触发器,当INSERT主表时执行触发器函数

create trigger trg_insert_tbl_inherits_test before insert on tbl_inherits_test for each row execute procedure f_insert_tbl_inherits_test();

4.向主表写数据验证结果

test=#insert into tbl_inherits_test(a,b) values(1,'2016-06-20 17:40:21');
test=# \d+ tbl_inherits_test
Table "public.tbl_inherits_test"
Column | Type | Modifiers | Storage | Stats target | Description
--------+-----------------------------+-----------+---------+--------------+-------------
a | integer | | plain | |
b | timestamp without time zone | | plain | |
Indexes:
"idx_tbl_inherits_test_b" btree (b)
Triggers:
trg_insert_tbl_inherits_test BEFORE INSERT ON tbl_inherits_test FOR EACH ROW EXECUTE PROCEDURE f_insert_tbl_inherits_test()
Child tables: tbl_inherits_test_2016_06_20

5.结果显示INSERT主表时会根据INSERT的数据b(2016-06-20 17:40:21)自动创建一个分表tbl_inherits_test_2016_06_20,再写入几条数据,查看结果

test=# insert into tbl_inherits_test(a,b) values (2,'2016-06-20 08:08:08'),(3,'2016-06-21 19:00:00');
INSERT 0 0
test=# \d+ tbl_inherits_test
Table "public.tbl_inherits_test"
Column | Type | Modifiers | Storage | Stats target | Description
--------+-----------------------------+-----------+---------+--------------+-------------
a | integer | | plain | |
b | timestamp without time zone | | plain | |
Indexes:
"idx_tbl_inherits_test_b" btree (b)
Triggers:
trg_insert_tbl_inherits_test BEFORE INSERT ON tbl_inherits_test FOR EACH ROW EXECUTE PROCEDURE f_insert_tbl_inherits_test()
Child tables: tbl_inherits_test_2016_06_20,
tbl_inherits_test_2016_06_21

6.分别查询主表和分表的数据,直接查询主表会查询到所有分表的数据,但是使用only查询主表发现,主表中并没有数据(因为触发器函数中返回的是null)

test=# select * from tbl_inherits_test_2016_06_20 ;
a | b
---+---------------------
1 | 2016-06-20 17:40:21
2 | 2016-06-20 08:08:08
(2 rows) test=# select * from tbl_inherits_test_2016_06_21 ;
a | b
---+---------------------
3 | 2016-06-21 19:00:00
(1 row) test=#
test=# select * from tbl_inherits_test ;
a | b
---+---------------------
1 | 2016-06-20 17:40:21
2 | 2016-06-20 08:08:08
3 | 2016-06-21 19:00:00
(3 rows) test=# select * from only tbl_inherits_test ;
a | b
---+---
(0 rows)

postgresql----表分区的更多相关文章

  1. 示例讲解PostgreSQL表分区的三种方式

    我最新最全的文章都在南瓜慢说 www.pkslow.com,欢迎大家来喝茶! 1 简介 表分区是解决一些因单表过大引用的性能问题的方式,比如某张表过大就会造成查询变慢,可能分区是一种解决方案.一般建议 ...

  2. POSTGRESQL表分区

    最近发现POSTGRESQL的一张表(下面统称为test表)达到67G大小,不得不进行重新分区,下面记录一下步骤: 前言.查看数据表结构(表结构肯定是虚构的) CREATE TABLE test ( ...

  3. 用HAWQ轻松取代传统数据仓库(八) —— 大表分区

    一.HAWQ中的分区表        与大多数关系数据库一样,HAWQ也支持分区表.这里所说的分区表是指HAWQ的内部分区表,外部分区表在后面“外部数据”篇讨论.在数据仓库应用中,事 实表通常有非常多 ...

  4. SQL Server表分区

    什么是表分区 一般情况下,我们建立数据库表时,表数据都存放在一个文件里. 但是如果是分区表的话,表数据就会按照你指定的规则分放到不同的文件里,把一个大的数据文件拆分为多个小文件,还可以把这些小文件放在 ...

  5. sql表分区

    1.单表达多少条数据后需要分区呢?   a.个人认为要似情况而定,有些常操作的表,分区反而带来麻烦,可以采用物理分表以及其它方法处理:   b.对于一些日志.历史订单类的查询数据,500w左右即可享受 ...

  6. Oracle10g 表分区

    1.分区的原因 (1)Tables greater than 2GB should always be considered for partitioning. (2)Tables containin ...

  7. oracle11g interval(numtoyminterval())自动创建表分区

    Oracle11g通过间隔分区实现按月创建表分区 在项目数据库设计过程中由于单表的数据量非常庞大,需要对表进行分区处理.由于表中的数据是历史交易,故按月分区,提升查询和管理. 由于之前对于表分区了解不 ...

  8. oracle表分区以及普表转分区表(转)

    概述 Oracle的表分区功能通过改善可管理性.性能和可用性,从而为各式应用程序带来了极大的好处.通常,分区可以使某些查询以及维护操作的性能大大提高.此外,分区还可以极大简化常见的管理任务,分区是构建 ...

  9. Mysql 表分区

    是否支持分区:mysql> show variables like '%partition%';+-----------------------+-------+| Variable_name ...

  10. SQL Server表分区的NULL值问题

    SQL Server表分区的NULL值问题 SQL Server表分区只支持range分区这一种类型,但是本人觉得已经够用了 虽然MySQL支持四种分区类型:RANGE分区.LIST分区.HASH分区 ...

随机推荐

  1. Hibernate 注解中CascadeType用法汇总

    这两天,参加一个课程设计,同时这个项目又作为一个模块镶嵌到其他项目中,考虑如此,应与原先的架构相同,因牵扯到留言和相互@功能,故数据库之间OneToOne,OneToMany,ManyToMany之风 ...

  2. debian下系列下的apt-get 命令与deb包的手动安装的dpkg命令

    手动下载的deb包的相关操作: 操作deb 使用dpkg 命令工具, dpkg 是Debian package的简写. 下面列举常用的 操作: dpkg –I name.deb  查看 包的详细信息( ...

  3. e1087. try/catch语句

    The try/catch statement encloses some code and is used to handle errors and exceptions that might oc ...

  4. Unity3D深入浅出 -组件与节点之间的调用关系

    一.transform组件用途 1.维护场景树 2.对3D物体的平移,缩放,旋转 二.场景树定义 在Hierarchy视图中显示的: 一个game_scene场景,下面有Main Camera节点,D ...

  5. 双卡手机怎么指定SIM卡打电话

    双卡手机如何指定SIM卡打电话 package com.example.dualsimtest; import android.app.Activity; import android.content ...

  6. IFrame实现的无刷新(仿ajax效果)...

    前台代码: <iframe style="display:none;" name="gg"></iframe> <form act ...

  7. Golang - OSX配置VIM下的Golang开发环境 (MacOS为例)

    测试环境 MacOS 10.12.6 首先安装VIM brew install vim 我已经安装了 Vim 8.0版本 然后安装 Vundle ,这是一个vim包管理器 git clone http ...

  8. thinkphp模板中无法给自定义函数传多个参数

    1.模板的用法 {:function(param1,param2,param3...)} 2.实例 <td>{:getChannelInfo($adminId,$v['sale_id']) ...

  9. 做游戏长知识------基于行为树与状态机的游戏AI(一)

    孙广东 2014.6.30 AI. 我们的第一印象可能是机器人,如今主要说在游戏中的应用. 现代的计算机游戏中已经大量融入了AI元素,平时我们进行游戏时产生的交互都是由AI来完毕的.比方在RPG游戏中 ...

  10. localhost 和 127.0.0.1

    转自:http://ordinarysky.cn/?p=431localhost与127.0.0.1的区别是什么?相信有人会说是本地ip,曾有人说,用127.0.0.1比localhost好,可以减少 ...