In a large People's table which has a PersonID column as primary key (int identity) and is clustered indexed by that (PK_People), with a whole bunch of additional columns, I had the need to add a non clustered index also on the same column with only one additional included column (Name).
The reason for that would apparently be because this table is most of the time inner joined with other tables via it's primary key (PersonID) just to get the Person Name and not all the other columns. It seems that SQL Server would prefer to use this index because it's much smaller than the whole clustered index (actual entire table).
I saw that need because SQL Server was actually using another non clustered index that existed in the table for the Name column alone, to retrieve the Name for the joins and thus having to scan the whole index to find the matching PersonID for the person it was looking for. Still it was preferring this over doing a key lookup using the actual clustered index. (Maybe because of the size of it?)
Then, the moment I created an index like this:
CREATE UNIQUE NONCLUSTERED INDEX UX_People_PersonID ON dbo.People (PersonID ASC)
INCLUDE (Name);
SQL was happily using this new index for the table joins, doing now an index seek instead of index scan, using the PersonID as key to output the Name column (included in the index).
Now... to the actual question, or questions:
- Should I make this index UNIQUE as I did? (It kind of made sense to me at the time, one reason being the fact that the PersonID column would never have duplicate values anyway since it's the primary key)
- Does it help SQL Server in any way by defining it UNIQUE or should it be a simple index?
- Do I still need the clustered index to be the PersonID as well?
- Is it a problem to have two indexes on the same column like this?
- Since SQL benefits from having this non clustered index like I described for most of the case scenarios that I use, should I make the cluster index on some other column instead of PersonID?
Please let me know what your thoughts are... if I'm on the right path here or there are better approaches to solve this (duplication and uniqueness)...