ITPub博客

首页 > 数据库 > PostgreSQL > PostgreSQL DBA(139) - PG 12(B-tree index improvement 1#)

PostgreSQL DBA(139) - PG 12(B-tree index improvement 1#)

翻译 PostgreSQL 作者:husthxd 时间:2019-12-04 17:33:16 3 删除 编辑

本节简单介绍了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

PG 12

从上面两个图可以看出,PG 11的索引leaf page发生在middle,而PG 12发生在rightmost,middle叶子节点的page稠密度明显要比PG 11好很多。

参考资料

Make heap TID a tiebreaker nbtree index column.

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/6906/viewspace-2666917/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
ITPUB数据库版块资深版主,对Oracle、PostgreSQL有深入研究。

注册时间:2007-12-28

  • 博文量
    1465
  • 访问量
    3897235