question

Tatyana avatar image
Tatyana asked

Once again, Why Primary Key should not be included in Non Clustered index?

While checking missing indexes statistics I found there recommendations for high impact new indexes that have primary key (that are also clustered key in their tables) columns among "included" columns.

E.g., image below, szTransactionId field is a primary key and a clustered index column in the table, and is in the "included" columns for several missing indexes:

Similar, itemId + version columns make a clustered key on another table, and, again, are in the "included" list of a missing index:

I just read in Navarez's "Inside SQL query optimizer..." (in addition to what I already knew on the subject): "When a non-clustered index is created on a table with a clustered index, each non-clustered index row also includes the table clustered key". So why do the missing indexes still recommend to include clustered keys?

primary-keyclustered-indexnonclustered-indexmissing-indexincluded-columns
included.jpg (21.3 KiB)
included-item.jpg (13.2 KiB)
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Jeff Moden avatar image
Jeff Moden answered

The missing index suggestions are just that way. They must have been designed after what I do because there's no guarantee that someone isn't going to move, add to, or even delete the clustered index. It also means that you don't have to lookup the columns in the clustered index when you're troubleshooting, etc.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.