当前位置:网站首页>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 .
边栏推荐
- Determining the subject area of data warehouse construction
- Unity脚本的基础语法(5)-向量
- Tronapi- wavefield interface - source code without encryption - can be opened in two places - interface document attached - encapsulation based on thinkphp5 - detailed guidance of the author - 21:59:3
- MySQL judges the calculation result and divides it by 100
- 基于ThinkPHP5封装-tronapi-波场接口-源码无加密-可二开--附接口文档-作者详细指导-2022年6月30日08:45:27
- MySQL queries the data within the radius according to the longitude and latitude, and draws a circle to query the database
- JS 二维数组变一维数组的方法
- Tronapi-波场接口-源码无加密-可二开--附接口文档-基于ThinkPHP5封装-作者详细指导-2022年6月29日21:59:34
- 【精选】资源变现资讯、新闻、自媒体、博客小程序(可引流,开通流量主,带pc后台管理)
- Open source of xinzhibao applet
猜你喜欢

【300+精选大厂面试题持续分享】大数据运维尖刀面试题专栏(二)

jmeter 学习笔记

Analysis of smart jiangcai login in Jiangxi University of Finance and Economics

Wechat applet reports an error: typeerror: cannot read property 'SetData' of undefined
![[qnx hypervisor 2.2 user manual]6.2.3 communication between guest and external](/img/ca/9065325ce8882d95fb24c82fb62abc.png)
[qnx hypervisor 2.2 user manual]6.2.3 communication between guest and external

Motor control Clarke( α/β) Derivation of equal amplitude transformation

【MySQL】MySQL的安装与配置

Shell编程概述

The independent station is Web3.0. The national "14th five year plan" requires enterprises to build digital websites!

Hangzhou E-Commerce Research Institute: the official website (website) is the only form of private domain
随机推荐
Qt中的事件处理
The independent station is Web3.0. The national "14th five year plan" requires enterprises to build digital websites!
[learn awk in one day] operator
你想要的异常知识点都在这里了
【惊了】迅雷下载速度竟然比不上虚拟机中的下载速度
STM32 移植 RT-Thread 标准版的 FinSH 组件
Basics of golang -- the difference between slicing and array
ERROR: Cannot uninstall ‘PyYAML‘. It is a distutils installed project and thus we cannot accurately
ERROR: Cannot uninstall ‘PyYAML‘. It is a distutils installed project and thus we cannot accurately
Mysql根据经纬度查询半径多少以内的数据,画个圈圈查数据库
Four ways for flinksql to customize udtf
App wechat payment unicloud version of uniapp payment (with source code)
[yitianxue awk] regular matching
Open source of xinzhibao applet
Spatiotemporal prediction 2-gcn_ LSTM
MySQL queries the data within the radius according to the longitude and latitude, and draws a circle to query the database
全面解析免费及收费SSH工具的基本特性和总结
Solve numpy core._ exceptions. Ufunctypeerror: UFUNC 'Add' did not contain a loop with signature matching
RK356x U-Boot研究所(命令篇)3.2 help命令的用法
Database usage in QT