PostgreSQL 10.0 preview 优化器改进 - 驱动索引+quicksort支持更多sort场景

简介:

标签

PostgreSQL , 10.0 , 排序 , 不完整索引排序


背景

当我们在使用数据库时,排序是一个比较常见的需求,排序有几种方法,使用索引,或者访问堆表然后显示的排序。

当使用索引排序时,索引必须包含排序列,同时必须是驱动列包含排序列。

例如

order by a,b,c,那么可使用索引(a,b,c,*)  

但是order by a,b,c能使用索引(a,b)或者(a)吗?  

实际上使用不完整索引,复合排序时,我们可以分为两个阶段,一个节点是按索引扫描,当扫描到一样的索引数据时,再从HEAP读取其他需要排序的列,然后使用quicksort或者其他排序手段对剩余字段进行排序。

这样原本需要再创建一个复合索引来支持复合排序就不必了。

Hackers!  

Currently when we need to get ordered result from table we have to choose  
one of two approaches: get results from index in exact order we need or do  
sort of tuples. However, it could be useful to mix both methods: get  
results from index in order which partially meets our requirements and do  
rest of work from heap.  

Two attached patches are proof of concept for this approach.  

*partial-sort-1.patch*  

This patch allows to use index for order-by if order-by clause and index  
has non-empty common prefix. So, index gives right ordering for first n  
order-by columns. In order to provide right order for rest m columns, sort  
node is inserted. This sort node sorts groups of tuples where values of  
first n order-by columns are equal.  

See an example.  

create table test as (select id, (random()*10000)::int as v1, random() as  
v2 from generate_series(1,1000000) id);  
create index test_v1_idx on test (v1);  

We've index by v1 column, but we can get results ordered by v1, v2.  

postgres=# select * from test order by v1, v2 limit 10;  
   id   | v1 |         v2  
--------+----+--------------------  
 390371 |  0 | 0.0284479795955122  
 674617 |  0 | 0.0322008323855698  
 881905 |  0 |  0.042586590629071  
 972877 |  0 | 0.0531588457524776  
 364903 |  0 | 0.0594307743012905  
  82333 |  0 | 0.0666455538012087  
 266488 |  0 |  0.072808934841305  
 892215 |  0 | 0.0744258034974337  
  13805 |  0 | 0.0794667331501842  
 338435 |  0 |  0.171817752998322  
(10 rows)  

And it's fast using following plan.  

                                                                QUERY PLAN  

------------------------------------------------------------------------------------------------------------------------------------------  
 Limit  (cost=69214.06..69214.08 rows=10 width=16) (actual  
time=0.097..0.099 rows=10 loops=1)  
   ->  Sort  (cost=69214.06..71714.06 rows=1000000 width=16) (actual  
time=0.096..0.097 rows=10 loops=1)  
         Sort Key: v1, v2  
         Sort Method: top-N heapsort  Memory: 25kB  
         ->  Index Scan using test_v1_idx on test  (cost=0.42..47604.42  
rows=1000000 width=16) (actual time=0.017..0.066 rows=56 loops=1)  
 Total runtime: 0.125 ms  
(6 rows)  

For sure, this approach is effective only when first n order-by columns we  
selected provides enough count of unique values (so, sorted groups are  
small). Patch is only PoC because it doesn't contains any try to estimate  
right cost of using partial sort.  


*partial-knn-1.patch*  

KNN-GiST provides ability to get ordered results from index, but this order  
is based only on index information. For instance, GiST index contains  
bounding rectangles for polygons, and we can't get exact distance to  
polygon from index (similar situation is in PostGIS). In attached patch,  
GiST distance method can set recheck flag (similar to consistent method).  
This flag means that distance method returned lower bound of distance and  
we should recheck it from heap.  

See an example.  

create table test as (select id, polygon(3+(random()*10)::int,  
circle(point(random(), random()), 0.0003 + random()*0.001)) as p from  
generate_series(1,1000000) id);  
create index test_idx on test using gist (p);  

We can get results ordered by distance from polygon to point.  

postgres=# select id, p <-> point(0.5,0.5) from test order by p <->  
point(0.5,0.5) limit 10;  
   id   |       ?column?  
--------+----------------------  
 755611 | 0.000405855808916853  
 807562 | 0.000464123777564343  
 437778 | 0.000738524708741959  
 947860 |  0.00076250998760724  
 389843 | 0.000886362723569568  
  17586 | 0.000981960100555216  
 411329 |  0.00145338112316853  
 894191 |  0.00149399559703506  
 391907 |   0.0016647896049741  
 235381 |  0.00167554614889509  
(10 rows)  

It's fast using just index scan.  

                                                            QUERY PLAN  

----------------------------------------------------------------------------------------------------------------------------------  
 Limit  (cost=0.29..1.86 rows=10 width=36) (actual time=0.180..0.230  
rows=10 loops=1)  
   ->  Index Scan using test_idx on test  (cost=0.29..157672.29  
rows=1000000 width=36) (actual time=0.179..0.228 rows=10 loops=1)  
         Order By: (p <-> '(0.5,0.5)'::point)  
 Total runtime: 0.305 ms  
(4 rows)  

This patch is also only PoC because of following:  
1) It's probably wrong at all to get heap tuple from index scan node. This  
work should be done from another node.  
2) Assumption that order-by operator returns float8 comparable with GiST  
distance method result in general case is wrong.  

------  
With best regards,  
Alexander Korotkov.  

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

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

参考

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

https://www.postgresql.org/message-id/flat/CAPpHfdscOX5an71nHd8WSUH6GNOCf=V7wgDaTXdDd9=goN-gfA@mail.gmail.com#CAPpHfdscOX5an71nHd8WSUH6GNOCf=V7wgDaTXdDd9=goN-gfA@mail.gmail.com

相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍如何基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
目录
相关文章
|
关系型数据库 物联网 PostgreSQL
沉浸式学习PostgreSQL|PolarDB 11: 物联网(IoT)、监控系统、应用日志、用户行为记录等场景 - 时序数据高吞吐存取分析
物联网场景, 通常有大量的传感器(例如水质监控、气象监测、新能源汽车上的大量传感器)不断探测最新数据并上报到数据库. 监控系统, 通常也会有采集程序不断的读取被监控指标(例如CPU、网络数据包转发、磁盘的IOPS和BW占用情况、内存的使用率等等), 同时将监控数据上报到数据库. 应用日志、用户行为日志, 也就有同样的特征, 不断产生并上报到数据库. 以上数据具有时序特征, 对数据库的关键能力要求如下: 数据高速写入 高速按时间区间读取和分析, 目的是发现异常, 分析规律. 尽量节省存储空间
1313 1
|
关系型数据库 分布式数据库 对象存储
沉浸式学习PostgreSQL|PolarDB 5: 零售连锁、工厂等数字化率较低场景的数据分析
零售连锁, 制作业的工厂等场景中, 普遍数字化率较低, 通常存在这些问题: 数据离线, 例如每天盘点时上传, 未实现实时汇总到数据库中. 数据格式多, 例如excel, csv, txt, 甚至纸质手抄. 让我们一起来思考一下, 如何使用较少的投入实现数据汇总分析?
685 0
|
存储 监控 关系型数据库
B-tree不是万能药:PostgreSQL索引失效的7种高频场景与破解方案
在PostgreSQL优化实践中,B-tree索引虽承担了80%以上的查询加速任务,但因多种原因可能导致索引失效,引发性能骤降。本文深入剖析7种高频失效场景,包括隐式类型转换、函数包裹列、前导通配符等,并通过实战案例揭示问题本质,提供生产验证的解决方案。同时,总结索引使用决策矩阵与关键原则,助你让索引真正发挥作用。
|
关系型数据库 Go 网络安全
go语言中PostgreSQL驱动安装
【11月更文挑战第2天】
1074 5
|
关系型数据库 定位技术 分布式数据库
沉浸式学习PostgreSQL|PolarDB 18: 通过GIS轨迹相似伴随|时态分析|轨迹驻点识别等技术对拐卖、诱骗场景进行侦查
本文主要教大家怎么用好数据库, 而不是怎么运维管理数据库、怎么开发数据库内核.
1963 1
|
关系型数据库 分布式数据库 PolarDB
沉浸式学习PostgreSQL|PolarDB 7: 移动社交、多媒体、内容分发、游戏业务场景, 跨地域多机房的智能加速
在移动社交、多媒体、内容分发业务场景中, 如果用户要交互的内容都在中心网络(假设深圳), 现在用户流动非常频繁, 当用户从深圳出差到北京, 因为网络延迟急剧增加, 他的访问体验就会变得非常差. 网络延迟对游戏业务的影响则更加严重. 为了解决这个问题, 企业会将业务部署在全国各地, 不管用户在哪里出差, 他都可以就近访问最近的中心. 由于标记用户的只有IP地址, 怎么根据用户的接入IP来判断他应该访问哪个中心呢? 通过这个实验, 大家可以了解到在数据库中如何存储IP地址范围和各中心IDC的映射关系, 以及如何根据用户的来源IP(接入IP)来判断他应该去哪个中心IDC访问.
476 0
|
关系型数据库 分布式数据库 数据库
沉浸式学习PostgreSQL|PolarDB 4: 跨境电商场景, 快速判断商标|品牌侵权
很多业务场景中需要判断商标侵权, 避免纠纷. 例如 电商的商品文字描述、图片描述中可能有侵权内容. 特别是跨境电商, 在一些国家侵权查处非常严厉. 注册公司名、产品名时可能侵权. 在写文章时, 文章的文字内容、视频内容、图片内容中的描述可能侵权. 例如postgresql是个商标, 如果你使用posthellogresql、postgresqlabc也可能算侵权. 以跨境电商为力, 为了避免侵权, 在发布内容时需要商品描述中出现的品牌名、产品名等是否与已有的商标库有相似. 对于跨境电商场景, 由于店铺和用户众多, 商品的修改、发布是比较高频的操作, 所以需要实现高性能的字符串相似匹配功能.
631 0
|
存储 JSON 关系型数据库
PostgreSQL Json应用场景介绍和Shared Detoast优化
PostgreSQL Json应用场景介绍和Shared Detoast优化
|
存储 SQL 人工智能
4 PostgreSQL 索引,全文检索,模糊匹配,近似度匹配(三)
4 PostgreSQL 索引,全文检索,模糊匹配,近似度匹配(三)
4 PostgreSQL 索引,全文检索,模糊匹配,近似度匹配(三)
|
关系型数据库 分布式数据库 数据库
沉浸式学习PostgreSQL|PolarDB 1: 短视频推荐去重、UV统计分析场景
本实验场景:短视频推荐去重、UV统计分析场景. 欢迎一起来建设数据库沉浸式学习教学素材库, 帮助开发者用好数据库, 提升开发者竞争力, 为企业降本提效. 本文的实验可以使用永久免费的云起实验室来完成. https://developer.aliyun.com/adc/scenario/exp/f55dbfac77c0467a9d3cd95ff6697a31 如果你本地有docker环境也可以把镜像拉到本地来做实验.
873 0

相关产品

  • 云原生数据库 PolarDB
  • 云数据库 RDS PostgreSQL 版
  • 推荐镜像

    更多