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

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

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. Droptiles - 炫酷的 Metro 风格的层叠式 Web 面板

    介绍 Droptiles是一套Metro风格的类似Win8的Web2.0控制面板.它采用图块(tiles)建立用户体验.图块(tiles)是一些可以从外部资源中获取数据的迷你应用.点击图块(tile) ...

  2. (转)引用---FFMPEG解码过程

    视频播放过程 首先简单介绍以下视频文件的相关知识.我们平时看到的视频文件有许多格式,比如 avi, mkv, rmvb, mov, mp4等等,这些被称为容器(Container), 不同的容器格式规 ...

  3. HDU 1556 Color the ball 树状数组 题解

    Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的"小飞鸽"牌电动 ...

  4. centos查看启动时间

    系统启动时间 who -b date -d "$(awk -F. '{print $1}' /proc/uptime) second ago" +"%Y-%m-%d %H ...

  5. MySQL 日期与时间的处理

    1.查询当前日期时间:函数有now(),localtime(),current_timestamp(),sysdate(). mysql> select now(),localtime(),cu ...

  6. xml & < 需要转义

    写了个request2XML的方法,每当数据中有'<'.'&'符号时,封装的XML就无法解析.发现了XML里的CDATA属性,问题迎刃而解!在XML文档中的所有文本都会被解析器解析. 只 ...

  7. SharePoint 2010用“localhost”方式访问网站,File not found问题处理方式

    场景:本地服务器上,用“localhost”方式访问网站:在某网站集(Site Collection)下的子网站(Sub Site)中,点击网站权限菜单(Site permissions)等关于调用L ...

  8. LazyValue<T>

    public void ExtendFuncT() { //():匿名无参方法.() =>方法名,指派匿名无参方法去执行另外一个方法. LazyValue<int> lazyOne ...

  9. HDOJ 4276 The Ghost Blows Light

    题意 1. 给定一棵树, 树上节点有 value, 节点之间 travel 有 cost. 给定起始节点和最大 cost, 求解最大 value 思路 1. 寻找最短路径 a. 题目描述中有两句话, ...

  10. ftp简单命令

    1.连接ftp ftp 192.168.10.15 进去后输入用户名 ,然后再输入密码,就这样登陆成功了,你会看到 ftp> 2.进入ftp后,你对目录需要切换操作.和linux一样的命令.cd ...