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