sql-server - SQL性能: Is there any performance hit using NVarchar(MAX) instead of NVarChar(200)

标签 sql-server performance indexing nvarchar

我想知道定义 nvarchar(max) 类型的列而不是为其指定(较小的)最大大小是否有任何缺点。

我在某处读到,如果列值超过 4?KB,则剩余数据将添加到“溢出”区域,这是可以的。

我正在创建一个表格,其中大多数情况下文本都是几行,但我想知道设置下限然后添加验证以避免打破该限制是否有任何优势。

使用 nvarchar(max) 列创建索引是否有任何限制,或者是否需要添加大小限制限制?

谢谢!

最佳答案

严格来说,MAX 类型总是比非 MAX 类型慢一点,请参阅 Performance comparison of varchar(max) vs. varchar(N) 。但这种差异在实践中永远不可见,它只会成为 IO 驱动的整体性能中的噪音。

您主要关心的不应该是 MAX 与非 MAX 的性能。您应该关心这个问题该列可能必须存储超过 8000 个字节吗?如果答案是肯定的,即使是一个非常非常不可能的"is",那么答案是显而易见:使用 MAX 类型,稍后将此列转换为 MAX 类型的痛苦不值得非 MAX 类型的微小性能优势。

其他问题(对该列建立索引的可能性、具有 MAX 列的表无法进行在线索引操作)已由 Denis 的回答解决。

顺便说一句,有关超过 4KB 的列在溢出区域中剩余数据的信息是错误的。正确信息在 Table and Index Organization :

ROW_OVERFLOW_DATA Allocation Unit

For every partition used by a table (heap or clustered table), index, or indexed view, there is one ROW_OVERFLOW_DATA allocation unit. This allocation unit contains zero (0) pages until a data row with variable length columns (varchar, nvarchar, varbinary, or sql_variant) in the IN_ROW_DATA allocation unit exceeds the 8 KB row size limit. When the size limitation is reached, SQL Server moves the column with the largest width from that row to a page in the ROW_OVERFLOW_DATA allocation unit. A 24-byte pointer to this off-row data is maintained on the original page.

因此,不是超过 4KB 的列,而是不适合页面上可用空间的行,并且不是“剩余”,而是整个列。

关于sql-server - SQL性能: Is there any performance hit using NVarchar(MAX) instead of NVarChar(200),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4378795/

相关文章:

sql - 谁在 SQL Server 代理中禁用了作业

sql - 数据库中的名字变化

javascript - 结合 Angular 模板以减少请求的策略?

用于开发和实时网站的 Git

sql - 使用一个 select 语句返回特定范围之间的行

sql - 无法对表或索引 View 使用 CONTAINS 或 FREETEXT 谓词,因为它不是全文索引

performance - 在 ZeroMQ 中, "inproc + PAIR"比 "inproc"快吗?

MongoDB 可选唯一索引

c# - C#中数组索引的类型?

indexing - Terraform 列表元素超出范围?