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