微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

postgresql – 如何确定位图堆扫描和索引扫描?

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

create index customers_email_idx on customers(email
varchar_pattern_ops);

如您所见,有一个customers表(dellstore示例),我向email列添加索引.

一个查询在这里

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%'(请记住,后缀不是索引扫描的一部分,只有最终的过滤器)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐