views:

25

answers:

2

I have a table in my schema that has a unique constraint on two columns:

UNIQUE(Column1, Column2)

The SQlite documentation tells me that this creates a unique index on these columns. My question is, does that make an explicitly created index on one of the columns, say Column1, redundant?

+2  A: 

Yes to your example, no to your question.

A compound index on 2 columns would make the additional index on the first one redundant. However, the index on the second column might still be useful.

But if each of the columns is by itself unique, it's possible you don't need a compound index. You might want to look into that.

Having too many indexes is not always an obvious problem. But wasting resources, especially for redundant purposes, is always bad.

MJB
+1  A: 

Any one index containing multiple columns can also serve as an index for fewer of the same columns, provided they're all the ones at the start of the index.

Let me give you an example. An index for these columns:

a, b, c, d, e, f

Can also serve as an index for the following column combinations:

a, b, c, d, e
a, b, c, d
a, b, c
a, b
a

So for your question: The index you have can also serve as an index for Column1, but not for Column2.

Lasse V. Karlsen