PostgreSQL 11 preview - BRIN索引接口功能扩展(BLOOM FILTER、min max分段)

本文主要是介绍PostgreSQL 11 preview - BRIN索引接口功能扩展(BLOOM FILTER、min max分段),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

标签

PostgreSQL , brin索引 , bloom filter , min max 分段


背景

BRIN索引是PG的一种块索引接口,存储指定连续数据块内被索引字段的元数据。

https://www.postgresql.org/docs/devel/static/brin.html

目前BRIN存储的元数据包括被索引字段在每个指定连续数据块区间的MIN,MAX值。所以对于比较分散的数据实际上效果是很差的,对于数据分布比较有时序属性的(或者说线性相关性很好)的字段,效果特别赞。

《HTAP数据库 PostgreSQL 场景与性能测试之 24 - (OLTP) 物联网 - 时序数据并发写入(含时序索引BRIN)》

《PostgreSQL BRIN索引的pages_per_range选项优化与内核代码优化思考》

《万亿级电商广告 - brin黑科技带你(最低成本)玩转毫秒级圈人(视觉挖掘姊妹篇) - 阿里云RDS PostgreSQL, HybridDB for PostgreSQL最佳实践》

《PostGIS空间索引(GiST、BRIN、R-Tree)选择、优化 - 阿里云RDS PostgreSQL最佳实践》

《自动选择正确索引访问接口(btree,hash,gin,gist,sp-gist,brin,bitmap...)的方法》

《PostgreSQL 并行写入堆表,如何保证时序线性存储 - BRIN索引优化》

《PostgreSQL 10.0 preview 功能增强 - BRIN 索引更新smooth化》

《PostgreSQL 聚集存储 与 BRIN索引 - 高并发行为、轨迹类大吞吐数据查询场景解说》

《PostgreSQL 物联网黑科技 - 瘦身几百倍的索引(BRIN index)》

《PostgreSQL 9.5 new feature - lets BRIN be used with R-Tree-like indexing strategies For "inclusion" opclasses》

《PostgreSQL 9.5 new feature - BRIN (block range index) index》

目前BRIN存在的可以改进的点:

当数据分布与HEAP存储的 线性相关性很差时,效果不好。如何改进呢?

多段MIN,MAX可能是一个非常有效果的改进方法,举个例子,我们有一个非常大的小区,有很多栋房子,然后每一栋房子我们保存了年龄最小和年龄最大的住户,比如说真实的分布是每栋楼都包含少部分是1-35岁,1个80岁的。

现在要找一位40岁的住户,如果是BRIN索引,会把所有的楼栋都返回给你原因是每栋楼的范围都是1-80岁。

如果使用多段存储,那么应该是1-35, 80。这样的话使用BRIN索引找40岁的住户直接返回0条记录。

1、现在PostgreSQL 11马上要提交的PATCH,就包含了multi min max的优化

https://commitfest.postgresql.org/17/1348/

2、第二个改进是引入了BRIN的BLOOM FILTER,我们知道BLOOM FILTER用少量的BIT位表示某被索引值是否存在,存在则设定这些BIT为1,如果对应的BITS不全为1,则说明没有这条记录。但是为了节约空间,BIT存在冲撞,例如某个值的BITS可能被其他一个或多个值的BITS覆盖。

那么就会出现一种情况,索引告诉你包含某个值,并不一定真的包含。但是索引告诉你不包含某个值,那就肯定不包含。

pic

所以

select * from tbl where a=? and b=? and c=? or d=?  

bloom会告诉你一个较大的结果集,然后再回HEAP表,使用FILTER过滤不满足条件的记录。

https://en.wikipedia.org/wiki/Bloom_filter

https://www.postgresql.org/docs/devel/static/bloom.html

目前使用bloom插件可以创建BLOOM索引,而PostgreSQL 11,会把这个功能加入BRIN索引接口中。

min max 分段

这个是POC里面的例子,可以看到使用分段MIN MAX后,BRIN索引的过滤性好了很多。

PATCH连接

https://commitfest.postgresql.org/17/1348/

https://www.postgresql.org/message-id/flat/c1138ead-7668-f0e1-0638-c3be3237e812@2ndquadrant.com#c1138ead-7668-f0e1-0638-c3be3237e812@2ndquadrant.com

To illustrate the improvement, consider this table:

    create table a (val float8) with (fillfactor = 90);  insert into a select i::float from generate_series(1,10000000) s(i);  update a set val = 1 where random() < 0.01;  update a set val = 10000000 where random() < 0.01;  

Which means the column 'val' is almost perfectly correlated with the
position in the table (which would be great for BRIN minmax indexes),
but then 1% of the values is set to 1 and 10.000.000. That means pretty
much every range will be [1,10000000], which makes this BRIN index
mostly useless, as illustrated by these explain plans:

    create index on a using brin (val) with (pages_per_range = 16);  explain analyze select * from a where val = 100;  QUERY PLAN  --------------------------------------------------------------------  Bitmap Heap Scan on a  (cost=54.01..10691.02 rows=8 width=8)  (actual time=5.901..785.520 rows=1 loops=1)  Recheck Cond: (val = '100'::double precision)  Rows Removed by Index Recheck: 9999999  Heap Blocks: lossy=49020  ->  Bitmap Index Scan on a_val_idx  (cost=0.00..54.00 rows=3400 width=0)  (actual time=5.792..5.792 rows=490240 loops=1)  Index Cond: (val = '100'::double precision)  Planning time: 0.119 ms  Execution time: 785.583 ms  (8 rows)  explain analyze select * from a where val between 100 and 10000;  QUERY PLAN  ------------------------------------------------------------------  Bitmap Heap Scan on a  (cost=55.94..25132.00 rows=7728 width=8)  (actual time=5.939..858.125 rows=9695 loops=1)  Recheck Cond: ((val >= '100'::double precision) AND  (val <= '10000'::double precision))  Rows Removed by Index Recheck: 9990305  Heap Blocks: lossy=49020  ->  Bitmap Index Scan on a_val_idx  (cost=0.00..54.01 rows=10200 width=0)  (actual time=5.831..5.831 rows=490240 loops=1)  Index Cond: ((val >= '100'::double precision) AND  (val <= '10000'::double precision))  Planning time: 0.139 ms  Execution time: 871.132 ms  (8 rows)  

Obviously, the queries do scan the whole table and then eliminate most
of the rows in "Index Recheck". Decreasing pages_per_range does not
really make a measurable difference in this case - it eliminates maybe
10% of the rechecks, but most pages still have very wide minmax range.

With the patch, it looks about like this:

    create index on a using brin (val float8_minmax_multi_ops)  with (pages_per_range = 16);  explain analyze select * from a where val = 100;  QUERY PLAN  -------------------------------------------------------------------  Bitmap Heap Scan on a  (cost=830.01..11467.02 rows=8 width=8)  (actual time=7.772..8.533 rows=1 loops=1)  Recheck Cond: (val = '100'::double precision)  Rows Removed by Index Recheck: 3263  Heap Blocks: lossy=16  ->  Bitmap Index Scan on a_val_idx  (cost=0.00..830.00 rows=3400 width=0)  (actual time=7.729..7.729 rows=160 loops=1)  Index Cond: (val = '100'::double precision)  Planning time: 0.124 ms  Execution time: 8.580 ms  (8 rows)  explain analyze select * from a where val between 100 and 10000;  QUERY PLAN  ------------------------------------------------------------------  Bitmap Heap Scan on a  (cost=831.94..25908.00 rows=7728 width=8)  (actual time=9.318..23.715 rows=9695 loops=1)  Recheck Cond: ((val >= '100'::double precision) AND  (val <= '10000'::double precision))  Rows Removed by Index Recheck: 3361  Heap Blocks: lossy=64  ->  Bitmap Index Scan on a_val_idx  (cost=0.00..830.01 rows=10200 width=0)  (actual time=9.274..9.274 rows=640 loops=1)  Index Cond: ((val >= '100'::double precision) AND  (val <= '10000'::double precision))  Planning time: 0.138 ms  Execution time: 36.100 ms  (8 rows)  

bloom filter

https://www.postgresql.org/docs/devel/static/bloom.html

参考

https://commitfest.postgresql.org/17/1348/

https://www.postgresql.org/message-id/flat/c1138ead-7668-f0e1-0638-c3be3237e812@2ndquadrant.com#c1138ead-7668-f0e1-0638-c3be3237e812@2ndquadrant.com

这篇关于PostgreSQL 11 preview - BRIN索引接口功能扩展(BLOOM FILTER、min max分段)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/338457

相关文章

Deepseek R1模型本地化部署+API接口调用详细教程(释放AI生产力)

《DeepseekR1模型本地化部署+API接口调用详细教程(释放AI生产力)》本文介绍了本地部署DeepSeekR1模型和通过API调用将其集成到VSCode中的过程,作者详细步骤展示了如何下载和... 目录前言一、deepseek R1模型与chatGPT o1系列模型对比二、本地部署步骤1.安装oll

MyBatis-Flex BaseMapper的接口基本用法小结

《MyBatis-FlexBaseMapper的接口基本用法小结》本文主要介绍了MyBatis-FlexBaseMapper的接口基本用法小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具... 目录MyBATis-Flex简单介绍特性基础方法INSERT① insert② insertSelec

Spring排序机制之接口与注解的使用方法

《Spring排序机制之接口与注解的使用方法》本文介绍了Spring中多种排序机制,包括Ordered接口、PriorityOrdered接口、@Order注解和@Priority注解,提供了详细示例... 目录一、Spring 排序的需求场景二、Spring 中的排序机制1、Ordered 接口2、Pri

Idea实现接口的方法上无法添加@Override注解的解决方案

《Idea实现接口的方法上无法添加@Override注解的解决方案》文章介绍了在IDEA中实现接口方法时无法添加@Override注解的问题及其解决方法,主要步骤包括更改项目结构中的Languagel... 目录Idea实现接China编程口的方法上无法添加@javascriptOverride注解错误原因解决方

SpringBoot整合DeepSeek实现AI对话功能

《SpringBoot整合DeepSeek实现AI对话功能》本文介绍了如何在SpringBoot项目中整合DeepSeekAPI和本地私有化部署DeepSeekR1模型,通过SpringAI框架简化了... 目录Spring AI版本依赖整合DeepSeek API key整合本地化部署的DeepSeek

Python实现多路视频多窗口播放功能

《Python实现多路视频多窗口播放功能》这篇文章主要为大家详细介绍了Python实现多路视频多窗口播放功能的相关知识,文中的示例代码讲解详细,有需要的小伙伴可以跟随小编一起学习一下... 目录一、python实现多路视频播放功能二、代码实现三、打包代码实现总结一、python实现多路视频播放功能服务端开

css实现图片旋转功能

《css实现图片旋转功能》:本文主要介绍了四种CSS变换效果:图片旋转90度、水平翻转、垂直翻转,并附带了相应的代码示例,详细内容请阅读本文,希望能对你有所帮助... 一 css实现图片旋转90度.icon{ -moz-transform:rotate(-90deg); -webkit-transfo

Java实现Elasticsearch查询当前索引全部数据的完整代码

《Java实现Elasticsearch查询当前索引全部数据的完整代码》:本文主要介绍如何在Java中实现查询Elasticsearch索引中指定条件下的全部数据,通过设置滚动查询参数(scrol... 目录需求背景通常情况Java 实现查询 Elasticsearch 全部数据写在最后需求背景通常情况下

Pandas中多重索引技巧的实现

《Pandas中多重索引技巧的实现》Pandas中的多重索引功能强大,适用于处理多维数据,本文就来介绍一下多重索引技巧,具有一定的参考价值,感兴趣的可以了解一下... 目录1.多重索引概述2.多重索引的基本操作2.1 选择和切片多重索引2.2 交换层级与重设索引3.多重索引的高级操作3.1 多重索引的分组聚

Java function函数式接口的使用方法与实例

《Javafunction函数式接口的使用方法与实例》:本文主要介绍Javafunction函数式接口的使用方法与实例,函数式接口如一支未完成的诗篇,用Lambda表达式作韵脚,将代码的机械美感... 目录引言-当代码遇见诗性一、函数式接口的生物学解构1.1 函数式接口的基因密码1.2 六大核心接口的形态学