gpt4 book ai didi

postgresql - Bitmap Heap Scan 和 Index Scan 是怎么决定的?

转载 作者:行者123 更新时间:2023-11-29 11:24:15 31 4
gpt4 key购买 nike

我正在测试不同的查询,我很好奇数据库如何决定使用位图堆扫描和索引扫描。

create index customers_email_idx on customers(email varchar_pattern_ops);

如您所见,有一个客户表(dellstore 示例),我向电子邮件列添加了一个索引。

第一个查询在这里:

select * from customers where email like 'ITQ%@dell.com'; -> query with Index Scan

解释分析查询在这里:

                                                           QUERY PLAN                                                            
---------------------------------------------------------------------------------------------------------------------------------
Index Scan using customers_email_idx on customers (cost=0.00..8.27 rows=2 width=268) (actual time=0.046..0.046 rows=0 loops=1)
Index Cond: (((email)::text ~>=~ 'ITQ'::text) AND ((email)::text ~<~ 'ITR'::text))
Filter: ((email)::text ~~ 'ITQ%@dell.com
'::text)
Total runtime: 0.113 ms

其他查询在这里:

select * from customers where email like 'IT%@dell.com'; -> query with Bitmap Heap Scan

解释分析查询在这里:

                                                          QUERY PLAN                                                          
------------------------------------------------------------------------------------------------------------------------------
Bitmap Heap Scan on customers (cost=4.54..106.77 rows=2 width=268) (actual time=0.206..0.206 rows=0 loops=1)
Filter: ((email)::text ~~ 'IT%@dell.com
'::text)
-> Bitmap Index Scan on customers_email_idx (cost=0.00..4.54 rows=29 width=0) (actual time=0.084..0.084 rows=28 loops=1)
Index Cond: (((email)::text ~>=~ 'IT'::text) AND ((email)::text ~<~ 'IU'::text))
Total runtime: 0.273 ms

你能解释一下这个例子为什么要用到Bitmap和Index Scan吗?

谢谢你..

最佳答案

你的表格总共有多少行?该决定基于索引扫描将输出的行的比例。

如果要访问的表的比例足够高,则使用位图索引扫描来确保尽可能多的磁盘访问是顺序的。相比之下,普通索引扫描对表数据进行一次一页的随机访问。 (而且如果预计访问表的比例足够高,则根本不使用索引,顺序加载整个表数据)

一个问题是表中有多少行将被访问的预测只是一个估计。但是你可以想象,'IT%' 可能比 'ITQ%' 匹配更多(记住后缀不是索引扫描的一部分,只是最终过滤器的一部分)

关于postgresql - Bitmap Heap Scan 和 Index Scan 是怎么决定的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12259573/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com