一、簡(jiǎn)介
表分區(qū)是解決一些因單表過(guò)大引用的性能問(wèn)題的方式,比如某張表過(guò)大就會(huì)造成查詢變慢,可能分區(qū)是一種解決方案。一般建議當(dāng)單表大小超過(guò)內(nèi)存就可以考慮表分區(qū)了。postgresql的表分區(qū)有三種方式:
- range:范圍分區(qū);
- list:列表分區(qū);
- hash:哈希分區(qū)。
本文通過(guò)示例講解如何進(jìn)行這三種方式的分區(qū)。
二、三種方式
為方便,我們通過(guò)docker的方式啟動(dòng)一個(gè)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ù)年齡分段來(lái)進(jìn)行分區(qū),創(chuàng)建表語(yǔ)句如下:
1
2
3
4
|
create table pkslow_person_r ( age int not null , city varchar not null ) partition by range (age); |
這個(gè)語(yǔ)句已經(jīng)指定了按age字段來(lái)分區(qū)了,接著創(chuàng)建分區(qū)表:
1
2
3
4
|
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ū)表,分別對(duì)應(yīng)年齡是0到10歲、11到20歲、21到30歲、30歲以上。
接著我們插入一些數(shù)據(jù):
1
2
3
4
5
6
|
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ū)表,說(shuō)明對(duì)于客戶端是無(wú)感知的。
我們查詢也一樣的:
但實(shí)際上是有分區(qū)表存在的:
而且分區(qū)表與主表的字段是一致的。
查詢分區(qū)表,就只能查到那個(gè)特定分區(qū)的數(shù)據(jù)了:
2.2、list列表分區(qū)
類似的,列表分區(qū)是按特定的值來(lái)分區(qū),比較某個(gè)城市的數(shù)據(jù)放在一個(gè)分區(qū)里。這里不再給出每一步的講解,代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
-- 創(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 ; -- 插入測(cè)試數(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)我們查詢第一個(gè)分區(qū)的時(shí)候,只有廣州的數(shù)據(jù):
2.3、hash哈希分區(qū)
哈希分區(qū)是指按字段取哈希值后再分區(qū)。具體的語(yǔ)句如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
-- 創(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); -- 插入測(cè)試數(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ū)表的時(shí)候,我們用了取模的方式,所以如果要?jiǎng)?chuàng)建n個(gè)分區(qū)表,就要取n取模。
隨便查詢一張分區(qū)表如下:
可以看到同是sz的哈希值是一樣的,肯定會(huì)分在同一個(gè)分區(qū),而bj的哈希值取模后也屬于同一個(gè)分區(qū)。
三、總結(jié)
本文講解了postgresql分區(qū)的三種方式。
代碼請(qǐng)查看:https://github.com/larrydpk/pkslow-samples
以上就是淺談postgresql表分區(qū)的三種方式的詳細(xì)內(nèi)容,更多關(guān)于postgresql表分區(qū)的資料請(qǐng)關(guān)注服務(wù)器之家其它相關(guān)文章!
原文鏈接:https://www.cnblogs.com/larrydpk/p/14944497.html