简体   繁体   English

nvarchar(50)与nvarchar(max)的含义

[英]Implications of nvarchar (50) vs nvarchar (max)

What are the general storage and performance differences between the below two tables if their only difference is nvarchar(50) vs. nvarchar(max) and the strings in each field range from 1 to 50 characters? 如果下面两个表的唯一区别是nvarchar(50)nvarchar(max)并且每个字段中的字符串范围是1到50个字符,那么下面两个表之间的一般存储和性能差异是什么? This is in SQL Server 2005. 这是在SQL Server 2005中。

TABLE 1 表格1

firstname nvarchar (50)
lastname nvarchar (50)
username nvarchar (50)

TABLE 2 表2

firstname nvarchar (max)
lastname nvarchar (max)
username nvarchar (max)

If you are guaranteed to have strings between 1 and 50 characters, then the same query run across strings of up-to-length X will run faster using varchar(X) vs. varchar(MAX). 如果您保证有1到50个字符之间的字符串,那么使用varchar(X)与varchar(MAX)运行的相同查询将在更长的X字符串中运行。 Additionally, you can't create an index on a varchar(MAX) field. 此外,您无法在varchar(MAX)字段上创建索引。

Once your rows have values above 8000 characters in length, then there are additional performance considerations to contend with (the rows are basically treated as TEXT instead of varchar(n)). 一旦您的行的长度超过8000个字符,那么还有其他性能考虑因素(这些行基本上被视为TEXT而不是varchar(n))。 Though this isn't terribly relevant as a comparison since there is no varchar(N) option for strings of length over 8000. 虽然这与比较并不十分相关,因为对于长度超过8000的字符串,没有varchar(N)选项。

First and foremost, you won't be able to create indexes on the (max) length columns. 首先,您将无法在(最大)长度列上创建索引。 So if you are going to store searchable data, ie., if these columns are going to be part of the WHERE predicate, you may not be able to improve the query performance. 因此,如果要存储可搜索的数据,即,如果这些列将成为WHERE谓词的一部分,则可能无法提高查询性能。 You may need to consider FullText Search and indexes on these columns in such a case. 在这种情况下,您可能需要考虑FullText Search和这些列的索引。

nvarchar max is for columns up to 2GB. nvarchar max适用于最大2GB的列。 So essentially it takes up more resources. 所以基本上它占用了更多的资源。 You are better off using the nvarchar(50) if you know you aren't going to need that much space. 如果您知道不需要那么多空间,最好使用nvarchar(50)。 each character is about 2 bytes so with 2 GB thats 1 billion characters... 每个字符大约2个字节,所以2 GB,10亿个字符......

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

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