Composite Clustered Index in SQL Server

点点圈 提交于 2019-12-20 19:56:07

问题


I have a table with a IDENTITY Column as Primary Key (a classic ID column).

SQL Server create automatically a Clustered Index for that Primary Key.

My question is:

  • Can I have a only single CLUSTERED INDEX composite with more columns?

If yes, how can I drop the default clustered index and recreate a new one with this attributes.

Thanks for your support


回答1:


Yes, you can only have a single clustered index per table - the data is physically arranged by that index, so you cannot have more than one.

I would however not advise to use a composite clustered index. Why? Because the clustered index should always be:

  • as small as possible - INT with 4 byte is perfect
  • stable - never change, so you don't have rippling updates through all your indices
  • unique - otherwise, SQL Server will have to "uniquify" your entries with artifical 4-byte values
  • optimal would be: ever increasing

INT IDENTITY is perfect as a clustered index - I would advise you keep it that way.

The clustered index column (or set of columns) is also added to each and every entry of each and every nonclustered index on that same table - so if you make your clustered index large, 20, 50 bytes or more, you begin to be wasting a lot of space - on disk and in your server's memory, which generally degrades your system performance.

Read all about clustered indices and what they should be to be good clustered indices here:

  • GUIDs as PRIMARY KEYs and/or the clustering key
  • The Clustered Index Debate Continues...
  • Ever-increasing clustering key - the Clustered Index Debate..........again!


来源:https://stackoverflow.com/questions/3202086/composite-clustered-index-in-sql-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!