当前位置:网站首页>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 .
边栏推荐
- rxjs Observable 两大类操作符简介
- jmeter 学习笔记
- golang文件的写入、追加、读取、复制操作:bufio包的使用示例
- 商品服务-平台属性
- How to handle ZABBIX server startup failure
- 基于ThinkPHP5封装-tronapi-波场接口-源码无加密-可二开--附接口文档-作者详细指导-2022年6月30日08:45:27
- Idea 2021.3 golang error: rning: undefined behavior version of delve is too old for go version 1.18
- MySQL implements the division of two query results
- 一次 Keepalived 高可用的事故,让我重学了一遍它!
- LeetCode_栈_中等_227.基本计算器 II(不含括号)
猜你喜欢

Rk356x u-boot Institute (command section) 3.3 env related command usage

In line with the trend of media integration, Zhongke Wenge and Meishe jointly create digital intelligence media publicity

Dark horse notes -- wrapper class, regular expression, arrays class

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

Four ways for flinksql to customize udtf

How to handle ZABBIX server startup failure

RK356x U-Boot研究所(命令篇)3.2 help命令的用法

JS converts an array to a two-dimensional array based on the same value

Visual studio configures QT and implements project packaging through NSIS

After the market value evaporated by 65billion yuan, the "mask king" made steady medical treatment and focused on condoms
随机推荐
STM32 移植 RT-Thread 标准版的 FinSH 组件
一次 Keepalived 高可用的事故,让我重学了一遍它!
After the market value evaporated by 65billion yuan, the "mask king" made steady medical treatment and focused on condoms
[yitianxue awk] regular matching
[300+ continuous sharing of selected interview questions from large manufacturers] column on interview questions of big data operation and maintenance (II)
Tronapi-波场接口-源码无加密-可二开--附接口文档-基于ThinkPHP5封装-作者详细指导-2022年6月29日21:59:34
2022-06-23 帆软部分公式及sql生成(月份、季度取数)
PG基础篇--逻辑结构管理(表继承、分区表)
How to solve cross domain problems
电机控制Clarke(α/β)等幅值变换推导
60 个神级 VS Code 插件!!
Postman automatically generates curl code snippets
Golang foundation -- slicing several declaration methods
产品经理专业知识50篇(七)-如何建立一套完整的用户成长体系?
Event handling in QT
常用的ui组件
【C】深入理解指针、回调函数(介绍模拟qsort)
Exploring the source code of Boca Cross Chain Communication: Elements
杭州电子商务研究院:官网(网站)是私域的唯一形态
JS 二维数组变一维数组的方法