+----------+--------------+-------------------------+
| ticketid | ticketpostid | date |
+----------+--------------+-------------------------+
| 1387935 | 3147808 | 2012-09-17 13:33:01 |
| 1387935 | 3147812 | 2012-09-17 13:33:41 |
| 1387938 | 3147818 | 2012-09-17 13:35:01 |
| 1387938 | 3148068 | 2012-09-17 13:37:01 |
| 1387938 | 3148323 | 2012-09-17 14:47:01 |
| 1387939 | 3147820 | 2012-09-17 13:36:01 |
| 1387939 | 3147834 | 2012-09-17 13:36:25 |
| 1387939 | 3147851 | 2012-09-17 13:41:01 |
| 1387939 | 3147968 | 2012-09-17 13:59:06 …Run Code Online (Sandbox Code Playgroud) 我正在测试不同的查询,我很好奇数据库如何决定使用位图堆扫描和索引扫描.
在客户上创建索引customers_email_idx(电子邮件varchar_pattern_ops);
如您所见,有一个customers表(dellstore示例),我向email列添加索引.
第一个查询在这里:
从客户处选择*,其中包含'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
Run Code Online (Sandbox Code Playgroud)
其他查询在这里:
从客户处选择*,其中包含'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 …Run Code Online (Sandbox Code Playgroud)