views:

50

answers:

1

One of my co workers is under the impression that when adding an index to a table in SQL Server 2008 that the PK's index is added to that index as well. Therefore if you are using a wider primary key then that key will also be included in the new index vastly increasing the disk space used above and beyond the penalty already paid for the index on the PK. I hadn't heard that before and my searching so far is coming up empty.

Hopefully someone here can point me at relevant docs to confirm or deny this. Please?

+7  A: 

Your co worker is conflating "Primary Key" with "clustered index key" (possibly because by default a PK created on a heap without specifying the nonclustered keyword will become the clustered index of the table).

It is true that on a table with a clustered index the value of the clustered index key will be added in as included column(s) to all non clustered indexes to act as the row locator. (though the column(s) won't be added in twice if they are already part of the non clustered index definition).

The ideal clustered index key is

  • unique (To act as a row locator it must be unique - SQL Server will add a uniquifier in if it is not)
  • narrow (As it is reproduced in all non clustered indexes)
  • static (Avoid having to update the value in multiple different places)
  • ever-increasing
Martin Smith
Yeah busted the SQL Server Internals book off the shelf. RTFM always seems to help I just never do it. Anyway this was all part of the GUID vs INT as primary/clustered key. FLAME ON!
Grummle