加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-13 16:02:10 所属栏目:百科 来源:网络整理
导读:我正在测试不同的查询,我很好奇数据库如何决定使用位图堆扫描和索引扫描. create index customers_email_idx on customers(email varchar_pattern_ops); 如您所见,有一个customers表(dellstore示例),我向email列添加索引. 第一个查询在这里: select * from
我正在测试不同的查询,我很好奇数据库如何决定使用位图堆扫描和索引扫描.

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读