If column c1 is unique, which of the following indexes would optimize the statement given below?
Select distinct (c1), c3 from foo where c1=10
create unique index foox on foo (c1) include (c3)
Correct:
create unique index foox on foo (c1) include (c3)
Explanation:
Answer option D is correct.
The optimal index has column C1 as unique, and includes column C3 using the following statement:
create unique index foox on foo (c1) include (c3)
Since the query has a predicate on column C1, that column should be part of the index. Since column C1 is unique, the index should be a unique index, to minimize any extra work required to enforce the DISTINCT clause on the query. By creating a unique index, the DISTINCT clause will not need to do a sort on that column. Therefore, the two non-unique indexes are not optimal.
Answer option C is incorrect. Since the query also returns column C2, if that is part of the index, DB2 need not access the table at all. The following statement does not enforce uniqueness on C1, rather on the combination of C1 and C3