nvarchar(max) 与 NText
在 SQL Server 中使用 nvarchar(max)
与 NText
数据类型有何优缺点?我不需要向后兼容性,因此旧版 SQL Server 版本不支持 nvarchar(max)
也没什么问题。
编辑:显然这个问题也适用于TEXT
和IMAGE
与varchar(max)
和varbinary (max)
,供稍后搜索这些数据类型的人使用。
What are the advantages and disadvantages of using the nvarchar(max)
vs. NText
data types in SQL Server? I don't need backward compatibility, so it is fine that nvarchar(max)
isn't supported in older SQL Server releases.
Edit: Apparently the question also applies to TEXT
and IMAGE
vs. varchar(max)
and varbinary(max)
, for those searching for those data-types later.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
优点是您可以在
nvarchar(max)
上使用LEN
和LEFT
等函数,而不能针对ntext
执行此操作代码>和<代码>文本。使用nvarchar(max)
也比使用text
更容易,在后者中您必须使用WRITETEXT
和UPDATETEXT
。此外,
text
、ntext
等已被弃用 (http://msdn.microsoft.com/en-us/library/ms187993.aspx)The advantages are that you can use functions like
LEN
andLEFT
onnvarchar(max)
and you cannot do that againstntext
andtext
. It is also easier to work withnvarchar(max)
thantext
where you had to useWRITETEXT
andUPDATETEXT
.Also,
text
,ntext
, etc., are being deprecated (http://msdn.microsoft.com/en-us/library/ms187993.aspx)ntext
将始终将其数据存储在单独的数据库页面中,而nvarchar(max)
将尝试将数据存储在数据库记录本身中。因此,
nvarchar(max)
速度稍快一些(如果您的文本小于 8 kB)。我还注意到数据库大小增长会稍微慢一些,这也很好。转到
nvarchar(max)
。ntext
will always store its data in a separate database page, whilenvarchar(max)
will try to store the data within the database record itself.So
nvarchar(max)
is somewhat faster (if you have text that is smaller as 8 kB). I also noticed that the database size will grow slightly slower, this is also good.Go
nvarchar(max)
.VARCHAR(MAX)
足够大,可以容纳TEXT
字段。 SQL Server 2000 的TEXT
、NTEXT
和IMAGE
数据类型将在 SQL Server 的未来版本中弃用,SQL Server 2005 提供对数据的向后兼容性类型,但建议使用新数据类型:VARCHAR(MAX)
、NVARCHAR(MAX)
和VARBINARY(MAX)
。VARCHAR(MAX)
is big enough to accommodateTEXT
field.TEXT
,NTEXT
andIMAGE
data types of SQL Server 2000 will be deprecated in future version of SQL Server, SQL Server 2005 provides backward compatibility to data types but it is recommended to use new data types which areVARCHAR(MAX)
,NVARCHAR(MAX)
andVARBINARY(MAX)
.nvarchar(max)
是您想要使用的。最大的优点是您可以在该数据类型上使用所有 T-SQL 字符串函数。ntext
无法做到这一点。我不知道有什么真正的缺点。nvarchar(max)
is what you want to be using. The biggest advantage is that you can use all the T-SQL string functions on this data type. This is not possible withntext
. I'm not aware of any real disadvantages.想添加我的转换经验。我在古老的 Linq2SQL 代码中有许多
text
字段。这是为了允许索引中存在的text
列在线重建。首先,我多年来一直知道转换的好处,但总是认为转换意味着一些可怕的长查询,其中 SQL Server 必须重建表并复制所有内容,从而导致我的网站瘫痪并提高我的心率。
我还担心 Linq2SQL 如果对列类型进行某种验证,可能会导致错误。
不过,很高兴地报告,ALTER 命令立即返回 - 所以它们肯定只是更改表元数据。可能会发生一些离线工作,将 <8000 个字符的数据带回到表中,但 ALTER 命令是即时的。
我运行以下命令来查找所有需要转换的列:
这给了我一个很好的查询列表,我刚刚选择了该列表并将其复制到新窗口。就像我说的——运行是即时的。
Linq2SQL 非常古老 - 它使用一个设计器,您可以将表拖到上面。对于 EF Code First 来说,情况可能更复杂,但我还没有解决这个问题。
Wanted to add my experience with converting. I had many
text
fields in ancient Linq2SQL code. This was to allowtext
columns present in indexes to be rebuilt ONLINE.First I've known about the benefits for years, but always assumed that converting would mean some scary long queries where SQL Server would have to rebuild the table and copy everything over, bringing down my websites and raising my heartrate.
I was also concerned that the Linq2SQL could cause errors if it was doing some kind of verification of the column type.
Happy to report though, that the ALTER commands returned INSTANTLY - so they are definitely only changing table metadata. There may be some offline work happening to bring <8000 character data back to be in-table, but the ALTER command was instant.
I ran the following to find all columns needing conversion:
This gave me a nice list of queries, which I just selected and copied to a new window. Like I said - running this was instant.
Linq2SQL is pretty ancient - it uses a designer that you drag tables onto. The situation may be more complex for EF Code first but I haven't tackled that yet.
您显然应该使用
nvarchar(max)
:MSDN
You should apparently use
nvarchar(max)
:MSDN
Text
(与NText
和Image
一起)的最大缺点是它将在 SQL Server 的未来版本中删除,如下所示: a href="http://msdn.microsoft.com/en-us/library/ms187993.aspx" rel="nofollow noreferrer">文档。当该版本的 SQL Server 发布时,这将有效地使您的架构更难升级。The biggest disadvantage of
Text
(together withNText
andImage
) is that it will be removed in a future version of SQL Server, as by the documentation. That will effectively make your schema harder to upgrade when that version of SQL Server will be released.我想补充一点,您可以使用 .WRITE 子句进行部分或完整更新和高性能附加到
varchar(max)/nvarchar(max)
数据类型。在这里您可以找到使用的完整示例
.WRITE
子句。I want to add that you can use the .WRITE clause for partial or full updates and high performance appends to
varchar(max)/nvarchar(max)
data types.Here you can found full example of using
.WRITE
clause.