我正在测试不同的查询,我很好奇数据库如何决定使用位图堆扫描和索引扫描。如何确定位图堆扫描和索引扫描?
在客户端创建索引customers_email_idx(电子邮件 varchar_pattern_ops);
正如你可以看到有一个客户表(dellstore的例子),我添加一个索引到电子邮件列。
首先查询是在这里:
SELECT * FROM客户那里电子邮件像 'ITQ%@dell.com'; - >查询 与索引扫描
解释分析查询是在这里:
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客户那里电子邮件像“IT%@ dell.com“; - >查询 与位图堆扫描
解释分析查询是在这里:
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和索引扫描用在这里?
谢谢..
查看@ araqnid的答案和这个:http ://wiki.postgresql.org/images/4/45/Explaining_EXPLAIN.pdf 寻找'索引扫描'。关键可能是'优化器可以选择它[位图扫描]用于低选择性的任何可索引扫描“ – dezso
我认为这个问题很适合dba.SE。 –