当前位置:网站首页>PG Basics - logical structure management (table inheritance, partition table)
PG Basics - logical structure management (table inheritance, partition table)
2022-06-30 13:04:00 【51CTO】
Table inheritance
What is table inheritance
Create the parent table person, Sub table studen
postgres
=#
create
table persons
(
postgres
(# name
text
,
postgres
(# age
int
,
postgres
(# sex
boolean
)
;
CREATE
TABLE
postgres
=#
create
table students
(
postgres
(# class_no
int
postgres
(#
)inherits
(persons
)
;
CREATE
TABLE
postgres
=#
insert
into students
values
(
' Zhang San '
,
15
,
true
,
1
)
;
INSERT
0
1
postgres
=#
select
*
from persons
;
name
| age
| sex
------+-----+-----
Zhang San
|
15
| t
(
1 row
)
postgres
=#
select
*
from students
;
name
| age
| sex
| class_no
------+-----+-----+----------
Zhang San
|
15
| t
|
1
(
1 row
)
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
You can see that the sub table inserts data , The parent table is also inserted into the corresponding row .
At the same time, the sub table updates the data , The parent table is also updated .
If to persons Table insert data , be studens Watch not visible
Table inheritance rules
(1) When querying the parent table, the data of the word table in the parent table will also be queried , Not the other way around , You just want to query the data of the parent table itself , You only need to add... Before the table name of the query ONLY keyword .
(2) The check constraints and non empty constraints of all parent tables are automatically inherited by all child tables , Other types of constraints ( only , Primary key , Foreign keys ) Will not be inherited
(3) A child table can inherit from multiple parent tables , In this case, it will have the sum of all the parent table fields , If the same field name is listed in multiple parent tables , Then it will merge , The merged field will have all the check constraints of its parent field .
(4) Use DML and DDL Operate parent table , It will also operate on sub tables at the same time , but REINDEX,VACUUM The command does not affect child tables .
Partition table
postgresql Internally, partition tables are implemented through table inheritance . Table partitioning is to divide a large logical table into physical blocks .
The benefits of partitioned tables
(1) Historical data is deleted faster , For example, partition by time , Delete the history partition directly , No partitions may result in VACUUM overload
(2) The performance of some types of queries can be greatly improved , Each partitioned table has its own index , The indexes of highly used partitioned tables may be fully cached in memory , In this way, the efficiency will be much higher .
When to partition
When the size of the table exceeds the physical memory size of the database server .
Steps for creating partition tables
(1) Create the parent table , All partitions inherit from it . There is no data in this table , Do not define any check constraints on it , Unless you want to constrain all partitions .
(2) Create several sub tables , Each is inherited from the main table , Usually these tables do not add any fields ,
(3) Add constraints to the partitioned table , Define the allowed key values for each partition
(4) For each partition , Create an index on key fields
(5) Define a rule or trigger , Insert and redirect the data from the main table to the appropriate partitioned table .
(6) Make sure constraint_exclusion Configuration parameters in postgresql.conf It's open , The purpose is , After opening , If the query where The filter condition of clause matches the constraint condition of partition , Then the query will intelligently query only this partition , No other partitions will be queried .
Steps are as follows
create
table sales_detail
(
product_id
int
not
null
,
price
numeric
(
12
,
2
)
,
amount
int
not
null
,
sale_date
date
not
null
,
buyer
varchar
(
40
)
,
buyer_countact
text
)
;
create
table sales_detail_y2022m01
(
check
(sale_date
>
DATE '2022-01-01'
AND sale_date
<
DATE '2022-02-01'
)
)INHERITS
(sales_detail
)
;
create
table sales_detail_y2022m02
(
check
(sale_date
>
DATE '2022-02-01'
AND sale_date
<
DATE '2022-03-01'
)
)INHERITS
(sales_detail
)
;
create
table sales_detail_y2022m03
(
check
(sale_date
>
DATE '2022-03-01'
AND sale_date
<
DATE '2022-04-01'
)
)INHERITS
(sales_detail
)
;
create index sale_detail_y2022m01_sales_date
on sales_detail_y2022m01
(sale_date
)
;
create index sale_detail_y2022m02_sales_date
on sales_detail_y2022m02
(sale_date
)
;
create index sale_detail_y2022m03_sales_date
on sales_detail_y2022m03
(sale_date
)
;
create
or replace function sale_detail_insert_trigger
(
)
returns trigger
as $$
begin
........
end
;
$$
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
Partitioned tables use triggers to relocate the inserted data to the corresponding partitions .
Partition optimization
constraint_exclusion Skip partitions that do not need to be scanned
Set to off, Each partition word table will be scanned
Declarative partition
Do not create the main table , The method of sub table is to use DDL Way to create
create
table sales_detail
(
product_id
int
not
null
,
price
numeric
(
12
,
2
)
,
amount
int
not
null
,
sale_date
date
not
null
,
buyer
varchar
(
40
)
,
buyer_countact
text
)PRATITION
BY RANGE
(sale_date
)
;
create
table sales_detail_y2022m01 PARTITION OF sales_detail
FOR
VALUES
FROM
(
'2022-01-01'
) TO
(
'2022-02-01'
)
create
table sales_detail_y2022m02 PARTITION OF sales_detail
FOR
VALUES
FROM
(
'2022-02-01'
) TO
(
'2022-03-01'
)
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
Be careful : The partition of the partitioned table is still a separate table , You can query directly .
边栏推荐
- Four ways for flinksql to customize udtf
- Common UI components
- Unity脚本的基础语法(4)-访问其他游戏对象
- Can the polardb MySQL fees for RDS MySQL data migration be transferred?
- FFMpeg AVBufferPool 的理解与掌握
- 黑马笔记---常用日期API
- Dark horse notes -- wrapper class, regular expression, arrays class
- Clearing TinyMCE rich text cache in elementui
- RK356x U-Boot研究所(命令篇)3.2 help命令的用法
- Mqtt ROS simulates publishing a custom message type
猜你喜欢

Motor control Clarke( α/β) Derivation of equal amplitude transformation
![[learn awk in one day] operator](/img/52/fd476d95202f3a956fd59437c2d960.png)
[learn awk in one day] operator

rxjs Observable 两大类操作符简介

kaniko官方文档 - Build Images In Kubernetes

QT read / write excel--qxlsx worksheet display / hide status setting 4

JMeter learning notes

数据湖(十一):Iceberg表数据组织与查询

How to use AI technology to optimize the independent station customer service system? Listen to the experts!

【OpenGL】OpenGL Examples

postman 自動生成 curl 代碼片段
随机推荐
rpm2rpm 打包步骤
[surprised] the download speed of Xunlei is not as fast as that of the virtual machine
Substrate 源码追新导读: Pallet Alliance 并入主线,
Package tronapi wave field interface based on thinkphp5 PHP version -- interface document attached -20220627
Spatiotemporal prediction 2-gcn_ LSTM
Substrate 源码追新导读: 修复BEEFY的gossip引擎内存泄漏问题, 智能合约删除队列优化
Solve numpy core._ exceptions. Ufunctypeerror: UFUNC 'Add' did not contain a loop with signature matching
JMeter's performance test process and performance test focus
Tronapi-波场接口-PHP版本--附接口文档-基于ThinkPHP5封装-源码无加密-可二开-作者详细指导-2022年6月28日11:49:56
【C】深入理解指针、回调函数(介绍模拟qsort)
全面解析免费及收费SSH工具的基本特性和总结
深度长文探讨Join运算的简化和提速
Open source of xinzhibao applet
The independent station is Web3.0. The national "14th five year plan" requires enterprises to build digital websites!
Docker installation of mysql8 and sqlyong connection error 2058 solution [jottings]
Qt中的数据库使用
资源变现小程序开通流量主教程
Goods and services - platform properties
Dark horse notes -- List series collections and generics
Discussion on JMeter operation principle