postgresql表分区的简单介绍
PostgreSQL走分区查询问题
PostgreSQL安装:一、windows下安装过程 安装介质:postgresql-9.1.3-1-windows.exe(46M),安装过程非常简单,过程如下: 1、开始安装: 2、选择程序安装目录: 注:安装 PostgreSQL 的分区最好是 NTFS 格式的。PostgreSQL 首要任务是要保证数
成都创新互联公司是一家专注于网站设计制作、网站设计与策划设计,绩溪网站建设哪家好?成都创新互联公司做网站,专注于网站建设十载,网设计领域的专业建站公司;建站业务涵盖:绩溪等地区。绩溪做网站价格咨询:028-86922220
PostgreSQL Partition
create_range_partitions
select create_range_partitions('trip_agg_data'::regclass,'time_key',0::bigint,1::bigint,0,false) ;
trip_agg_data --主表OID
time_key --分区列名,必须有not null约束
0::bigint: --起始值 bigint取决于time_key 类型
1::bigint --间隔值 必须大于0
0 --分区个数
false --不立即迁移数据
add_range_partition
select add_range_partition('trip_agg_data'::REGCLASS,0,2019010100,'trip_agg_data_lt_20190101');
2019010100 不能超过time_key类型的最大值
partition_table_concurrently
select partition_table_concurrently ('trip_agg_data_tim'::regclass,10000, 1.0);
迁移数据到子表
10000 --一个事务批量迁移多少记录
1.0 --获得行锁失败时,休眠多久再次获取,重试60次退出任务
pathman_concurrent_part_tasks
select * from pathman_concurrent_part_tasks;
查看数据迁移进度
disable_pathman_for
select disable_pathman_for('trip_agg_data_tim'::regclass);
禁用分区
drop_range_partition
SELECT drop_range_partition(partition, false) /* move data to parent */
FROM pathman_partition_list WHERE parent = 'trip_agg_data'::regclass
and partition = 'trip_agg_data_greater_20200101'::regclass
删除某个分区表
pathman_partition_list
select * from pathman_partition_list;
查看所有分区表信息
set_enable_parent
select set_enable_parent('trip_agg_data'::regclass,false);
禁用主表
split_range_partition
分裂范围分区
merge_range_partitions
合并范围分区
append_range_partition
向后添加范围分区
prepend_range_partition
向前添加范围分区
drop_range_partition
drop_range_partition(partition TEXT, -- 分区名称
delete_data BOOLEAN DEFAULT TRUE) -- 是否删除分区数据,如果false,表示分区数据迁移到主表
drop_partitions
drop_partitions(parent REGCLASS,
delete_data BOOLEAN DEFAULT FALSE)
attach_range_partition
attach_range_partition(relation REGCLASS, -- 主表OID
partition REGCLASS, -- 分区表OID
start_value ANYELEMENT, -- 起始值
end_value ANYELEMENT) -- 结束值
detach_range_partition
detach_range_partition(partition REGCLASS) -- 指定分区名,转换为普通表
-- 1, create table trip_agg_data_2019
create table trip_agg_data_2019 as select * from trip_agg_data where 1=0;
alter table trip_agg_data_2019 alter column time_key set not null;
-- 2, create_range_partition
select create_range_partitions('trip_agg_data_2019'::regclass,'time_key',0::bigint,1::bigint,0,false) ;
-- 3, detach_range_partition for trip_agg_data'
select detach_range_partition('trip_agg_data_20190101');
select detach_range_partition('trip_agg_data_20190201');
select detach_range_partition('trip_agg_data_20190301');
select detach_range_partition('trip_agg_data_20190401');
select detach_range_partition('trip_agg_data_20190501');
select detach_range_partition('trip_agg_data_20190601');
select detach_range_partition('trip_agg_data_20190701');
select detach_range_partition('trip_agg_data_20190801');
select detach_range_partition('trip_agg_data_20190901');
select detach_range_partition('trip_agg_data_20191001');
select detach_range_partition('trip_agg_data_20191101');
select detach_range_partition('trip_agg_data_20191201');
-- 4, add_range_partition for trip_agg_data'
select add_range_partition('trip_agg_data'::REGCLASS,2019010100,2020010100,'trip_agg_data_20190101_20200101');
-- 5, attach_range_partition for trip_agg_data_2019
select attach_range_partition('trip_agg_data_2019'::REGCLASS,'trip_agg_data_20190101',2019010100,2019020100);
select attach_range_partition('trip_agg_data_2019'::REGCLASS,'trip_agg_data_20190201',2019020100,2019030100);
select attach_range_partition('trip_agg_data_2019'::REGCLASS,'trip_agg_data_20190301',2019030100,2019040100);
select attach_range_partition('trip_agg_data_2019'::REGCLASS,'trip_agg_data_20190401',2019040100,2019050100);
select attach_range_partition('trip_agg_data_2019'::REGCLASS,'trip_agg_data_20190501',2019050100,2019060100);
select attach_range_partition('trip_agg_data_2019'::REGCLASS,'trip_agg_data_20190601',2019060100,2019070100);
select attach_range_partition('trip_agg_data_2019'::REGCLASS,'trip_agg_data_20190701',2019070100,2019080100);
select attach_range_partition('trip_agg_data_2019'::REGCLASS,'trip_agg_data_20190801',2019080100,2019090100);
select attach_range_partition('trip_agg_data_2019'::REGCLASS,'trip_agg_data_20190901',2019090100,2019100100);
select attach_range_partition('trip_agg_data_2019'::REGCLASS,'trip_agg_data_20191001',2019100100,2019110100);
select attach_range_partition('trip_agg_data_2019'::REGCLASS,'trip_agg_data_20191101',2019110100,2019120100);
select attach_range_partition('trip_agg_data_2019'::REGCLASS,'trip_agg_data_20191201',2019120100,2020010100);
-- 6, disable parent for trip_agg_data_2019
select set_enable_parent('trip_agg_data_2019'::regclass,false);
alter table trip_agg_data alter column time_key set not null;
select create_range_partitions('trip_agg_data'::regclass,'time_key',0::bigint,1::bigint,0,false) ;
select add_range_partition('trip_agg_data'::REGCLASS,0,2019010100,'trip_agg_data_lt_20190101');
select add_range_partition('trip_agg_data'::REGCLASS,2019010100,2019020100,'trip_agg_data_20190101');
select add_range_partition('trip_agg_data'::REGCLASS,2019020100,2019030100,'trip_agg_data_20190201');
select add_range_partition('trip_agg_data'::REGCLASS,2019030100,2019040100,'trip_agg_data_20190301');
select add_range_partition('trip_agg_data'::REGCLASS,2019040100,2019050100,'trip_agg_data_20190401');
select add_range_partition('trip_agg_data'::REGCLASS,2019050100,2019060100,'trip_agg_data_20190501');
select add_range_partition('trip_agg_data'::REGCLASS,2019060100,2019070100,'trip_agg_data_20190601');
select add_range_partition('trip_agg_data'::REGCLASS,2019070100,2019080100,'trip_agg_data_20190701');
select add_range_partition('trip_agg_data'::REGCLASS,2019080100,2019090100,'trip_agg_data_20190801');
select add_range_partition('trip_agg_data'::REGCLASS,2019090100,2019100100,'trip_agg_data_20190901');
select add_range_partition('trip_agg_data'::REGCLASS,2019100100,2019110100,'trip_agg_data_20191001');
select add_range_partition('trip_agg_data'::REGCLASS,2019110100,2019120100,'trip_agg_data_20191101');
select add_range_partition('trip_agg_data'::REGCLASS,2019120100,2020010100,'trip_agg_data_20191201');
select add_range_partition('trip_agg_data'::REGCLASS,2020010100,2020020100,'trip_agg_data_20200101');
select add_range_partition('trip_agg_data'::REGCLASS,2020020100,2020030100,'trip_agg_data_20200201');
select add_range_partition('trip_agg_data'::REGCLASS,2020030100,2020040100,'trip_agg_data_20200301');
select add_range_partition('trip_agg_data'::REGCLASS,2020040100,2020050100,'trip_agg_data_20200401');
select add_range_partition('trip_agg_data'::REGCLASS,2020050100,2020060100,'trip_agg_data_20200501');
select add_range_partition('trip_agg_data'::REGCLASS,2020060100,2020070100,'trip_agg_data_20200601');
select add_range_partition('trip_agg_data'::REGCLASS,2020070100,2020080100,'trip_agg_data_20200701');
select add_range_partition('trip_agg_data'::REGCLASS,2020080100,2020090100,'trip_agg_data_20200801');
select add_range_partition('trip_agg_data'::REGCLASS,2020090100,2020100100,'trip_agg_data_20200901');
select add_range_partition('trip_agg_data'::REGCLASS,2020100100,2020110100,'trip_agg_data_20201001');
select add_range_partition('trip_agg_data'::REGCLASS,2020110100,2020120100,'trip_agg_data_20201101');
select add_range_partition('trip_agg_data'::REGCLASS,2020120100,2021010100,'trip_agg_data_20201201');
select add_range_partition('trip_agg_data'::REGCLASS,2021010100,2021020100,'trip_agg_data_20210101');
select add_range_partition('trip_agg_data'::REGCLASS,2021020100,2021030100,'trip_agg_data_20210201');
select add_range_partition('trip_agg_data'::REGCLASS,2021030100,2021040100,'trip_agg_data_20210301');
select add_range_partition('trip_agg_data'::REGCLASS,2021040100,2021050100,'trip_agg_data_20210401');
select add_range_partition('trip_agg_data'::REGCLASS,2021050100,2021060100,'trip_agg_data_20210501');
select add_range_partition('trip_agg_data'::REGCLASS,2021060100,2021070100,'trip_agg_data_20210601');
select add_range_partition('trip_agg_data'::REGCLASS,2021070100,2021080100,'trip_agg_data_20210701');
select add_range_partition('trip_agg_data'::REGCLASS,2021080100,2021090100,'trip_agg_data_20210801');
select add_range_partition('trip_agg_data'::REGCLASS,2021090100,2021100100,'trip_agg_data_20210901');
select add_range_partition('trip_agg_data'::REGCLASS,2021100100,2021110100,'trip_agg_data_20211001');
select add_range_partition('trip_agg_data'::REGCLASS,2021110100,2021120100,'trip_agg_data_20211101');
select add_range_partition('trip_agg_data'::REGCLASS,2021120100,2022010100,'trip_agg_data_20211201');
select add_range_partition('trip_agg_data'::REGCLASS,2022010100,2022020100,'trip_agg_data_20220101');
select add_range_partition('trip_agg_data'::REGCLASS,2022020100,2022030100,'trip_agg_data_20220201');
select add_range_partition('trip_agg_data'::REGCLASS,2022030100,2022040100,'trip_agg_data_20220301');
select add_range_partition('trip_agg_data'::REGCLASS,2022040100,2022050100,'trip_agg_data_20220401');
select add_range_partition('trip_agg_data'::REGCLASS,2022050100,2022060100,'trip_agg_data_20220501');
select add_range_partition('trip_agg_data'::REGCLASS,2022060100,2022070100,'trip_agg_data_20220601');
select add_range_partition('trip_agg_data'::REGCLASS,2022070100,2022080100,'trip_agg_data_20220701');
select add_range_partition('trip_agg_data'::REGCLASS,2022080100,2022090100,'trip_agg_data_20220801');
select add_range_partition('trip_agg_data'::REGCLASS,2022090100,2022100100,'trip_agg_data_20220901');
select add_range_partition('trip_agg_data'::REGCLASS,2022100100,2022110100,'trip_agg_data_20221001');
select add_range_partition('trip_agg_data'::REGCLASS,2022110100,2022120100,'trip_agg_data_20221101');
select add_range_partition('trip_agg_data'::REGCLASS,2022120100,2023010100,'trip_agg_data_20221201');
select add_range_partition('trip_agg_data'::REGCLASS,2023010100,2023020100,'trip_agg_data_20230101');
select add_range_partition('trip_agg_data'::REGCLASS,2023020100,2023030100,'trip_agg_data_20230201');
select add_range_partition('trip_agg_data'::REGCLASS,2023030100,2023040100,'trip_agg_data_20230301');
select add_range_partition('trip_agg_data'::REGCLASS,2023040100,2023050100,'trip_agg_data_20230401');
select add_range_partition('trip_agg_data'::REGCLASS,2023050100,2023060100,'trip_agg_data_20230501');
select add_range_partition('trip_agg_data'::REGCLASS,2023060100,2023070100,'trip_agg_data_20230601');
select add_range_partition('trip_agg_data'::REGCLASS,2023070100,2023080100,'trip_agg_data_20230701');
select add_range_partition('trip_agg_data'::REGCLASS,2023080100,2023090100,'trip_agg_data_20230801');
select add_range_partition('trip_agg_data'::REGCLASS,2023090100,2023100100,'trip_agg_data_20230901');
select add_range_partition('trip_agg_data'::REGCLASS,2023100100,2023110100,'trip_agg_data_20231001');
select add_range_partition('trip_agg_data'::REGCLASS,2023110100,2023120100,'trip_agg_data_20231101');
select add_range_partition('trip_agg_data'::REGCLASS,2023120100,2024010100,'trip_agg_data_20231201');
select add_range_partition('trip_agg_data'::REGCLASS,2024010100,2024020100,'trip_agg_data_20240101');
select add_range_partition('trip_agg_data'::REGCLASS,2024020100,2024030100,'trip_agg_data_20240201');
select add_range_partition('trip_agg_data'::REGCLASS,2024030100,2024040100,'trip_agg_data_20240301');
select add_range_partition('trip_agg_data'::REGCLASS,2024040100,2024050100,'trip_agg_data_20240401');
select add_range_partition('trip_agg_data'::REGCLASS,2024050100,2024060100,'trip_agg_data_20240501');
select add_range_partition('trip_agg_data'::REGCLASS,2024060100,2024070100,'trip_agg_data_20240601');
select add_range_partition('trip_agg_data'::REGCLASS,2024070100,2024080100,'trip_agg_data_20240701');
select add_range_partition('trip_agg_data'::REGCLASS,2024080100,2024090100,'trip_agg_data_20240801');
select add_range_partition('trip_agg_data'::REGCLASS,2024090100,2024100100,'trip_agg_data_20240901');
select add_range_partition('trip_agg_data'::REGCLASS,2024100100,2024110100,'trip_agg_data_20241001');
select add_range_partition('trip_agg_data'::REGCLASS,2024110100,2024120100,'trip_agg_data_20241101');
select add_range_partition('trip_agg_data'::REGCLASS,2024120100,2025010100,'trip_agg_data_20241201');
select add_range_partition('trip_agg_data'::REGCLASS,2025010100,2025020100,'trip_agg_data_20250101');
select add_range_partition('trip_agg_data'::REGCLASS,2025020100,2025030100,'trip_agg_data_20250201');
select add_range_partition('trip_agg_data'::REGCLASS,2025030100,2025040100,'trip_agg_data_20250301');
select add_range_partition('trip_agg_data'::REGCLASS,2025040100,2025050100,'trip_agg_data_20250401');
select add_range_partition('trip_agg_data'::REGCLASS,2025050100,2025060100,'trip_agg_data_20250501');
select add_range_partition('trip_agg_data'::REGCLASS,2025060100,2025070100,'trip_agg_data_20250601');
select add_range_partition('trip_agg_data'::REGCLASS,2025070100,2025080100,'trip_agg_data_20250701');
select add_range_partition('trip_agg_data'::REGCLASS,2025080100,2025090100,'trip_agg_data_20250801');
select add_range_partition('trip_agg_data'::REGCLASS,2025090100,2025100100,'trip_agg_data_20250901');
select add_range_partition('trip_agg_data'::REGCLASS,2025100100,2025110100,'trip_agg_data_20251001');
select add_range_partition('trip_agg_data'::REGCLASS,2025110100,2025120100,'trip_agg_data_20251101');
select add_range_partition('trip_agg_data'::REGCLASS,2025120100,2026010100,'trip_agg_data_20251201');
select add_range_partition('trip_agg_data'::REGCLASS,2026010100,2026020100,'trip_agg_data_20260101');
select add_range_partition('trip_agg_data'::REGCLASS,2026020100,2026030100,'trip_agg_data_20260201');
select add_range_partition('trip_agg_data'::REGCLASS,2026030100,2026040100,'trip_agg_data_20260301');
select add_range_partition('trip_agg_data'::REGCLASS,2026040100,2026050100,'trip_agg_data_20260401');
select add_range_partition('trip_agg_data'::REGCLASS,2026050100,2026060100,'trip_agg_data_20260501');
select add_range_partition('trip_agg_data'::REGCLASS,2026060100,2026070100,'trip_agg_data_20260601');
select add_range_partition('trip_agg_data'::REGCLASS,2026070100,2026080100,'trip_agg_data_20260701');
select add_range_partition('trip_agg_data'::REGCLASS,2026080100,2026090100,'trip_agg_data_20260801');
select add_range_partition('trip_agg_data'::REGCLASS,2026090100,2026100100,'trip_agg_data_20260901');
select add_range_partition('trip_agg_data'::REGCLASS,2026100100,2026110100,'trip_agg_data_20261001');
select add_range_partition('trip_agg_data'::REGCLASS,2026110100,2026120100,'trip_agg_data_20261101');
select add_range_partition('trip_agg_data'::REGCLASS,2026120100,2027010100,'trip_agg_data_20261201');
select add_range_partition('trip_agg_data'::REGCLASS,2027010100,2027020100,'trip_agg_data_20270101');
select add_range_partition('trip_agg_data'::REGCLASS,2027020100,2027030100,'trip_agg_data_20270201');
select add_range_partition('trip_agg_data'::REGCLASS,2027030100,2027040100,'trip_agg_data_20270301');
select add_range_partition('trip_agg_data'::REGCLASS,2027040100,2027050100,'trip_agg_data_20270401');
select add_range_partition('trip_agg_data'::REGCLASS,2027050100,2027060100,'trip_agg_data_20270501');
select add_range_partition('trip_agg_data'::REGCLASS,2027060100,2027070100,'trip_agg_data_20270601');
select add_range_partition('trip_agg_data'::REGCLASS,2027070100,2027080100,'trip_agg_data_20270701');
select add_range_partition('trip_agg_data'::REGCLASS,2027080100,2027090100,'trip_agg_data_20270801');
select add_range_partition('trip_agg_data'::REGCLASS,2027090100,2027100100,'trip_agg_data_20270901');
select add_range_partition('trip_agg_data'::REGCLASS,2027100100,2027110100,'trip_agg_data_20271001');
select add_range_partition('trip_agg_data'::REGCLASS,2027110100,2027120100,'trip_agg_data_20271101');
select add_range_partition('trip_agg_data'::REGCLASS,2027120100,2028010100,'trip_agg_data_20271201');
select add_range_partition('trip_agg_data'::REGCLASS,2028010100,2028020100,'trip_agg_data_20280101');
select add_range_partition('trip_agg_data'::REGCLASS,2028020100,2028030100,'trip_agg_data_20280201');
select add_range_partition('trip_agg_data'::REGCLASS,2028030100,2028040100,'trip_agg_data_20280301');
select add_range_partition('trip_agg_data'::REGCLASS,2028040100,2028050100,'trip_agg_data_20280401');
select add_range_partition('trip_agg_data'::REGCLASS,2028050100,2028060100,'trip_agg_data_20280501');
select add_range_partition('trip_agg_data'::REGCLASS,2028060100,2028070100,'trip_agg_data_20280601');
select add_range_partition('trip_agg_data'::REGCLASS,2028070100,2028080100,'trip_agg_data_20280701');
select add_range_partition('trip_agg_data'::REGCLASS,2028080100,2028090100,'trip_agg_data_20280801');
select add_range_partition('trip_agg_data'::REGCLASS,2028090100,2028100100,'trip_agg_data_20280901');
select add_range_partition('trip_agg_data'::REGCLASS,2028100100,2028110100,'trip_agg_data_20281001');
select add_range_partition('trip_agg_data'::REGCLASS,2028110100,2028120100,'trip_agg_data_20281101');
select add_range_partition('trip_agg_data'::REGCLASS,2028120100,2029010100,'trip_agg_data_20281201');
select add_range_partition('trip_agg_data'::REGCLASS,2029010100,2029020100,'trip_agg_data_20290101');
select add_range_partition('trip_agg_data'::REGCLASS,2029020100,2029030100,'trip_agg_data_20290201');
select add_range_partition('trip_agg_data'::REGCLASS,2029030100,2029040100,'trip_agg_data_20290301');
select add_range_partition('trip_agg_data'::REGCLASS,2029040100,2029050100,'trip_agg_data_20290401');
select add_range_partition('trip_agg_data'::REGCLASS,2029050100,2029060100,'trip_agg_data_20290501');
select add_range_partition('trip_agg_data'::REGCLASS,2029060100,2029070100,'trip_agg_data_20290601');
select add_range_partition('trip_agg_data'::REGCLASS,2029070100,2029080100,'trip_agg_data_20290701');
select add_range_partition('trip_agg_data'::REGCLASS,2029080100,2029090100,'trip_agg_data_20290801');
select add_range_partition('trip_agg_data'::REGCLASS,2029090100,2029100100,'trip_agg_data_20290901');
select add_range_partition('trip_agg_data'::REGCLASS,2029100100,2029110100,'trip_agg_data_20291001');
select add_range_partition('trip_agg_data'::REGCLASS,2029110100,2029120100,'trip_agg_data_20291101');
select add_range_partition('trip_agg_data'::REGCLASS,2029120100,2030010100,'trip_agg_data_20291201');
select add_range_partition('trip_agg_data'::REGCLASS,2030010100,2147483647,'trip_agg_data_gt_20300101');
select partition_table_concurrently ('trip_agg_data'::regclass,10000, 1.0);
select set_enable_parent('trip_agg_data'::regclass,false);
postgresql 怎么自动创建分区
在数据库日渐庞大的今天,为了方便对数据库数据的管理,比如按时间,按地区去统计一些数据时,基数过于庞大,多有不便。很多商业数据库都提供分区的概念,按不同的维度去存放数据,便于后期的管理,PostgreSQL也不例外。
PostgresSQL分区的意思是把逻辑上的一个大表分割成物理上的几块儿。分区不仅能带来访问速度的提升,关键的是,它能带来管理和维护上的方便。
分区的具体好处是:
某些类型的查询性能可以得到极大提升。
更新的性能也可以得到提升,因为表的每块的索引要比在整个数据集上的索引要小。如果索引不能全部放在内存里,那么在索引上的读和写都会产生更多的磁盘访问。
批量删除可以用简单的删除某个分区来实现。
可以将很少用的数据移动到便宜的、转速慢的存储介质上。
在PG里表分区是通过表继承来实现的,一般都是建立一个主表,里面是空,然后每个分区都去继承它。无论何时,都应保证主表里面是空的。
小表分区不实际,表在多大情况下才考虑分区呢?PostgresSQL官方给出的建议是:当表本身大小超过了机器物理内存的实际大小时(the size of the table should exceed the physical memory of the database server),可以考虑分区。
PG目前(9.2.2)仅支持范围分区和列表分区,尚未支持散列分区。
二、环境
系统环境:CentOS release 6.3 (Final)
PostgreSQL版本:PostgreSQL 9.2.2 on x86_64-unknown-linux-gnu, compiled by gcc (GCC) 4.4.6 20120305 (Red Hat 4.4.6-4), 64-bit
三、实现分区
3.1 创建主表
david=# create table tbl_partition (
david(# id integer,
david(# name varchar(20),
david(# gender boolean,
david(# join_date date,
david(# dept char(4));
CREATE TABLE
david=#
3.2 创建分区表
david=# create table tbl_partition_201211 (
check ( join_date = DATE '2012-11-01' AND join_date DATE '2012-12-01' )
) INHERITS (tbl_partition);
CREATE TABLE
david=# create table tbl_partition_201212 (
check ( join_date = DATE '2012-12-01' AND join_date DATE '2013-01-01' )
) INHERITS (tbl_partition);
CREATE TABLE
david=# create table tbl_partition_201301 (
check ( join_date = DATE '2013-01-01' AND join_date DATE '2013-02-01' )
) INHERITS (tbl_partition);
CREATE TABLE
david=# create table tbl_partition_201302 (
check ( join_date = DATE '2013-02-01' AND join_date DATE '2013-03-01' )
) INHERITS (tbl_partition);
CREATE TABLE
david=# create table tbl_partition_201303 (
check ( join_date = DATE '2013-03-01' AND join_date DATE '2013-04-01' )
) INHERITS (tbl_partition);
CREATE TABLE
david=# create table tbl_partition_201304 (
check ( join_date = DATE '2013-04-01' AND join_date DATE '2013-05-01' )
) INHERITS (tbl_partition);
CREATE TABLE
david=# create table tbl_partition_201305 (
check ( join_date = DATE '2013-05-01' AND join_date DATE '2013-06-01' )
) INHERITS (tbl_partition);
CREATE TABLE
david=#
3.3 分区键上建索引
david=# create index tbl_partition_201211_joindate on tbl_partition_201211 (join_date);
CREATE INDEX
david=# create index tbl_partition_201212_joindate on tbl_partition_201212 (join_date);
CREATE INDEX
david=# create index tbl_partition_201301_joindate on tbl_partition_201301 (join_date);
CREATE INDEX
david=# create index tbl_partition_201302_joindate on tbl_partition_201302 (join_date);
CREATE INDEX
david=# create index tbl_partition_201303_joindate on tbl_partition_201303 (join_date);
CREATE INDEX
david=# create index tbl_partition_201304_joindate on tbl_partition_201304 (join_date);
CREATE INDEX
david=# create index tbl_partition_201305_joindate on tbl_partition_201305 (join_date);
CREATE INDEX
david=#
对于开发人员来说,希望数据库是透明的,只管 insert into tbl_partition。对于数据插向哪个分区,则希望由DB决定。这点,ORACLE实现了,但是PG不行,需要前期人工处理下。
3.4 创建触发器函数
david=# CREATE OR REPLACE FUNCTION tbl_partition_insert_trigger()
RETURNS TRIGGER AS $$
BEGIN
IF ( NEW.join_date = DATE '2012-11-01' AND
NEW.join_date DATE '2012-12-01' ) THEN
INSERT INTO tbl_partition_201211 VALUES (NEW.*);
ELSIF ( NEW.join_date = DATE '2012-12-01' AND
NEW.join_date DATE '2013-01-01' ) THEN
INSERT INTO tbl_partition_201212 VALUES (NEW.*);
ELSIF ( NEW.join_date = DATE '2013-01-01' AND
NEW.join_date DATE '2013-02-01' ) THEN
INSERT INTO tbl_partition_201301 VALUES (NEW.*);
ELSIF ( NEW.join_date = DATE '2013-02-01' AND
NEW.join_date DATE '2013-03-01' ) THEN
INSERT INTO tbl_partition_201302 VALUES (NEW.*);
ELSIF ( NEW.join_date = DATE '2013-03-01' AND
NEW.join_date DATE '2013-04-01' ) THEN
INSERT INTO tbl_partition_201303 VALUES (NEW.*);
ELSIF ( NEW.join_date = DATE '2013-04-01' AND
NEW.join_date DATE '2013-05-01' ) THEN
INSERT INTO tbl_partition_201304 VALUES (NEW.*);
ELSIF ( NEW.join_date = DATE '2013-05-01' AND
NEW.join_date DATE '2013-06-01' ) THEN
INSERT INTO tbl_partition_201305 VALUES (NEW.*);
ELSE
RAISE EXCEPTION 'Date out of range. Fix the tbl_partition_insert_trigger() function!';
END IF;
RETURN NULL;
END;
$$
LANGUAGE plpgsql;
CREATE FUNCTION
david=#
说明:如果不想丢失数据,上面的ELSE 条件可以改成 INSERT INTO tbl_partition_error_join_date VALUES (NEW.*); 同时需要创建一张结构和tbl_partition 一样的表tbl_partition_error_join_date,这样,错误的join_date 数据就可以插入到这张表中而不是报错了。
附上出处链接:
PostGresql 分区表
插入数据的时候,会根据address_id算出它的hashcode值,然后落入到对应的分区表上。查询数据的时候,只要带入这个address_id作为查询条件,就会算出对应的hashcode的值,然后就可以直接去指定的分区表中查询检索数据,避免大表扫描,提升查询效率。
举个例子,有2000万的数据,做了hash分区,分出来20张表,在插入数据的时候把2000万的数据均匀分布到这20张表中,平均每张表的数据只有100万,那么查询某个数据的时候,就可以只查询其中的一个分区表,因为数据只有100万,远远比2000万小,所以速度更快
网站栏目:postgresql表分区的简单介绍
标题来源:http://myzitong.com/article/dscioho.html