francs.tan
2018-12-18
2817浏览量
PostgreSQL 11 版本索引方面一个显著的新特性是创建索引时支持 INCLUDE COLUMNS ,语法如下
CREATE INDEX idx_name ON table_name USING BTREE (column_a) INCLUDE (column_b);
Allow indexes to INCLUDE columns that are not part of the unique constraint but are available for index-only scans (Anastasia Lubennikova, Alexander Korotkov, Teodor Sigaev)
This is also useful for including columns that dont have btree support
此特性主要用途和使用场景:
以上描述颇为费力,以下通过实例演示。
首先验证第一点:不支持Btree索引的字段支持使用INCLUDE方式索引,创建测试表。
francs=> CREATE TABLE t_json1(a serial, user_info json);
CREATE TABLE
在(a,user_info) 字段上创建 btree 索引,如下:
francs=> CREATE INDEX t_json1_idx1 ON t_json1 USING BTREE(a,user_info);
ERROR: data type json has no default operator class for access method "btree"
HINT: You must specify an operator class for the index or define a default operator class for the data type.
以上创建索引报错,是由于 json 不支持 btree 索引。
使用 INCLUDE 方式创建索引成功,如下:
francs=> CREATE INDEX t_json1_idx_include ON t_json1 USING btree(a) INCLUDE(user_info);
CREATE INDEX
创建测试表并插入300万数据,如下:
francs=> CREATE TABLE t_include(a int4, name text);
CREATE TABLE
francs=> INSERT INTO t_include(a,name) SELECT n,n || '_INCLUDE TEST' FROM generate_series(1,3000000) n;
INSERT 0 3000000
在字段a上创建索引,如下:
francs=> CREATE INDEX idx_t_include_a ON t_include USING BTREE (a);
CREATE INDEX
where条件中只包含a,查询a字段,以下SQL走了 Index Only Scan。
francs=> EXPLAIN ANALYZE SELECT a FROM t_include WHERE a<5;
QUERY PLAN
--------------------------------------------------------------------------------------------------------------------------------
Index Only Scan using idx_t_include_a on t_include (cost=0.43..8.50 rows=4 width=4) (actual time=0.017..0.018 rows=4 loops=1)
Index Cond: (a < 5)
Heap Fetches: 4
Planning Time: 0.272 ms
Execution Time: 0.038 ms
(5 rows)
加入 name 字段后,不走 Index Only Scan,如下。
francs=> EXPLAIN ANALYZE SELECT a,name FROM t_include WHERE a<5;
QUERY PLAN
----------------------------------------------------------------------------------------------------------------------------
Index Scan using idx_t_include_a on t_include (cost=0.43..8.50 rows=4 width=24) (actual time=0.005..0.007 rows=4 loops=1)
Index Cond: (a < 5)
Planning Time: 0.125 ms
Execution Time: 0.025 ms
(4 rows)
加入 name 后走了 Index Scan using ,根据索引回表查询name字段。
创建索引时使用 INCLUDE(name),如下
francs=> CREATE INDEX idx_t_include ON t_include USING BTREE (a) INCLUDE (name);
CREATE INDEX
再次执行查询,走了 Index Only Scan
francs=> VACUUM ANALYZE t_include;
VACUUM
francs=> EXPLAIN ANALYZE SELECT a,name FROM t_include WHERE a<5;
QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------------
Index Only Scan using idx_t_include on t_include (cost=0.43..4.50 rows=4 width=24) (actual time=0.017..0.018 rows=4 loops=1)
Index Cond: (a < 5)
Heap Fetches: 0
Planning Time: 0.175 ms
Execution Time: 0.038 ms
(5 rows)
只查询name字段,也走了 Index Only Scan,如下
francs=> EXPLAIN ANALYZE SELECT name FROM t_include WHERE a<5;
QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------------
Index Only Scan using idx_t_include on t_include (cost=0.43..4.50 rows=4 width=20) (actual time=0.012..0.014 rows=4 loops=1)
Index Cond: (a < 5)
Heap Fetches: 0
Planning Time: 0.163 ms
Execution Time: 0.038 ms
(5 rows)
或许有朋友问以下两种索引方式有啥区别? 这个问题欢迎大家留言讨论。
CREATE INDEX idx_t_not_include ON t_include USING BTREE (a,name);
CREATE INDEX idx_t_include ON t_include USING BTREE (a) INCLUDE (name);
最后推荐和张文升共同编写的《PostgreSQL实战》,本书基于PostgreSQL 10 编写,共18章,重点介绍SQL高级特性、并行查询、分区表、物理复制、逻辑复制、备份恢复、高可用、性能优化、PostGIS等,涵盖大量实战用例!
链接:https://item.jd.com/12405774.html
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
分享数据库前沿,解构实战干货,推动数据库技术变革