我正在測試不同的查詢,我很好奇數據庫如何決定使用位圖堆掃描和索引掃描。如何確定位圖堆掃描和索引掃描?
在客戶端創建索引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。 –