This is a follow-up to: Do covering indexes in PostgreSQL help JOIN columns?
Consider the inverse of the schema in the other question where you filter in the joined-on table:
CREATE TABLE thing_types(
id INTEGER PRIMARY KEY
, first_lvl_type TEXT
, second_lvl_type TEXT
);
CREATE TABLE things(
id INTEGER PRIMARY KEY
, thing_type INTEGER REFERENCES thing_types(id)
, t1c1 INTEGER
);
And a query like so:
SELECT things.t1c1
FROM things
JOIN thing_types ON things.thing_type = thing_types.id
WHERE thing_types.first_lvl_type = 'Book'
AND thing_types.second_lvl_type = 'Biography';
Is it madness to have an index like:
CREATE INDEX ON thing_types(first_lvl_type, second_lvl_type, id);
which covers the primary key for use in that join? Will the index be used as a covering index to help the JOIN
in the above query? Should I change my indexing strategy to cover the primary key more often when I know the table is going to be JOIN
ed on like this?