CREATE INDEX message_index ON message(queue_id, target_client_id, timestamp ASC, source_client_id);
查詢:
EXPLAIN ANALYZE
SELECT content
FROM message
WHERE message.queue_id = 1
AND message.source_client_id = 1
AND (message.target_client_id = -1 OR message.target_client_id = 1);
輸出:
Bitmap Heap Scan on message (cost=8.87..12.89 rows=1 width=13) (actual time=0.022..0.026 rows=50 loops=1)
Recheck Cond: (((queue_id = 1) AND (target_client_id = (-1)) AND (source_client_id = 1)) OR ((queue_id = 1) AND (target_client_id = 1) AND (source_client
_id = 1)))
-> BitmapOr (cost=8.87..8.87 rows=1 width=0) (actual time=0.017..0.017 rows=0 loops=1)
-> Bitmap Index Scan on message_index (cost=0.00..4.43 rows=1 width=0) (actual time=0.011..0.011 rows=0 loops=1)
Index Cond: ((queue_id = 1) AND (target_client_id = (-1)) AND (source_client_id = 1))
-> Bitmap Index Scan on message_index (cost=0.00..4.44 rows=1 width=0) (actual time=0.006..0.006 rows=50 loops=1)
Index Cond: ((queue_id = 1) AND (target_client_id = 1) AND (source_client_id = 1))
這又如何查詢使用與問候索引source_client_id
即最右邊一列綜合指數沒有第三列(timestamp
)正在參與查詢所有?
根據這裏的最後一個答案How important is the order of columns in indexes?這應該是無效的。我錯過了什麼?
它有效地使用謂詞'timestamp ='anything''爲第3列。 BTW'timestamp'是表列的錯誤名稱。這是一個保留字,沒有商業意義。 – 2014-10-22 09:37:16
我在過去24小時內發佈了三個問題。每個包含「timestamp」列,每次有人提到它。 SO很棒;)@a_horse_with_no_name如果你認爲合適,你可以發表你的評論作爲回答,因爲我覺得這個問題已經足夠了。 – ben 2014-10-22 14:16:29