java nvarchar max,SQL性能:使用NVarchar(MAX)而不是NVarChar(200)是否有任何性能损失...

文章讨论了在SQL中使用NVARCHAR(MAX)类型的优缺点。尽管MAX类型在性能上略逊于非MAX类型,但在实际应用中影响微乎其微,主要关注点应在于是否可能需要存储超过8000字节的数据。如果存在这种可能性,即使概率极小,也应选择MAX类型,避免后期转换带来的不便。同时提到了超过8KB的数据会存储在溢出区域,而非特定列超过4KB。
摘要由CSDN通过智能技术生成

I am wondering if there is any disadvantage on defining a column of type nvarchar(max) instead of giving it a (smaller) maximum size.

I read somewhere that if the column value has more than 4?KB the remaining data will be added to an "overflow" area, which is ok.

I'm creating a table where most of the time the text will be of a few lines, but I was wondering if there's any advantage in setting a lower limit and then adding a validation to avoid breaking that limit.

Is there any restriction on the creation of indexes with nvarchar(max) column, or anything that pays for having to add the restriction on the size limit?

Thanks!

解决方案

Strictly speaking the MAX types will always be a bit slower than the non-MAX types, see Performance comparison of varchar(max) vs. varchar(N). But this difference is never visible in practice, where it just becomes noise in the overall performance driven by IO.

Your main concern should not be performance of MAX vs. non-MAX. You should be concerned with the question it will be possible that this column will have to store more than 8000 bytes? If the answer is yes, even by if is a very very unlikely yes, then the answer is obvious: use a MAX type, the pain to convert this column later to a MAX type is not worth the minor performance benefit of non-MAX types.

Other concerns (possibility to index that column, unavailability of ONLINE index operations for tables with MAX columns) were already addressed by Denis' answer.

BTW, the information about the columns over 4KB having remaining data in an overflow area is wrong. The correct information is in 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.

So is not columns over 4KB, is rows that don't fit in the free space on the page, and is not the 'remaining', is the entire column.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值