淺談PostgreSQL表分區(qū)的三種方式
一、簡介
表分區(qū)是解決一些因單表過大引用的性能問題的方式,比如某張表過大就會造成查詢變慢,可能分區(qū)是一種解決方案。一般建議當(dāng)單表大小超過內(nèi)存就可以考慮表分區(qū)了。PostgreSQL的表分區(qū)有三種方式:
- Range:范圍分區(qū);
- List:列表分區(qū);
- Hash:哈希分區(qū)。
本文通過示例講解如何進(jìn)行這三種方式的分區(qū)。
二、三種方式
為方便,我們通過Docker的方式啟動一個PostgreSQL。我們要選擇較高的版本,否則不支持Hash分區(qū),命令如下:
docker run -itd \
--name pkslow-postgres \
-e POSTGRES_DB=pkslow \
-e POSTGRES_USER=pkslow \
-e POSTGRES_PASSWORD=pkslow \
-p 5432:5432 \
postgres:13
2.1、Range范圍分區(qū)
先創(chuàng)建一張表帶有年齡,然后我們根據(jù)年齡分段來進(jìn)行分區(qū),創(chuàng)建表語句如下:
CREATE TABLE pkslow_person_r (
age int not null,
city varchar not null
) PARTITION BY RANGE (age);
這個語句已經(jīng)指定了按age字段來分區(qū)了,接著創(chuàng)建分區(qū)表:
create table pkslow_person_r1 partition of pkslow_person_r for values from (MINVALUE) to (10); create table pkslow_person_r2 partition of pkslow_person_r for values from (11) to (20); create table pkslow_person_r3 partition of pkslow_person_r for values from (21) to (30); create table pkslow_person_r4 partition of pkslow_person_r for values from (31) to (MAXVALUE);
這里創(chuàng)建了四張分區(qū)表,分別對應(yīng)年齡是0到10歲、11到20歲、21到30歲、30歲以上。
接著我們插入一些數(shù)據(jù):
insert into pkslow_person_r(age, city) VALUES (1, 'GZ'); insert into pkslow_person_r(age, city) VALUES (2, 'SZ'); insert into pkslow_person_r(age, city) VALUES (21, 'SZ'); insert into pkslow_person_r(age, city) VALUES (13, 'BJ'); insert into pkslow_person_r(age, city) VALUES (43, 'SH'); insert into pkslow_person_r(age, city) VALUES (28, 'HK');
可以看到這里的表名還是pkslow_person_r,而不是具體的分區(qū)表,說明對于客戶端是無感知的。
我們查詢也一樣的:

但實際上是有分區(qū)表存在的:

而且分區(qū)表與主表的字段是一致的。
查詢分區(qū)表,就只能查到那個特定分區(qū)的數(shù)據(jù)了:

2.2、List列表分區(qū)
類似的,列表分區(qū)是按特定的值來分區(qū),比較某個城市的數(shù)據(jù)放在一個分區(qū)里。這里不再給出每一步的講解,代碼如下:
-- 創(chuàng)建主表
create table pkslow_person_l (
age int not null,
city varchar not null
) partition by list (city);
-- 創(chuàng)建分區(qū)表
CREATE TABLE pkslow_person_l1 PARTITION OF pkslow_person_l FOR VALUES IN ('GZ');
CREATE TABLE pkslow_person_l2 PARTITION OF pkslow_person_l FOR VALUES IN ('BJ');
CREATE TABLE pkslow_person_l3 PARTITION OF pkslow_person_l DEFAULT;
-- 插入測試數(shù)據(jù)
insert into pkslow_person_l(age, city) VALUES (1, 'GZ');
insert into pkslow_person_l(age, city) VALUES (2, 'SZ');
insert into pkslow_person_l(age, city) VALUES (21, 'SZ');
insert into pkslow_person_l(age, city) VALUES (13, 'BJ');
insert into pkslow_person_l(age, city) VALUES (43, 'SH');
insert into pkslow_person_l(age, city) VALUES (28, 'HK');
insert into pkslow_person_l(age, city) VALUES (28, 'GZ');
當(dāng)我們查詢第一個分區(qū)的時候,只有廣州的數(shù)據(jù):

2.3、Hash哈希分區(qū)
哈希分區(qū)是指按字段取哈希值后再分區(qū)。具體的語句如下:
-- 創(chuàng)建主表
create table pkslow_person_h (
age int not null,
city varchar not null
) partition by hash (city);
-- 創(chuàng)建分區(qū)表
create table pkslow_person_h1 partition of pkslow_person_h for values with (modulus 4, remainder 0);
create table pkslow_person_h2 partition of pkslow_person_h for values with (modulus 4, remainder 1);
create table pkslow_person_h3 partition of pkslow_person_h for values with (modulus 4, remainder 2);
create table pkslow_person_h4 partition of pkslow_person_h for values with (modulus 4, remainder 3);
-- 插入測試數(shù)據(jù)
insert into pkslow_person_h(age, city) VALUES (1, 'GZ');
insert into pkslow_person_h(age, city) VALUES (2, 'SZ');
insert into pkslow_person_h(age, city) VALUES (21, 'SZ');
insert into pkslow_person_h(age, city) VALUES (13, 'BJ');
insert into pkslow_person_h(age, city) VALUES (43, 'SH');
insert into pkslow_person_h(age, city) VALUES (28, 'HK');
可以看到創(chuàng)建分區(qū)表的時候,我們用了取模的方式,所以如果要創(chuàng)建N個分區(qū)表,就要取N取模。
隨便查詢一張分區(qū)表如下:

可以看到同是SZ的哈希值是一樣的,肯定會分在同一個分區(qū),而BJ的哈希值取模后也屬于同一個分區(qū)。
三、總結(jié)
本文講解了PostgreSQL分區(qū)的三種方式。
代碼請查看:https://github.com/LarryDpk/pkslow-samples
以上就是淺談PostgreSQL表分區(qū)的三種方式的詳細(xì)內(nèi)容,更多關(guān)于PostgreSQL表分區(qū)的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
常用?PostgreSQL?預(yù)防數(shù)據(jù)丟失解決方案
這篇文章主要介紹了常用?PostgreSQL?預(yù)防數(shù)據(jù)丟失方案,本篇主要介紹關(guān)于?DDL?和?DML?操作,如何預(yù)防數(shù)據(jù)丟失的方案,需要的朋友可以參考下2022-01-01
postgreSQL數(shù)據(jù)庫默認(rèn)用戶postgres常用命令分享
這篇文章主要介紹了postgreSQL數(shù)據(jù)庫默認(rèn)用戶postgres常用命令分享,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01
postgresql的now()與Oracle的sysdate區(qū)別說明
這篇文章主要介紹了postgresql的now()與Oracle的sysdate區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12
Postgresql 存儲過程(plpgsql)兩層for循環(huán)的操作
這篇文章主要介紹了Postgresql 存儲過程(plpgsql)兩層for循環(huán)的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01
PostgreSQL常用字符串函數(shù)與示例說明小結(jié)
文章介紹了PostgreSQL中常用字符串函數(shù)的使用方法,包括空值處理、字符串位置查詢、長度計算、大小寫轉(zhuǎn)換、去除空格、連接、替換、匹配、拆分和截取等操作,感興趣的朋友跟隨小編一起看看吧2024-11-11
postgresql連續(xù)歸檔及時間點恢復(fù)的操作
這篇文章主要介紹了postgresql連續(xù)歸檔及時間點恢復(fù)的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01

