Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

Lets assume we have a table as follows:

PK -- nonclustered primary key
Col1 -- unique clustered
Col2
Col3

Is this index:

nonclustered index on (Col2, Col3)

any different from this index:

nonclustered index on (Col2, Col3) include (PK)

What if my PK is clustered instead of nonclustered? Now does it make/not make a difference?

Edit to add:

I suppose this is a round-about way of asking: Is the nonclustered index's pointer back to the table based on the clustered index or the primary key? I assume the clustered index RID is what is used.

share|improve this question

migrated from stackoverflow.com Feb 13 '12 at 22:05

This question came from our site for professional and enthusiast programmers.

    
For your table the Col1 column will be added to each index (so a lookup can get back to the actual row, and why small clustered keys are better than wide ones), so the include (PK) will make a difference, because it is not the clustered index. –  KM. Aug 5 '11 at 14:30
    
This is why I asked - Leons mentions its always included if it's clustered, you say always. I'm not sure which is correct! –  Derek Kromm Aug 5 '11 at 14:35
1  
By reading this msdn.microsoft.com/en-us/library/ms177484.aspx I would say that the nonclustered index has a pointer back to the clustered index. The primary key is not stored in the nonclustered index unless the PK and the clustered index is the same. –  Mikael Eriksson Aug 5 '11 at 14:42
    
The clustered index is always included in every non-clustered index. Not the primary key (unless that is the clustered index). If it was the primary key, what use would a non-clustered index on the primary key be? And how would you then find the non-indexed columns using it? –  Damien_The_Unbeliever Aug 5 '11 at 14:43
    
@Derek Kromm, I've edited my answer to reflect your given table –  KM. Aug 5 '11 at 14:44
show 1 more comment

3 Answers 3

up vote 6 down vote accepted

The clustered index key is included in every non-clustered index as the row identifier.

SQL Server just ignores an INCLUDE of the clustered index key.

If your PK index is NONCLUSTERED then it will not be included in every row of all other NC indexes.

You can test this yourself by making two identical indexes, one with INCLUDE(ClusterKey) and one without, and compare the sizes. They will be identical, even on hundreds of millions of rows.

share|improve this answer
add comment

If the PK is a clustered PK, then the PK will be included in every index that you create, except for a unique nonclustered index. If you need the PK column on the unique nonclustered index, then you will still need to include the column.

share|improve this answer
3  
What do you mean by "except for a unique index"? the PK will still be included in the index right? It just won't participate in the uniqueness constraint...? –  Tao Aug 5 '11 at 14:36
    
@Tao - My answer was not clear - I should have said "Except for a unique nonclustered index". A unique nonclustered index will not contain the clustered key, resulting in an additional lookup. This can be verified via the explain plan. I will edit my post to indicate that this applies only to this case. –  Leons Aug 5 '11 at 15:41
    
OK, after correction of @KM's answer I regret my use of the term "PK" - I meant clustered index key, which in OP's case is an entirely different column. I still don't understand what you mean by the PK not being included in a unique nonclustered index though: it definitely WILL be included, if it is a clustered PK! –  Tao Aug 5 '11 at 15:58
add comment

Imagine if each non-clustered index contained the primary key, as you suppose:

This would, based on your table, presumably include the non-clustered index that's implementing your primary key. Imagine that you're performing a query against the table, and the primary key index is used. There's no way, having used this index, to find the remaining data for the table. So that couldn't possibly be the way things work.

Or another hypothesis:

Every non-clustered index that isn't the primary key index stores the primary key. If the primary key index is non-clustered, then it contains some magical other value (e.g. the clustered key). But that then means, than any query that uses a non-clustered, non-primary key index must now perform twice as many index operations - once against it's own index, and a second one against the primary key index. And we'd also need two separate implementations of non-clustered indexes.

Since neither of these are a good fit, it's hopefully obvious why non-clustered indexes store the clustered index key, rather than anything else.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.