I have the following structure.
CREATE TABLE join_table (
id integer NOT NULL,
col_a integer NOT NULL,
col_b integer NOT NULL
)
CREATE INDEX index_on_col_a ON join_table USING btree (col_a);
CREATE INDEX index_on_col_b ON join_table USING btree (col_b);
CREATE UNIQUE INDEX index_on_col_a_and_col_b ON join_table USING btree (col_a, col_b);
There are also foreign keys on col_a and col_b.
Clearly index_on_col_a
is no longer needed,
but is there a cost or benefit to keeping or deleting it?
My guess is;
col_a
may be faster if I keep itDuplicate indexes are those that exactly match the Key and Included columns. That's easy. Possible duplicate indexes are those that very closely match Key/Included columns.
Fortunately, PostgreSQL has the ability to combine multiple indexes (including multiple uses of the same index) to handle cases that cannot be implemented by single index scans. The system can form AND and OR conditions across several index scans.
Indexes should not be used on tables containing few records. Tables that have frequent, large batch updates or insert operations. Indexes should not be used on columns that contain a high number of NULL values.
Indexes can have up to 32 columns, including INCLUDE columns. (This limit can be altered when building PostgreSQL.) Only B-tree currently supports unique indexes. An operator class with optional parameters can be specified for each column of an index.
You can drop the index on col_a
. PostgreSQL is able to use the combined index if you query on col_a
and is also able to use the index if you query on col_a
and col_b
. These query types can use the combined index:
WHERE col_a = 'val'
WHERE col_a = 'val' AND col_b = 'val'
The combined index cannot be used to query only col_b
or an OR
junction of col_a
and col_b
. So the additional index over col_b
can make sense if you frequently have queries querying only col_b
.
Edit: So: you don't have an advantage creating index_on_col_a
, but you have a slower write speed. Drop it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With