PostgreSQL 10.0 preview 功能增强 - 两段式索引(约束字段+附加字段)

本文涉及的产品
云原生数据库 PolarDB 分布式版,标准版 2核8GB
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
RDS PostgreSQL Serverless,0.5-4RCU 50GB 3个月
推荐场景:
对影评进行热评分析
简介:

标签

PostgreSQL , 10.0 , 约束覆盖索引


背景

如果我们有这样的查询

select * from tbl where c1=? and c2=? and c3=? and c4=?

我们建立了复合索引达到最好的查询性能

create index idx on tbl(c1,c2,c3,c4);

同时还有这样的约束

create unique index idx on tbl (c1,c2);

那么这样的场景中,我们就有两个索引。

PostgreSQL 10.0提供了一个新的功能,可以将这两个索引合并,只有一个索引的体积,同时支持这两个场景。。

create unique index idx on tbl (c1,c2) including (c3,c4);

这便是唯一约束+附加字段组合功能索引

详见

Hi hackers,  

I'm working on a patch that allows to combine covering and unique   
functionality for btree indexes.  

_Previous discussion was here:_  
1) Proposal thread   
<http://www.postgresql.org/message-id/55F2CCD0.7040608@postgrespro.ru>  
2) Message with proposal clarification   
<http://www.postgresql.org/message-id/55F84DF4.5030207@postgrespro.ru>  

In a nutshell, the feature allows to create index with "key" columns and   
"included" columns.  
"key" columns can be used as scan keys. Unique constraint relates only   
to "key" columns.  
"included" columns may be used as scan keys if they have suitable opclass.  
Both "key" and "included" columns can be returned from index by   
IndexOnlyScan.  

Btree is the default index and it's used everywhere. So it requires   
properly testing. Volunteers are welcome)  

_Use case:_  
- We have a table (c1, c2, c3, c4);  
- We need to have an unique index on (c1, c2).  
- We would like to have a covering index on all columns to avoid reading   
of heap pages.  

Old way:  
CREATE UNIQUE INDEX olduniqueidx ON oldt USING btree (c1, c2);  
CREATE INDEX oldcoveringidx ON oldt USING btree (c1, c2, c3, c4);  

What's wrong?  
Two indexes contain repeated data. Overhead to data manipulation   
operations and database size.  

New way:  
CREATE UNIQUE INDEX newidx ON newt USING btree (c1, c2) INCLUDING (c3, c4);  

The patch is attached.  
In 'test.sql' you can find a test with detailed comments on each step,   
and comparison of old and new indexes.  

New feature has following syntax:  
CREATE UNIQUE INDEX newidx ON newt USING btree (c1, c2) INCLUDING (c3, c4);  
Keyword INCLUDING defines the "included" columns of index. These columns   
aren't concern to unique constraint.  
Also, them are not stored in index inner pages. It allows to decrease   
index size.  

_Results:_  
1) Additional covering index is not required anymore.  
2) New index can use IndexOnlyScan on queries, where old index can't.  

For example,  
explain analyze select c1, c2 from newt where c1<10000 and c3<20;  

*more examples in 'test.sql'  

_Future work:_  
To do opclasses for "included" columns optional.  

CREATE TABLE tbl (c1 int, c4 box);  
CREATE UNIQUE INDEX idx ON tbl USING btree (c1) INCLUDING (c4);  

If we don't need c4 as an index scankey, we don't need any btree opclass   
on it.  
But we still want to have it in covering index for queries like  

SELECT c4 FROM tbl WHERE c1=1000;  
SELECT * FROM tbl WHERE c1=1000;  

--   
Anastasia Lubennikova  
Postgres Professional:http://www.postgrespro.com  
The Russian Postgres Company  

这个patch的讨论,详见邮件组,本文末尾URL。

PostgreSQL社区的作风非常严谨,一个patch可能在邮件组中讨论几个月甚至几年,根据大家的意见反复的修正,patch合并到master已经非常成熟,所以PostgreSQL的稳定性也是远近闻名的。

参考

https://commitfest.postgresql.org/13/961/

https://www.postgresql.org/message-id/flat/56168952.4010101@postgrespro.ru#56168952.4010101@postgrespro.ru

相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
目录
相关文章
|
5月前
|
关系型数据库 Serverless 分布式数据库
【公测】PolarDB PostgreSQL版Serverless功能免费使用​!
【公测】PolarDB PostgreSQL版Serverless功能免费使用​,公测于2024年3月28日开始,持续三个月,公测期间可以免费使用!
|
5月前
|
关系型数据库 PostgreSQL
PostgreSQL排序字段不唯一导致分页查询结果出现重复数据
PostgreSQL排序字段不唯一导致分页查询结果出现重复数据
113 0
|
存储 关系型数据库 数据库
深入了解 PostgreSQL:功能、特性和部署
PostgreSQL,通常简称为Postgres,是一款强大且开源的关系型数据库管理系统(RDBMS),它在数据存储和处理方面提供了广泛的功能和灵活性。本文将详细介绍 PostgreSQL 的功能、特性以及如何部署和使用它。
638 1
深入了解 PostgreSQL:功能、特性和部署
|
2月前
|
监控 关系型数据库 数据库
PostgreSQL的索引优化策略?
【8月更文挑战第26天】PostgreSQL的索引优化策略?
75 1
|
5月前
|
SQL 关系型数据库 数据库
实时计算 Flink版操作报错之使用SQL 将 PostgreSQL 的 date 类型字段转换为 TIMESTAMP 类型时遇到报错,该如何处理
在使用实时计算Flink版过程中,可能会遇到各种错误,了解这些错误的原因及解决方法对于高效排错至关重要。针对具体问题,查看Flink的日志是关键,它们通常会提供更详细的错误信息和堆栈跟踪,有助于定位问题。此外,Flink社区文档和官方论坛也是寻求帮助的好去处。以下是一些常见的操作报错及其可能的原因与解决策略。
|
5月前
|
运维 Cloud Native 关系型数据库
云原生数据仓库产品使用合集之原生数据仓库AnalyticDB PostgreSQL版如果是列存表的话, adb支持通过根据某个字段做upsert吗
阿里云AnalyticDB提供了全面的数据导入、查询分析、数据管理、运维监控等功能,并通过扩展功能支持与AI平台集成、跨地域复制与联邦查询等高级应用场景,为企业构建实时、高效、可扩展的数据仓库解决方案。以下是对AnalyticDB产品使用合集的概述,包括数据导入、查询分析、数据管理、运维监控、扩展功能等方面。
|
5月前
|
关系型数据库 Serverless 分布式数据库
PolarDB PostgreSQL版Serverless功能上线公测啦,公测期间免费使用!
Serverless数据库能够使得数据库集群资源随客户业务负载动态弹性扩缩,将客户从复杂的业务资源评估和运维工作中解放出来。PolarDB PostgreSQL版 Serverless提供了CPU、内存、存储、网络资源的实时弹性能力,构建计算与存储分离架构下的 PolarDB PostgreSQL版产品新形态。
|
2月前
|
SQL 关系型数据库 MySQL
SQL Server、MySQL、PostgreSQL:主流数据库SQL语法异同比较——深入探讨数据类型、分页查询、表创建与数据插入、函数和索引等关键语法差异,为跨数据库开发提供实用指导
【8月更文挑战第31天】SQL Server、MySQL和PostgreSQL是当今最流行的关系型数据库管理系统,均使用SQL作为查询语言,但在语法和功能实现上存在差异。本文将比较它们在数据类型、分页查询、创建和插入数据以及函数和索引等方面的异同,帮助开发者更好地理解和使用这些数据库。尽管它们共用SQL语言,但每个系统都有独特的语法规则,了解这些差异有助于提升开发效率和项目成功率。
228 0
|
2月前
|
关系型数据库 数据库 PostgreSQL
PostgreSQL索引维护看完这篇就够了
PostgreSQL索引维护看完这篇就够了
211 0
|
4月前
|
SQL 关系型数据库 PostgreSQL
PostgreSQL和greenplum的copy命令可以添加字段吗?
【6月更文挑战第5天】PostgreSQL和greenplum的copy命令可以添加字段吗?
81 3

相关产品

  • 云原生数据库 PolarDB
  • 云数据库 RDS PostgreSQL 版