PostgreSQL DBA(139) - PG 12(B-tree index improvement 1#)
本節簡單介紹了PostgreSQL 12 B-tree的改進:索引出現很多重複值時提升效能,減少空間佔用。
PG 11
建立資料表,建立索引
[local]:5110 xdb@testdb=# drop table rel;
DROP TABLE
Time: 130.868 ms
[local]:5110 xdb@testdb=# CREATE TABLE rel (
xdb@testdb(# aid bigint NOT NULL,
xdb@testdb(# bid bigint NOT NULL
xdb@testdb(# );
CREATE TABLE
Time: 16.041 ms
[local]:5110 xdb@testdb=#
[local]:5110 xdb@testdb=# ALTER TABLE rel
xdb@testdb-# ADD CONSTRAINT rel_pkey PRIMARY KEY (aid, bid);
ALTER TABLE
Time: 5.236 ms
[local]:5110 xdb@testdb=#
[local]:5110 xdb@testdb=# CREATE INDEX rel_bid_idx ON rel (bid);
CREATE INDEX
Time: 1.838 ms
[local]:5110 xdb@testdb=#
[local]:5110 xdb@testdb=# INSERT INTO rel (aid, bid)
xdb@testdb-# SELECT i, i / 10000
xdb@testdb-# FROM generate_series(1, 20000000) AS i;
INSERT 0 20000000
Time: 152699.275 ms (02:32.699)
[local]:5110 xdb@testdb=#
[local]:5110 xdb@testdb=#
檢視索引資訊
[local]:5110 xdb@testdb=#
[local]:5110 xdb@testdb=# \d rel
Table "public.rel"
Column | Type | Collation | Nullable | Default
--------+--------+-----------+----------+---------
aid | bigint | | not null |
bid | bigint | | not null |
Indexes:
"rel_pkey" PRIMARY KEY, btree (aid, bid)
"rel_bid_idx" btree (bid)
[local]:5110 xdb@testdb=# \di+ rel_pkey
List of relations
Schema | Name | Type | Owner | Table | Size | Description
--------+----------+-------+-------+-------+--------+-------------
public | rel_pkey | index | xdb | rel | 602 MB |
(1 row)
[local]:5110 xdb@testdb=# \di+ rel_bid_idx
List of relations
Schema | Name | Type | Owner | Table | Size | Description
--------+-------------+-------+-------+-------+--------+-------------
public | rel_bid_idx | index | xdb | rel | 545 MB |
(1 row)
PG 12
建立資料表,建立索引
[local:/run/pg12]:5120 pg12@testdb=# \timing on
Timing is on.
[local:/run/pg12]:5120 pg12@testdb=# drop table rel;
DROP TABLE
Time: 279.144 ms
[local:/run/pg12]:5120 pg12@testdb=# CREATE TABLE rel (
pg12@testdb(# aid bigint NOT NULL,
pg12@testdb(# bid bigint NOT NULL
pg12@testdb(# );
CREATE TABLE
Time: 1.579 ms
[local:/run/pg12]:5120 pg12@testdb=#
[local:/run/pg12]:5120 pg12@testdb=# ALTER TABLE rel
pg12@testdb-# ADD CONSTRAINT rel_pkey PRIMARY KEY (aid, bid);
ALTER TABLE
Time: 3.450 ms
[local:/run/pg12]:5120 pg12@testdb=#
[local:/run/pg12]:5120 pg12@testdb=# CREATE INDEX rel_bid_idx ON rel (bid);
CREATE INDEX
Time: 1.201 ms
[local:/run/pg12]:5120 pg12@testdb=#
[local:/run/pg12]:5120 pg12@testdb=# INSERT INTO rel (aid, bid)
pg12@testdb-# SELECT i, i / 10000
pg12@testdb-# FROM generate_series(1, 20000000) AS i;
INSERT 0 20000000
Time: 124503.212 ms (02:04.503)
[local:/run/pg12]:5120 pg12@testdb=#
檢視索引資訊
[local:/run/pg12]:5120 pg12@testdb=# \di+ rel_pkey
List of relations
Schema | Name | Type | Owner | Table | Size | Description
--------+----------+-------+-------+-------+--------+-------------
public | rel_pkey | index | pg12 | rel | 601 MB |
(1 row)
[local:/run/pg12]:5120 pg12@testdb=# \di+ rel_bid_idx
List of relations
Schema | Name | Type | Owner | Table | Size | Description
--------+-------------+-------+-------+-------+--------+-------------
public | rel_bid_idx | index | pg12 | rel | 408 MB |
(1 row)
[local:/run/pg12]:5120 pg12@testdb=#
可以看到PK沒有太大的變化,但有很多重複值的bid列索引則有明顯的變化,比PG 11少了25%的空間。
原理
PG 11 vs PG 12
從上面兩個圖可以看出,PG 11的索引leaf page發生在middle,而PG 12發生在rightmost,middle葉子節點的page稠密度明顯要比PG 11好很多。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/6906/viewspace-2666917/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- PostgreSQL DBA(41) - PG Index PropertiesSQLIndex
- PostgreSQL DBA(40) - PG 12 pg_promoteSQL
- PostgreSQL DBA(125) - PG 12(TPCC)SQL
- PostgreSQL DBA(82) - PG 12 Improving COPYSQL
- PostgreSQL DBA(92) - PG 12 Improving VACUUMSQL
- PostgreSQL DBA(67) - PG 12 SQLJSON pathSQLJSON
- PostgreSQL DBA(53) - PG 12 Generated columnsSQL
- PostgreSQL DBA(37) - PG 12 REINDEX CONCURRENTLYSQLIndex
- PostgreSQL DBA(39) - PG 12 Functions for partitionsSQLFunction
- PostgreSQL DBA(36) - PG 12 Inlined WITH queriesSQLinline
- PostgreSQL DBA(142) - PG 12(Monitoring PostgreSQL VACUUM processes)SQL
- PostgreSQL DBA(94) - PG 12 Improving Partition(Select)SQL
- PostgreSQL DBA(93) - PG 12 Improving Partition(Insert)SQL
- PostgreSQL DBA(62) - PG 12 More progress reportingSQL
- PostgreSQL DBA(70) - PG 12 Add SETTINGS option to EXPLAINSQLAI
- PostgreSQL DBA(98) - PG 12 Faster float conversion to textSQLAST
- PostgreSQL DBA(95) - PG 12 Partition(out of shared memory)SQL
- PostgreSQL DBA(126) - PG 12(搭建流複製)SQL
- PostgreSQL DBA(149) - PG 12(Add SETTINGS option to EXPLAIN)SQLAI
- PostgreSQL DBA(38) - PG 12 Connection slots and WAL sendersSQL
- PostgreSQL DBA(141) - PG 12(Discovering less-known PostgreSQL v12 features)SQL
- PostgreSQL DBA(59) - Index(Bloom)SQLIndexOOM
- PostgreSQL DBA(52) - Index(RUM)SQLIndex
- PostgreSQL DBA(47) - Index(Btree)SQLIndex
- PostgreSQL DBA(48) - Index(GiST)SQLIndex
- PostgreSQL DBA(51) - Index(GIN)SQLIndex
- PostgreSQL DBA(43) - Index(Hash)SQLIndex
- PostgreSQL DBA(53) - Index(BRIN)SQLIndex
- PostgreSQL DBA(91) - PG upgradeSQL
- PostgreSQL DBA(140) - PG 12(Don't log incomplete startup packet if it's empty)SQL
- PostgreSQL DBA(170) - pgAdmin(Queries For Index Maintenance)SQLIndexAINaN
- PostgreSQL DBA(49) - Index(SP-GiST)SQLIndex
- PostgreSQL DBA(83) - Extension(pg_buffercache)SQL
- PostgreSQL DBA(84) - Extension(pg_prewarm)SQL
- PostgreSQL DBA(162) - Extension(pg_catcheck)SQL
- PostgreSQL DBA(63) - Extension(pg_qualstats)SQL
- PostgreSQL DBA(163) - Extension(pg_cron)SQL
- PostgreSQL DBA(46) - PG Operator classes and familiesSQL