Greenplum 最佳实践 - 什么时候选择bitmap索引

简介:

标签

PostgreSQL , Greenplum , bitmap index


背景

PostgreSQL 目前支持8种索引接口,包括B-Tree, hash, gin, gist, sp-gist, brin, rum, bloom。

Greenplum 目前支持B-Tree, GiST, bitmap三种索引接口。

用户可以根据不同的数据类型,不同的请求类型,使用不同的索引接口建立相应的索引。例如对于数组,全文检索类型,可以使用GIN索引,对于地理位置数据,范围数据类型,图像特征值数据,几何类数据等,可以选择GiST索引。

PG的八种索引的介绍,可以参考bruce写的index internal、源码以及如下文档

http://leopard.in.ua/2015/04/13/postgresql-indexes#.WRHHH_mGOiQ

bitmap index 原理

如图所示,bitmap索引将每个被索引的VALUE作为KEY,使用每个BIT表示一行,当这行中包含这个VALUE时,设置为1,否则设置为0。

pic

如何从bitmap 索引检索数据,并查找到对应HEAP表的记录呢?

必须要有一个mapping 转换功能(函数),才能将BIT位翻译为行号。例如第一个BIT代表第一行,。。。以此类推。(当然了,mapping函数没有这么简单,还有很多优化技巧)

bitmap 的优化技术举例,比如

1. 压缩

例如连续的0或1可以被压缩,具体可以参考WIKI里面关于BITMAP的压缩算法,算法也是比较多的。

2. 分段或分段压缩

例如,每个数据块作为一个分段,每个分段内,记录这个数据块中的VALU对应的BIT信息。

3. 排序

排序是为了更好的进行压缩,例如堆表按被索引的列进行排序后,每个VALUE对应的行号就都是连续的了,压缩比很高。

另外用户也可以参考一下roaring bitmap这个位图库,应用非常广泛,效果也很不错。

https://github.com/zeromax007/gpdb-roaringbitmap

https://github.com/RoaringBitmap/CRoaring

bitmap index 适合什么场景

从bitmap index的结构我们了解到,被索引的列上面,每一个value都分别对应一个BIT串,BIT串的长度是记录数,每个BIT代表一行,1表示该行存在这个值,0表示该行不存在这个值。

因此bitmap index索引的列,不能有太多的VALUE,最好是100到10万个VALUE,也就是说,这样的表的BITMAP索引有100到10万条BIT串。

当我们对这个表的这个字段进行类似这样的查询时,效率就非常高。

select * from table where col = a and col = b and col2=xxx;        
-- a,b的bit串进行BITAND的操作,然后再和col2=xxx的BIT进行BITAND操作,返回BIT位为1的,使用bitmap function返回行号,取记录。      
      
select count(*) from table where col = a and col = b and col2=xxx;        
-- a,b的bit串进行BITAND的操作,然后再和col2=xxx的BIT进行BITAND操作,返回BIT位为1的,使用bitmap function返回行号,取记录,算count(*)。      

1. 适合有少量不重复值的列 。

2. 适合多个条件的查询,条件越多,bit and,or 的操作过滤掉的数据就越多,返回结果集越少。

bitmap index 不适合什么场景

由于每个VALUE都要记录每行的BIT位,所以如果有1亿条记录,那么每个VALUE的BIT串长度就是1亿。如果有100万个不同的VALUE,那么BITMAP INDEX就有100万个长度为1亿的bit串。

1. 不适合有太多不重复值的表字段。

3. 同样,也不适合有太少不重复值的列,例如男女。这样的列,除非可以和其他列组合赛选出很少量的结果集,否则返回的结果集是非常庞大的,也是不适合的。

3. 不适合频繁的更新,因为更新可能带来行迁移,以及VALUE的变化。如果是行迁移,需要更新整个bitmap串。如果是VALUE变化,则需要修改整个与变化相关的VALUE的BIT串。

greenplum bitmap index手册

About Bitmap Indexes

Greenplum Database provides the Bitmap index type.
Bitmap indexes are best suited to data warehousing applications and decision support systems with large amounts of data, many ad hoc queries, and few data modification (DML) transactions.

An index provides pointers to the rows in a table that contain a given key value.
A regular index stores a list of tuple IDs for each key corresponding to the rows with that key value.
Bitmap indexes store a bitmap for each key value. Regular indexes can be several times larger than the data in the table,
but bitmap indexes provide the same functionality as a regular index and use a fraction of the size of the indexed data.

Each bit in the bitmap corresponds to a possible tuple ID. If the bit is set, the row with the corresponding tuple ID contains the key value.
A mapping function converts the bit position to a tuple ID. Bitmaps are compressed for storage.
If the number of distinct key values is small, bitmap indexes are much smaller, compress better,
and save considerable space compared with a regular index.
The size of a bitmap index is proportional to the number of rows in the table times the number of distinct values in the indexed column.

Bitmap indexes are most effective for queries that contain multiple conditions in the WHERE clause.
Rows that satisfy some, but not all, conditions are filtered out before the table is accessed.
This improves response time, often dramatically.

When to Use Bitmap Indexes

Bitmap indexes are best suited to data warehousing applications where users query the data rather than update it.
Bitmap indexes perform best for columns that have between 100 and 100,000 distinct values and when the indexed column is often queried in conjunction with other indexed columns.
Columns with fewer than 100 distinct values, such as a gender column with two distinct values (male and female),
usually do not benefit much from any type of index.
On a column with more than 100,000 distinct values, the performance and space efficiency of a bitmap index decline.

Bitmap indexes can improve query performance for ad hoc queries.
AND and OR conditions in the WHERE clause of a query can be resolved quickly by performing the corresponding Boolean operations directly on the bitmaps before converting the resulting bitmap to tuple ids.
If the resulting number of rows is small, the query can be answered quickly without resorting to a full table scan.

When Not to Use Bitmap Indexes

Do not use bitmap indexes for unique columns or columns with high cardinality data,
such as customer names or phone numbers.
The performance gains and disk space advantages of bitmap indexes start to diminish on columns with 100,000 or more unique values,
regardless of the number of rows in the table.

Bitmap indexes are not suitable for OLTP applications with large numbers of concurrent transactions modifying the data.

Use bitmap indexes sparingly. Test and compare query performance with and without an index.
Add an index only if query performance improves with indexed columns.

greenplum中如何创建bitmap index

CREATE INDEX title_bmp_idx ON films USING bitmap (title);    

bitmap 在PG数据库中的应用

在PostgreSQL中虽然没有bitmap索引,但是多个条件的查询,支持自动生成BIT,并通过BitmapAnd, BitmapOr操作,计算并合并结果。

PostgreSQL is not provide persistent bitmap index.

But it can be used in database to combine multiple indexes.

PostgreSQL scans each needed index and prepares a bitmap in memory giving the
locations of table rows that are reported as matching that index’s conditions.

The bitmaps are then ANDed and ORed together as needed by the query.

Finally, the actual table rows are visited and returned.

bitmap 的其他应用

bitmap在阿里云RDS PG中进行了扩展,支持更多的BIT操作,用户可以通过varbit来维护自己业务数据相关的BIT索引(字段),例如用户画像系统,铁路售票系统,门禁广告系统等。

《阿里云RDS for PostgreSQL varbitx插件与实时画像应用场景介绍》

《基于 阿里云 RDS PostgreSQL 打造实时用户画像推荐系统》

《PostgreSQL 与 12306 抢火车票的思考》

《门禁广告销售系统需求剖析 与 PostgreSQL数据库实现》

另外,roaring bitmap也可以作为一种数据类型,植入到PG中。

https://github.com/zeromax007/gpdb-roaringbitmap

参考

https://gpdb.docs.pivotal.io/4390/admin_guide/ddl/ddl-index.html#topic93

http://leopard.in.ua/2015/04/13/postgresql-indexes#.WRHHH_mGOiQ

《阿里云RDS for PostgreSQL varbitx插件与实时画像应用场景介绍》

《基于 阿里云 RDS PostgreSQL 打造实时用户画像推荐系统》

https://github.com/zeromax007/gpdb-roaringbitmap

相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍如何基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
目录
相关文章
|
7月前
|
SQL 存储 安全
PHP 安全攻防 —— 从 SQL 注入到 RCE 的完整防御指南
性能不仅仅关乎用户体验,还直接影响服务器成本和能源消耗。PHP、Java和C++分别代表了三个性能层次:PHP是动态解释型脚本语言,Java是JIT编译的字节码语言,C++是原生编译的静态语言
472 2
|
10月前
|
存储 弹性计算 小程序
阿里云 2 核 2G 云服务器:价格、性能与适用场景解析
在云服务器选型中,2 核 2G 是典型的入门级配置 —— 既不会像 1 核 1G 那样受限于基础性能,又比 4 核 4G 更贴近个人和小微企业的预算,适合搭建个人博客、小型网站或运行轻量应用。下面从价格构成、性能表现、适用场景三方面,用通俗语言拆解这款配置,帮助判断是否契合需求。
|
9月前
|
弹性计算 人工智能 并行计算
阿里云服务器多少钱一年?2026年新版阿里云服务器配置与价格表解析
在云计算应用日益普及的当下,阿里云服务器凭借稳定的性能、灵活的配置选择和覆盖广泛的地域支持,成为个人开发者、中小企业及大型企业数字化转型的重要基础设施。2026年,阿里云对服务器产品线进行了全面优化,推出了涵盖轻量应用服务器、ECS云服务器、GPU服务器等多个系列的产品,各系列在配置规格、价格定位和适用场景上形成了清晰的区分,满足不同用户的多样化需求。本文基于官方公布的配置参数与价格信息,对2026年阿里云服务器的产品体系、核心配置、价格标准及适用场景进行详细解析,为用户选择合适的服务器提供参考。
785 0
|
9月前
|
人工智能 API 调度
AI API 的未来图景:从“红包营销”到智能体生态爆发
AI API正从开发者工具升级为全民基础设施:C端规模化调用(如春节AI红包)、模型闪电式更新(季度/月度迭代)、智能体API爆发(支持函数调用与多模型协作),叠加聚合平台崛起——统一接口、动态路由、弹性调度。未来竞争核心在于生态速度×场景渗透×聚合能力。
|
10月前
|
弹性计算 人工智能 并行计算
阿里云服务器ECS架构:X86计算和Arm有啥区别?GPU、裸金属和高性能计算区别
阿里云ECS提供五大架构:X86(兼容性强,适用通用场景)、ARM(低功耗高能效,搭载倚天710等)、GPU型(加速AI训练/图形渲染)、弹性裸金属(无虚拟化,物理机性能+云弹性)、HPC/SCC(专为科学计算与分布式AI优化)。按需选型,提升效率与性价比。(239字)
1051 0
|
11月前
|
并行计算 算法 Linux
毅硕HPC | 一文详解HPC环境中的MPI并行计算
MPI主要用于分布式内存系统,适合跨多个服务器节点的大规模并行任务。MPI 不仅仅是一种编程接口,它是连接算法与硬件之间的桥梁,是实现“算得更快、看得更远”的关键技术支撑。
1106 0
毅硕HPC | 一文详解HPC环境中的MPI并行计算
|
7月前
|
人工智能 运维 自然语言处理
AI 时代下的 SSH 终端演进趋势(2026 观察)
SSH终端正从传统“远程登录工具”跃升为智能运维入口:云原生驱动下,AI赋能自然语言转命令、错误实时诊断、流程自动化;并发批量操作与多协议融合成为标配。终端已进化为集理解、决策与执行于一体的AI原生运维助手。(239字)
785 8
|
存储 搜索推荐 算法
Java 大视界 -- Java 大数据在智慧文旅旅游线路规划与游客流量均衡调控中的应用实践(196)
本实践案例深入探讨了Java大数据技术在智慧文旅中的创新应用,聚焦旅游线路规划与游客流量调控难题。通过整合多源数据、构建用户画像、开发个性化推荐算法及流量预测模型,实现了旅游线路的精准推荐与流量的科学调控。在某旅游城市的落地实践中,游客满意度显著提升,景区流量分布更加均衡,充分展现了Java大数据技术在推动文旅产业智能化升级中的核心价值与广阔前景。
|
人工智能 安全 网络安全
阿里云亮相2025国家网安周 | 首次提出AI全栈安全
了解阿里云最新的AI安全产品技术
2199 1