I could obviously test this empirically but I've found that without loading fake data EXPLAIN
shows that Postgres just loads the full page of memory containing the table and scans. I also haven't found the answer in the docs.
My question is, if I have an index on a table such as (colA, colB)
and a query containing where colA = 'something'
will Postgres use this index even though colB
is not in the query? Assume that no index containing just colA
exists. It stands to reason that this index could be useful in the query execution, but I've had a hard time tracking the answer down definitively.
(Postgres v12.x)
Best Answer
Yes, because that composite index in your example is also known as a covering index. Specifically mentioned in the previously linked documentation here:
Basically, as long as the fields of your predicates are all sequentially part of the index definition (from left to right), then that index is covering and applicable to be used for that query.
The reverse of your example would not work though since the index does not cover that scenario. I.e.
WHERE colB = 'someValue'
is not covered by an index with the definition of(colA, colB)
since the order of the field list in the index definition literally defines the structure of the underlying B-Tree used to sort the data. So first the data is sorted oncolA
and thencolB
, making it not possible to directly search oncolB
(aka directly seek oncolB
, rather an index scan could still be performed to indirectly search oncolB
) without a predicate to search oncolA
first.