简体   繁体   English

SQL Server包含的列是否占用双倍的空间?

[英]Do SQL Server included columns takes double space?

My question is for example I have following columns 我的问题是例如我有以下专栏

Cl1 int - primary key
Cl2 int
Cl3 nvarchar(200)
Cl4 nvarchar(max)

Now assume that I am creating a non-clustered index on Cl2 however when I query Cl2 I always retrieve Cl3 and Cl4 also. 现在假设我在Cl2上创建了一个非聚集索引,但是当我查询Cl2我总是也检索Cl3Cl4

It is logical to include Cl3 in the index as it will make retrieval operation faster, however Cl4 is nvarchar(max) and can be pretty big: this column holds crawled page source Cl3包含在索引中是合乎逻辑的,因为它将使检索操作更快,但是Cl4nvarchar(max)并且可能很大:此列包含已爬网的页面源

So my question is: it is logical to include Cl4 in the non-clustered index or not 所以我的问题是:在非聚集索引中是否包含Cl4是合乎逻辑的

Including it would make Cl4 to be stored exactly 2 times on the hard drive? 包括它会使Cl4精确地存储在硬盘上2次吗?

Thank you very much for answers 非常感谢您的回答

SQL Server 2014 SQL Server 2014

I will refer you to this answer that explains a bit more about included columns in indexes. 我将向您介绍这个答案该答案进一步说明了索引中包含的列。

In short, yes, the space used will be greater if you include the column in your index, but the performance will be better. 简而言之,是的,如果在索引中包含该列,则使用的空间将更大,但是性能会更好。 The choice is yours. 这是你的选择。

from Books online for SQL Server 2014: 从针对SQL Server 2014的联机丛书中获得:

Up to 16 columns can be combined into a single composite index key. All the columns in a composite index key must be in the same table or view. The maximum allowable size of the combined index values is 900 bytes.

Columns that are of the large object (LOB) data types ntext, text, varchar(max), nvarchar(max), varbinary(max), xml, or image cannot be specified as key columns for an index.

As you surmised, including C14 likely will have a positive impact on select performance, at the cost of disc space. 如您所料,包括C14可能会以磁盘空间为代价,对某些性能产生积极影响。 I think you also understand that it's not just disc space, but also inserts, and updates, and deletes that are impacted by this. 我想您也了解,受此影响的不仅是磁盘空间,还包括插入,更新和删除。 Whether the change is worth it or not will depend on your system. 更改是否值得取决于您的系统。 I'd encourage you to do some real performance profiling... of both options. 我鼓励您对这两个选项进行真实的性能分析。 Then make your decision based on the results. 然后根据结果做出决定。

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

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