简体   繁体   English

对nvarchar(4000)的性能有影响吗?

[英]Performance implication on nvarchar(4000)?

I have a column that is declared as nvarchar(4000) and 4000 is SQL Server's limit on the length of nvarchar. 我有一列被声明为nvarchar(4000),而4000是SQL Server对nvarchar长度的限制。 it would not be used as a key to sort rows. 它不会用作对行进行排序的键。

Are there any implication that i should be aware of before setting the length of the nvarchar field to 4000? 在将nvarchar字段的长度设置为4000之前,我有什么需要注意的暗示?

Update 更新资料

I'm storing an XML, an XML Serialized Object to be exact. 我存储的是XML,确切地说是XML序列化对象。 I know this isn't favorable but we will most likely never need to perform queries on it and this implementation dramatically decreases development time for certain features that we plan on extending. 我知道这是不利的,但是我们很可能永远不需要对其进行查询,并且这种实现大大减少了我们计划扩展的某些功能的开发时间。 I expect the XML data to be 1500 characters long on average but there can be those exceptions where it can be longer than 4000. Could it be longer than 4000 characters? 我期望XML数据的平均长度为1500个字符,但是在某些情况下可能会超过4000个字符。可能会超过4000个字符吗? It could be but in very rare occasions, if it ever happens. 如果曾经发生的话,可能是这样,但在极少数情况下。 Is this application mission critical? 这个应用程序任务至关重要吗? Nope, not at all. 不,一点也不。

SQL Server has three types of storage: in-row, LOB and Row-Overflow, see Table and Index Organization . SQL Server具有三种类型的存储:行,LOB和行溢出,请参阅表和索引组织 The in-row storage is fastest to access. 行内存储是最快访问的。 LOB and Row-Overflow are similar to each other, both slightly slower than in-row. LOB和行溢出彼此相似,但都比行内慢。

If you have a column of NVARCHAR(4000) it will be stored in row if possible, if not it will be stored in the row-overflow storage. 如果您有一列NVARCHAR(4000),则将在可能的情况下将其存储在行中,否则将存储在行溢出存储中。 Having such a column does not necesarily indicate future performance problems, but it begs the question: why nvarchar(4000)? 拥有这样的列并不一定表示将来的性能问题,但这引出了一个问题:为什么要使用nvarchar(4000)? Is your data likely to be always near 4000 characters long? 您的数据可能总是接近4000个字符吗? Can it be 4001, how will your applicaiton handle it in this case? 可以是4001,在这种情况下您的应用程序将如何处理? Why not nvarchar(max)? 为什么不使用nvarchar(max)? Have you measured performance and found that nvarchar(max) is too slow for you? 您是否测量过性能并发现nvarchar(max)对您来说太慢了?

My recommendation would be to either use a small nvarchar length, appropiate for the real data, or nvarchar(max) if is expected to be large. 我的建议是使用较小的nvarchar长度(适合实际数据),或者使用nvarchar(max)(如果预期较大)。 nvarchar(4000) smells like unjustified and not tested premature optimisation. nvarchar(4000)闻起来像不合理且未经测试的过早优化。

Update 更新资料

For XML, use the XML data type . 对于XML,请使用XML数据类型 It has many advantages over varchar or nvarchar, like the fact that it supports XML indexes , it supports XML methods and can actually validate the XML for a compliance to a specific schema or at least for well-formed XML compliance. 与varchar或nvarchar相比,它具有许多优点,例如它支持XML索引 ,支持XML方法 ,并且实际上可以验证XML是否符合特定模式或至少符合格式良好的XML。

XML will be stored in the LOB storage, outside the row. XML将存储在该行外部的LOB存储中。

Even if the data is not XML, I would still recommend LOB storage (nvarchar(max)) for something of a length of 1500. There is a cost associated with retrieving the LOB stored data, but the cost is more than compensated by macking the table narrower . 即使数据不是XML,我仍然建议LOB存储(nvarchar(max))的长度为1500。检索LOB存储的数据会带来一定的成本,但是通过对Mac OS X进行存储,可以弥补成本的不足。表较窄 The width of a table row is a primary factor of performance, because wider tables fit less rows per page, so any operation that has to scan a range of rows or the entire table needs to fetch more pages into memory, and this shows up in the query cost (is actualy the driving factor of the overall cost). 表格行的宽度是性能的主要因素,因为更宽的表格每页可容纳更少的行,因此任何必须扫描一定范围的行或整个表格的操作都需要将更多的页读取到内存中,查询费用(实际上是总费用驱动因素)。 A LOB stored column only expands the size of the row with the width of a 'page id', which is 8 bytes if I remember correctly, so you can get much better density of rows per page, hence faster queries. LOB存储的列仅使用'页面ID'的宽度扩展行的大小,如果我没有记错的话,则为8字节,因此您可以获得更好的每页行密度,因此查询速度更快。

Are you sure that you'll actually need as many as 4000 characters? 您确定实际上需要多达4000个字符吗? If 1000 is the practical upper limit, why not set it to that? 如果实际上限是1000,为什么不将其设置为上限? Conversely, if you're likely to get more than 4000 bytes, you'll want to look at nvarchar(max). 相反,如果您可能获得超过4000个字节,则需要查看nvarchar(max)。

I like to "encourage" users not use storage space too freely. 我喜欢“鼓励”用户不要过于随意地使用存储空间。 The more space required to store a given row, the less space you can store per page, which potentially results in more disk I/O when the table is read or written to. 存储给定行所需的空间越多,每页可以存储的空间就越少,这可能会导致在读取或写入表时出现更多的磁盘I / O。 Even though only as many bytes of data are stored as are necessary (ie not the full 4000 per row), whenever you get a bit more than 2000 characters of nvarchar data, you'll only have one row per page, and performance can really suffer. 即使只存储了必要的字节数据(即,每行不存储完整的4000个字节),但是每当您获得多于2000个字符的nvarchar数据时,每页将只有一行,并且性能确实可以遭受。

This of course assumes you need to store unicode (double-byte) data, and that you only have one such column per row. 当然,这假设您需要存储unicode(双字节)数据,并且每行只有一个这样的列。 If you don't, drop down to varchar. 如果不这样做,请降到varchar。

Do you certainly need nvarchar or can you go with varchar? 您肯定需要nvarchar还是可以使用varchar? The limitation applies mainly to sql server 2k. 该限制主要适用于sql server 2k。 Are you using 2k5 / 2k8 ? 您正在使用2k5 / 2k8吗?

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

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