I have a large table with a btree index on multiple keys. If a make a query by fixing the first two columns of the index and putting a unilateral bound on the third column, it results in a very very slow query even if the number of matching rows is very low. If I put a bilateral bound on the third column the query is instead fast. See code snippet below.
I would expect that postgresql should be able to find quickly a lower bound for an indexed column, but in this case it seems it is not.
Can you give an explanation of why I get this problem? How to fix it?
> select min(minute) from data_minutesample where probe_id = 19 and power = 0 and minute between 0 and 22780323;
min
----------
22780262
(1 row)
Time: 28233.498 ms
> select min(minute) from data_minutesample where probe_id = 19 and power = 0 and minute between 22780000 and 22780323;
min
----------
22780262
(1 row)
Time: 13.946 ms
> \d+ data_minutesample
Table "public.data_minutesample"
[...]
Indexes:
"data_minutesample_index_unique" UNIQUE, btree (probe_id, power, minute, proto_id, src_port, dst_port, src_addr, dst_addr)