PPAS中的分区表可以按oracle兼容的语法创建,具体使用请参见《Postgres_Plus_Advanced_Server_Oracle_Compatibility_Guide_v91.pdf》。
下面是分区表上操作的相关情况
1
创建表:
create table test (id integer primary key, name varchar(32))
PARTITION BY RANGE (id)
(PARTITION t1_1000 VALUES LESS THAN(1001),
PARTITION t1001_2000 VALUES LESS THAN(2001),
PARTITION t2001_3000 VALUES LESS THAN(3001));
1.1
从数据库取的表定义
-- Table: test
-- DROP TABLE test;
CREATE TABLE test
(
id integer NOT NULL,
name character varying(32),
CONSTRAINT test_pkey PRIMARY KEY (id)
)
WITH (
OIDS=FALSE
);
ALTER TABLE test
OWNER TO enterprisedb;
-- Table: test_t1_1000
-- DROP TABLE test_t1_1000;
CREATE TABLE test_t1_1000
(
-- Inherited from table test: id integer NOT NULL,
-- Inherited from table test: name character varying(32),
CONSTRAINT test_t1_1000_pkey PRIMARY KEY (id),
CONSTRAINT test_t1_1000_partition CHECK (id < 1001)
)
INHERITS (test)
WITH (
OIDS=FALSE
);
ALTER TABLE test_t1_1000
OWNER TO enterprisedb;
-- Table: test_t1001_2000
-- DROP TABLE test_t1001_2000;
CREATE TABLE test_t1001_2000
(
-- Inherited from table test: id integer NOT NULL,
-- Inherited from table test: name character varying(32),
CONSTRAINT test_t1001_2000_pkey PRIMARY KEY (id),
CONSTRAINT test_t1001_2000_partition CHECK (id >= 1001 AND id < 2001)
)
INHERITS (test)
WITH (
OIDS=FALSE
);
ALTER TABLE test_t1001_2000
OWNER TO enterprisedb;
-- Table: test_t2001_3000
-- DROP TABLE test_t2001_3000;
CREATE TABLE test_t2001_3000
(
-- Inherited from table test: id integer NOT NULL,
-- Inherited from table test: name character varying(32),
CONSTRAINT test_t2001_3000_pkey PRIMARY KEY (id),
CONSTRAINT test_t2001_3000_partition CHECK (id >= 2001 AND id < 3001)
)
INHERITS (test)
WITH (
OIDS=FALSE
);
ALTER TABLE test_t2001_3000
OWNER TO enterprisedb;
2
给表中插入值时自动根据ID值插入到分区表中
edbtest=# INSERT INTO test(id, name)VALUES (6, 'ertr');
INSERT 0 0
edbtest=# select * from test;
id | name
----+------
6 | ertr
(1 row)
edbtest=#
edbtest=# select count(*) from only test;
count
-------
0
(1 row)
edbtest=# select count(*) from only test_t1_1000;
count
-------
1
(1 row)
^
edbtest=#
edbtest=# select count(*) from only test_t1001_2000;
count
-------
0
(1 row)
edbtest=#
3
从父表中删除该值
edbtest=# delete from test where id=6;
DELETE 1
edbtest=#
edbtest=# select count(*) from only test_t1_1000;
count
-------
0
(1 row)
4
批量插入值
edbtest=#insert into test select generate_series(1,2600),'abc';
INSERT 0 0
edbtest=#
edbtest=# select count(*) from test;
count
-------
2600
(1 row)
edbtest=# select count(*) from only test;
count
-------
0
(1 row)
edbtest=# select count(*) from only test_t2001_3000;
count
-------
600
(1 row)
edbtest=#
5
查询
5.1
按分区列值查询,只查询对应分区表
edbtest=# explain select * from test where id=200;
QUERY PLAN
-------------------------------------------------------------------------------------------------------
Result (cost=0.00..7.27 rows=2 width=47)
-> Append (cost=0.00..7.27 rows=2 width=47)
-> Seq Scan on test (cost=0.00..0.00 rows=1 width=86)
Filter: (id = 200)
-> Index Scan using test_t1_1000_pkey on test_t1_1000 test (cost=0.00..7.27 rows=1 width=8)
Index Cond: (id = 200)
(6 rows)
5.2
按分区列值做范围查询,只查询对应分区表
edbtest=# explain select * from test where id<200 and id>100;
QUERY PLAN
---------------------------------------------------------------------------------------------------------
Result (cost=0.00..9.25 rows=101 width=9)
-> Append (cost=0.00..9.25 rows=101 width=9)
-> Seq Scan on test (cost=0.00..0.00 rows=1 width=86)
Filter: ((id < 200) AND (id > 100))
-> Index Scan using test_t1_1000_pkey on test_t1_1000 test (cost=0.00..9.25 rows=100 width=8)
Index Cond: ((id < 200) AND (id > 100))
(6 rows)
edbtest=#
edbtest=# explain select * from test where id<700 and id>100;
QUERY PLAN
--------------------------------------------------------------------------------
Result (cost=0.00..20.00 rows=601 width=8)
-> Append (cost=0.00..20.00 rows=601 width=8)
-> Seq Scan on test (cost=0.00..0.00 rows=1 width=86)
Filter: ((id < 700) AND (id > 100))
-> Seq Scan on test_t1_1000 test (cost=0.00..20.00 rows=600 width=8)
Filter: ((id < 700) AND (id > 100))
(6 rows)
edbtest=#
edbtest=# explain select * from test where id<1100 and id>900;
QUERY PLAN
---------------------------------------------------------------------------------------------------------------
Result (cost=0.00..18.50 rows=201 width=8)
-> Append (cost=0.00..18.50 rows=201 width=8)
-> Seq Scan on test (cost=0.00..0.00 rows=1 width=86)
Filter: ((id < 1100) AND (id > 900))
-> Index Scan using test_t1_1000_pkey on test_t1_1000 test (cost=0.00..9.25 rows=100 width=8)
Index Cond: ((id < 1100) AND (id > 900))
-> Index Scan using test_t1001_2000_pkey on test_t1001_2000 test (cost=0.00..9.25 rows=100 width=8)
Index Cond: ((id < 1100) AND (id > 900))
(8 rows)
5.3
按分区列值和其它列查询,只查询对应分区表
edbtest=# explain select * from test where id=300 and name='ccc';
QUERY PLAN
-------------------------------------------------------------------------------------------------------
Result (cost=0.00..7.27 rows=2 width=47)
-> Append (cost=0.00..7.27 rows=2 width=47)
-> Seq Scan on test (cost=0.00..0.00 rows=1 width=86)
Filter: ((id = 300) AND ((name)::text = 'ccc'::text))
-> Index Scan using test_t1_1000_pkey on test_t1_1000 test (cost=0.00..7.27 rows=1 width=8)
Index Cond: (id = 300)
Filter: ((name)::text = 'ccc'::text)
(7 rows)
5.4
按分区列值查询,值有显式类型转换,只查询对应分区表
edbtest=# explain select * from test where id='5'::int;
QUERY PLAN
-------------------------------------------------------------------------------------------------------
Result (cost=0.00..7.27 rows=2 width=47)
-> Append (cost=0.00..7.27 rows=2 width=47)
-> Seq Scan on test (cost=0.00..0.00 rows=1 width=86)
Filter: (id = 5)
-> Index Scan using test_t1_1000_pkey on test_t1_1000 test (cost=0.00..7.27 rows=1 width=8)
Index Cond: (id = 5)
(6 rows)
5.5
按分区列值查询,值和列类型不同,值有隐式类型转换,只查询对应分区表
edbtest=# explain select * from test where id='5';
QUERY PLAN
-------------------------------------------------------------------------------------------------------
Result (cost=0.00..7.27 rows=2 width=47)
-> Append (cost=0.00..7.27 rows=2 width=47)
-> Seq Scan on test (cost=0.00..0.00 rows=1 width=86)
Filter: (id = 5)
-> Index Scan using test_t1_1000_pkey on test_t1_1000 test (cost=0.00..7.27 rows=1 width=8)
Index Cond: (id = 5)
(6 rows)
5.6
按分区列值查询,列要做隐式类型转换,走全表扫描
edbtest=# explain select * from test where id || name ='5abc';
QUERY PLAN
---------------------------------------------------------------------------------
Result (cost=0.00..65.00 rows=14 width=14)
-> Append (cost=0.00..65.00 rows=14 width=14)
-> Seq Scan on test (cost=0.00..0.00 rows=1 width=86)
Filter: (((id)::text || (name)::text) = '5abc'::text)
-> Seq Scan on test_t1_1000 test (cost=0.00..25.00 rows=5 width=8)
Filter: (((id)::text || (name)::text) = '5abc'::text)
-> Seq Scan on test_t1001_2000 test (cost=0.00..25.00 rows=5 width=8)
Filter: (((id)::text || (name)::text) = '5abc'::text)
-> Seq Scan on test_t2001_3000 test (cost=0.00..15.00 rows=3 width=8)
Filter: (((id)::text || (name)::text) = '5abc'::text)
(10 rows)
edbtest=#
5.7
按分区列值查询,值使用了函数,走全表扫描
edbtest=# explain select * from test where id=to_number('1');
QUERY PLAN
---------------------------------------------------------------------------------
Result (cost=0.00..52.00 rows=14 width=14)
-> Append (cost=0.00..52.00 rows=14 width=14)
-> Seq Scan on test (cost=0.00..0.00 rows=1 width=86)
Filter: ((id)::numeric = 1::numeric)
-> Seq Scan on test_t1_1000 test (cost=0.00..20.00 rows=5 width=8)
Filter: ((id)::numeric = 1::numeric)
-> Seq Scan on test_t1001_2000 test (cost=0.00..20.00 rows=5 width=8)
Filter: ((id)::numeric = 1::numeric)
-> Seq Scan on test_t2001_3000 test (cost=0.00..12.00 rows=3 width=8)
Filter: ((id)::numeric = 1::numeric)
(10 rows)
5.8
按分区列值查询,值使用了函数,走分区表索引扫描
edbtest=# explain select * from test where id=cast('1' as int);
QUERY PLAN
-------------------------------------------------------------------------------------------------------
Result (cost=0.00..7.27 rows=2 width=47)
-> Append (cost=0.00..7.27 rows=2 width=47)
-> Seq Scan on test (cost=0.00..0.00 rows=1 width=86)
Filter: (id = 1)
-> Index Scan using test_t1_1000_pkey on test_t1_1000 test (cost=0.00..7.27 rows=1 width=8)
Index Cond: (id = 1)
(6 rows)
edbtest=#
5.9
按分区列值查询,值使用了子查询,用等号走全表扫描,用in走分区表索引扫描
edbtest=# explain select * from test where id in(select 1 from dual);
QUERY PLAN
-------------------------------------------------------------------------------------------------------
Nested Loop Semi Join (cost=0.00..8.31 rows=2 width=47)
-> Append (cost=0.00..7.27 rows=2 width=47)
-> Seq Scan on test (cost=0.00..0.00 rows=1 width=86)
Filter: (id = 1)
-> Index Scan using test_t1_1000_pkey on test_t1_1000 test (cost=0.00..7.27 rows=1 width=8)
Index Cond: (id = 1)
-> Materialize (cost=0.00..1.01 rows=1 width=0)
-> Seq Scan on dual (cost=0.00..1.01 rows=1 width=0)
(8 rows)
edbtest=# explain select * from test where id =(select 1 from dual);
QUERY PLAN
-------------------------------------------------------------------------------------------------------------
Result (cost=1.01..22.82 rows=4 width=28)
InitPlan 1 (returns $0)
-> Seq Scan on dual (cost=0.00..1.01 rows=1 width=0)
-> Append (cost=0.00..21.81 rows=4 width=28)
-> Seq Scan on test (cost=0.00..0.00 rows=1 width=86)
Filter: (id = $0)
-> Index Scan using test_t1_1000_pkey on test_t1_1000 test (cost=0.00..7.27 rows=1 width=8)
Index Cond: (id = $0)
-> Index Scan using test_t1001_2000_pkey on test_t1001_2000 test (cost=0.00..7.27 rows=1 width=8)
Index Cond: (id = $0)
-> Index Scan using test_t2001_3000_pkey on test_t2001_3000 test (cost=0.00..7.27 rows=1 width=8)
Index Cond: (id = $0)
(12 rows)
edbtest=#
5.10
按分区列值 更新,走分区表索引扫描
edbtest=# explain update test set name = 'bbb' where id=99;
QUERY PLAN
--------------------------------------------------------------------------------------------------
Update on test (cost=0.00..7.27 rows=2 width=10)
-> Seq Scan on test (cost=0.00..0.00 rows=1 width=10)
Filter: (id = 99)
-> Index Scan using test_t1_1000_pkey on test_t1_1000 test (cost=0.00..7.27 rows=1 width=10)
Index Cond: (id = 99)
(5 rows)
5.11
按分区列值 删除,走分区表索引扫描
edbtest=# explain delete from test where id=99;
QUERY PLAN
-------------------------------------------------------------------------------------------------
Delete on test (cost=0.00..7.27 rows=2 width=6)
-> Seq Scan on test (cost=0.00..0.00 rows=1 width=6)
Filter: (id = 99)
-> Index Scan using test_t1_1000_pkey on test_t1_1000 test (cost=0.00..7.27 rows=1 width=6)
Index Cond: (id = 99)
(5 rows)
edbtest=#
-----------------
转载请著明出处:
blog.csdn.net/beiigang
beigang.iteye.com
相关推荐
PostgreSQL 官方分区表功能的演进包括传统分区表、内置分区表、增强分区表和高性能分区表等。 PostgreSQL 9.x 及之前的版本使用传统分区表,通过继承和触发器实现分区表关系。从 PostgreSQL 10 版本开始,官方支持...
项目中有需求要垂直分表,即按照时间区间将数据拆分到n个表中,PostgreSQL提供了分区表的功能。分区表实际上是把逻辑上的一个大表分割成物理上的几小块,提供了很多好处,比如: 1、查询性能大幅提升 2、删除历史...
PostgreSQL表分区和子表及删除所有的数据库表 最近需求要求统计DNS近7天每天的解析情况。数据量相对大,所以我这边对表进行分区。 对每天的数据进行分区存储。主表只存储近7天的数据,7天之前的数据删掉。所以我...
在数据库管理中,分区表是一种优化查询性能的技术,它将一个大表分成多个较小、更易管理的部分,每个部分称为一个分区。PostgreSQL作为一个强大的开源关系型数据库管理系统,支持多种分区策略,如范围、列表、哈希等...
PostgreSQL 作为一款功能强大的开源关系型数据库系统,支持多种类型的表分区机制。本文将详细介绍 PostgreSQL 中的表分区技术,包括其工作原理、实现方法以及示例演示。 #### 二、表分区原理与实现 ##### 2.1 表...
除了手动分区,PostgreSQL还支持分区继承,例如内容中提到的parent表和它的两个子分区child1和child2。通过使用inherits关键字,子表child1和child2继承了parent表的结构,并且可以包含额外的数据列。如果向parent表...
在PostgreSQL中,表分区是一种优化数据库性能的技术,它允许将大表的数据分割成更小、更易管理的部分,每个部分称为一个分区。这有助于提高查询速度,减少维护成本,并优化存储空间。以下是对创建表分区的详细说明:...
从这里可以看出PostgreSQL表...通过作用于主表的触发器,把主表的修改重定向到适当的分区表等工作,分区的创建和管理都需要我们用语言来控制,增加了开发人员的工作量,PostgreSQL表分区的所有表结构和表分区及触发器
通过以上知识点的梳理,可以看出本文将深入探讨如何利用PostgreSQL的分区表技术来提升数据库性能、管理大数据量,并针对特定场景下如何有效实施分区策略,从而实现更好的数据维护和查询优化。对于从事数据库管理、...
表分区(Table Partitioning)是指在特定场景下,把逻辑上的一个大表分裂成多个更小的物理分片,以获得性能的提升。通过分区,可以减少查询的数据量,提高查询速度,减少索引的大小和维护成本。 什么是分区表? ...
PostgreSql数据库分区表.html
在postgresql10之后,引入了内置分区表,用户不需要先在父表上定义insert,update,delete触发器,对父表的DML操作会自动路由到相应分区,相比传统分区表大幅度降低了维护成本,目前内置分区表仅支持范围分区和列表...
在PostgreSQL中,表的继承是一个非常有用且强大的特性,它可以使得一个表(子表)继承另一个表(父表)的所有列和约束。这为数据库设计提供了一种灵活的方式来组织相似类型的数据,特别是当需要对一组共享相同属性的...
2. **易于管理**:分区表可以独立备份和恢复,简化了大数据量环境下的维护工作。 3. **优化存储**:分区可以帮助优化空间使用,避免不必要的数据冗余。 在实际应用中,开发者还可以结合Django的ORM(对象关系映射)...
PostgreSQL则更为灵活,支持B-树、哈希、R-树和Gist等多种索引类型,以及临时表、常规表和复杂分区表。 #### 十、约束与扩展功能 MySQL支持主键、外键、唯一和非空约束,对检查约束仅做解析但不强制执行。...
优化SQL语句是提升数据库性能的直接方式,例如通过减少不必要的数据表连接、合理使用索引、优化查询条件等方式来提高查询效率。 文档还提到了如何让数据库输出好的执行计划。这需要通过执行计划分析工具,比如...
**cpp-pgpathman:PostgreSQL的高性能表分区插件** `cpp-pgpathman` 是针对 PostgreSQL 数据库系统设计的一个高效表分区插件。它提供了对数据表进行分区的能力,以优化大型数据库的性能,尤其适用于处理大量数据的...