PostgreSQL 13新特性:btree索引存储优化

btree索引在很多不同的场景中都可以使用,可以说是使用最为广泛的索引类型了。但是btree索引存在一个很大的不足之处就是比较占用存储空间,在PostgreSQL13中针对btree索引中的重复值做了很大的优化:通过将重复值只存储一次,大大节省了存储成本。

介绍:

More efficiently store duplicates in btree indexes (Anastasia Lubennikova, Peter Geoghegan)

This allows efficient btree indexing of low cardinality columns by storing duplicate keys only once. Users upgrading with pg_upgrade will need to use REINDEX to make use of this feature.

例子:
–PostgreSQL12中:
100万的重复数据,索引大小21MB。

bill=# create table t2(id int ,info text); 
CREATE TABLE
bill=# insert into t2 select 1,md5(random()::text) from generate_series(1,1000000);
INSERT 0 1000000
bill=# create index idx_t2 on t2 using btree(id);
CREATE INDEX
bill=# \di+ idx_t2
                       List of relations
 Schema |  Name  | Type  | Owner | Table | Size  | Description 
--------+--------+-------+-------+-------+-------+-------------
 public | idx_t2 | index | bill  | t2    | 21 MB | 
(1 row)

–PostgreSQL13中:
100万的重复数据,索引大小6792kB!

bill=# create table t2(id int ,info text); 
CREATE TABLE
bill=# insert into t2 select 1,md5(random()::text) from generate_series(1,1000000);
INSERT 0 1000000
bill=# create index idx_t2 on t2 using btree(id);
CREATE INDEX
bill=# \di+ idx_t2
                       List of relations
 Schema |  Name  | Type  | Owner | Table | Size  | Description 
--------+--------+-------+-------+-------+-------+-------------
 public | idx_t2 | index | bill  | t2    | 6792 kB | 
(1 row)

参考链接:
https://www.postgresql.org/docs/13/release-13.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章