简体   繁体   English

SQL Server中的复合聚簇索引

[英]Composite Clustered Index in SQL Server

I have a table with a IDENTITY Column as Primary Key (a classic ID column). 我有一个表IDENTITY列作为主键(经典ID列)。

SQL Server create automatically a Clustered Index for that Primary Key. SQL Server自动为该主键创建聚簇索引。

My question is: 我的问题是:

  • Can I have a only single CLUSTERED INDEX composite with more columns? 我可以只有一个具有更多列的CLUSTERED INDEX复合材料吗?

If yes, how can I drop the default clustered index and recreate a new one with this attributes. 如果是,我如何删除默认聚簇索引并重新创建具有此属性的新索引。

Thanks for your support 感谢您的支持

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 尽可能小--4字节的INT是完美的
  • 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 唯一的 - 否则,SQL Server必须使用人工的4字节值“统一”您的条目
  • optimal would be: ever increasing 最佳的将是:不断增加

INT IDENTITY is perfect as a clustered index - I would advise you keep it that way. INT IDENTITY非常适合作为聚簇索引 - 我建议你保持这种方式。

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. 聚集索引列(或列集)也会添加到同一个表上每个非聚簇索引的每个条目中 - 因此,如果使聚簇索引大,20,50个字节或更多,则会开始浪费大量空间 - 在磁盘上和服务器的内存中,这通常会降低系统性能。

Read all about clustered indices and what they should be to be good clustered indices here: 阅读所有关于聚集索引的内容以及它们应该是什么样的好聚簇索引:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM