I have a table defined like this:
CREATE TABLE sessions (
session_id BIGSERIAL PRIMARY KEY,
session_start TIMESTAMP NOT NULL,
session_end TIMESTAMP NOT NULL,
site_id BIGINT NOT NULL,
photo_id BIGINT NOT NULL,
uuid TEXT NOT NULL
)
CREATE INDEX sessions_lookup ON sessions (
site_id, photo_id, uuid, session_start, session_end
)
And I need to run queries of this type against it:
SELECT session_id
FROM sessions
WHERE site_id = %s
AND photo_id = %s
AND uuid = %s
AND %s <@ tsrange(
session_start - interval '3 hours',
session_end + interval '3 hours'
)
There's different variations of the range query part (check seperately for both session_start and session_end, use OVERLAPS, etc. but as far as I have tested they all do the same thing).
Using EXPLAIN ANALYZE I get the following results (using actual values instead of placeholders):
Index Scan using sessions_lookup on sessions (cost=0.00..8.30 rows=1 width=8) (actual time=0.062..0.063 rows=1 loops=1)
Index Cond: ((site_id = 10113150) AND (photo_id = 10240980) AND (uuid = '042d6f26-e298-0140-a4cc-7bfd0f9ccd27'::text))
Filter: (((session_start - '03:00:00'::interval) <= '2014-09-05 09:45:38'::timestamp without time zone) AND ((session_end + '03:00:00'::interval) >= '2014-09-05 09:45:38'::timestamp without time zone))
Total runtime: 1.384 ms
(4 rows)
Subsequent runs give ~0.080ms query runtime.
Ideally, I would like to use the index as part of the timestamp lookup, but it seems to be completely ignored (same result with or without it in the index). Do I need to alter the order of fields or am I doing something wrong (do I need a different type of index)?
This is tested on a table with 45k records, but if need be I can create a bigger sample set.
<@is btree-indexable, and even if there was a suitable GiST index here I don't think a bitmap index scan is (a) possible in this case or (b) likely to help performance wise anyway. Though it's interesting that<@is being decomposed into range range predicates that are potentially b-tree indexable; I wasn't aware that was actually possible.