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

本文涉及的产品
云数据库 RDS SQL Server,基础系列 2核4GB
云原生数据库 PolarDB 分布式版,标准版 2核8GB
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
简介:

标签

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=?  
AI 代码解读

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;  
AI 代码解读

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)  
AI 代码解读

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)  
AI 代码解读

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

相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
目录
打赏
0
0
0
0
20691
分享
相关文章
云原生数据仓库AnalyticDB PostgreSQL同一个SQL可以实现向量索引、全文索引GIN、普通索引BTREE混合查询,简化业务实现逻辑、提升查询性能
本文档介绍了如何在AnalyticDB for PostgreSQL中创建表、向量索引及混合检索的实现步骤。主要内容包括:创建`articles`表并设置向量存储格式,创建ANN向量索引,为表增加`username`和`time`列,建立BTREE索引和GIN全文检索索引,并展示了查询结果。参考文档提供了详细的SQL语句和配置说明。
49 1
PostgreSQL 9种索引的原理和应用场景
PostgreSQL 支持九种主要索引类型,包括 B-Tree、Hash、GiST、SP-GiST、GIN、BRIN、Bitmap、Partial 和 Unique 索引。每种索引适用于不同场景,如 B-Tree 适合范围查询和排序,Hash 仅用于等值查询,GiST 支持全文搜索和几何数据查询,GIN 适用于多值列和 JSON 数据,BRIN 适合非常大的表,Bitmap 适用于低基数列,Partial 只对部分数据创建索引,Unique 确保列值唯一。
PostgreSQL的索引优化策略?
【8月更文挑战第26天】PostgreSQL的索引优化策略?
192 1
SQL Server、MySQL、PostgreSQL:主流数据库SQL语法异同比较——深入探讨数据类型、分页查询、表创建与数据插入、函数和索引等关键语法差异,为跨数据库开发提供实用指导
【8月更文挑战第31天】SQL Server、MySQL和PostgreSQL是当今最流行的关系型数据库管理系统,均使用SQL作为查询语言,但在语法和功能实现上存在差异。本文将比较它们在数据类型、分页查询、创建和插入数据以及函数和索引等方面的异同,帮助开发者更好地理解和使用这些数据库。尽管它们共用SQL语言,但每个系统都有独特的语法规则,了解这些差异有助于提升开发效率和项目成功率。
820 0
PolarDB for PostgreSQL 开源必读手册-开源PolarDB for PostgreSQL架构介绍(中)
PolarDB for PostgreSQL 开源必读手册-开源PolarDB for PostgreSQL架构介绍
513 0
PolarDB for PostgreSQL 开源必读手册-开源PolarDB for PostgreSQL架构介绍(下)
PolarDB for PostgreSQL 开源必读手册-开源PolarDB for PostgreSQL架构介绍
430 0

相关产品

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