nvarchar(max) 与 NText

发布于 2024-08-19 03:14:59 字数 296 浏览 7 评论 0原文

在 SQL Server 中使用 nvarchar(max)NText 数据类型有何优缺点?我不需要向后兼容性,因此旧版 SQL Server 版本不支持 nvarchar(max) 也没什么问题。

编辑:显然这个问题也适用于TEXTIMAGEvarchar(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

怪异←思 2024-08-26 03:14:59

优点是您可以在 nvarchar(max) 上使用 LENLEFT 等函数,而不能针对 ntext 执行此操作代码>和<代码>文本。使用 nvarchar(max) 也比使用 text 更容易,在后者中您必须使用 WRITETEXTUPDATETEXT

此外,textntext 等已被弃用 (http://msdn.microsoft.com/en-us/library/ms187993.aspx)

The advantages are that you can use functions like LEN and LEFT on nvarchar(max) and you cannot do that against ntext and text. It is also easier to work with nvarchar(max) than text where you had to use WRITETEXT and UPDATETEXT.

Also, text, ntext, etc., are being deprecated (http://msdn.microsoft.com/en-us/library/ms187993.aspx)

剑心龙吟 2024-08-26 03:14:59

ntext 将始终将其数据存储在单独的数据库页面中,而 nvarchar(max) 将尝试将数据存储在数据库记录本身中。

因此,nvarchar(max) 速度稍快一些(如果您的文本小于 8 kB)。我还注意到数据库大小增长会稍微慢一些,这也很好。

转到nvarchar(max)

ntext will always store its data in a separate database page, while nvarchar(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).

因为看清所以看轻 2024-08-26 03:14:59

VARCHAR(MAX) 足够大,可以容纳 TEXT 字段。 SQL Server 2000 的 TEXTNTEXTIMAGE 数据类型将在 SQL Server 的未来版本中弃用,SQL Server 2005 提供对数据的向后兼容性类型,但建议使用新数据类型:VARCHAR(MAX)NVARCHAR(MAX)VARBINARY(MAX)

VARCHAR(MAX) is big enough to accommodate TEXT field. TEXT, NTEXT and IMAGE 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 are VARCHAR(MAX), NVARCHAR(MAX) and VARBINARY(MAX).

遗失的美好 2024-08-26 03:14:59

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 with ntext. I'm not aware of any real disadvantages.

清风挽心 2024-08-26 03:14:59

想添加我的转换经验。我在古老的 Linq2SQL 代码中有许多 text 字段。这是为了允许索引中存在的 text在线重建

首先,我多年来一直知道转换的好处,但总是认为转换意味着一些可怕的长查询,其中 SQL Server 必须重建表并复制所有内容,从而导致我的网站瘫痪并提高我的心率。

我还担心 Linq2SQL 如果对列类型进行某种验证,可能会导致错误。

不过,很高兴地报告,ALTER 命令立即返回 - 所以它们肯定只是更改表元数据。可能会发生一些离线工作,将 <8000 个字符的数据带回到表中,但 ALTER 命令是即时的。

我运行以下命令来查找所有需要转换的列:

SELECT concat('ALTER TABLE dbo.[', table_name, '] ALTER COLUMN [', column_name, '] VARCHAR(MAX)'), table_name, column_name
FROM information_schema.columns where data_type = 'TEXT' order by table_name, column_name

SELECT concat('ALTER TABLE dbo.[', table_name, '] ALTER COLUMN [', column_name, '] NVARCHAR(MAX)'), table_name, column_name
FROM information_schema.columns where data_type = 'NTEXT' order by table_name, column_name

这给了我一个很好的查询列表,我刚刚选择了该列表并将其复制到新窗口。就像我说的——运行是即时的。

输入图像描述这里

Linq2SQL 非常古老 - 它使用一个设计器,您可以将表拖到上面。对于 EF Code First 来说,情况可能更复杂,但我还没有解决这个问题。

Wanted to add my experience with converting. I had many text fields in ancient Linq2SQL code. This was to allow text 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:

SELECT concat('ALTER TABLE dbo.[', table_name, '] ALTER COLUMN [', column_name, '] VARCHAR(MAX)'), table_name, column_name
FROM information_schema.columns where data_type = 'TEXT' order by table_name, column_name

SELECT concat('ALTER TABLE dbo.[', table_name, '] ALTER COLUMN [', column_name, '] NVARCHAR(MAX)'), table_name, column_name
FROM information_schema.columns where data_type = 'NTEXT' order by table_name, column_name

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.

enter image description here

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.

迷雾森÷林ヴ 2024-08-26 03:14:59

您显然应该使用nvarchar(max)

MSDN

You should apparently use nvarchar(max):

MSDN

所有深爱都是秘密 2024-08-26 03:14:59

Text(与 NTextImage 一起)的最大缺点是它将在 SQL Server 的未来版本中删除,如下所示: a href="http://msdn.microsoft.com/en-us/library/ms187993.aspx" rel="nofollow noreferrer">文档。当该版本的 SQL Server 发布时,这将有效地使您的架构更难升级。

The biggest disadvantage of Text (together with NText and Image) 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.

放飞的风筝 2024-08-26 03:14:59

我想补充一点,您可以使用 .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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文