简体   繁体   English

在什么时候使用文本字段比使用SQL Server中的nvarchar字段更有效?

[英]At what point does it become more efficient to use a text field than an nvarchar field in SQL Server?

How long does an nvarchar field need to be before it is better to use a text field in SQL Server? 在SQL Server中使用文本字段更好之前,nvarchar字段需要保留多长时间? What are the general indications for using one or the other for textual content that may or may not be queried? 使用一种或另一种用于可能被查询或可能不会被查询的文本内容的一般指示是什么?

From what I understand, the TEXT datatype should never be used in SQL 2005+. 据我了解,在SQL 2005+中绝对不应使用TEXT数据类型。 You should start using VARCHAR(MAX) instead. 您应该改为使用VARCHAR(MAX)

See this question about VARCHAR(MAX) vs. TEXT . 这个问题有关VARCHAR(MAX)TEXT

UPDATE (per comment): 更新 (每条评论):

This blog does a good job at explaining the advantages. 该博客在解释优势方面做得很好。 Taken from it: 取自:

But the pain from using the type text comes in when trying to query against it. 但是,在尝试对文本进行查询时会遇到使用类型文本带来的痛苦。 For example grouping by a text type is not possible. 例如,无法按文本类型分组。

Another downside to using text types is increased disk IO due to the fact each record now points to a blob (or file). 使用文本类型的另一个缺点是磁盘IO增加,原因是每个记录现在都指向一个Blob(或文件)。

So basically, VARCHAR(MAX) keeps the data with the record, and gives you the ability to treat it like other VARCHAR types, like using GROUP BY and string functions ( LEN , CHARINDEX , etc.). 因此,基本上, VARCHAR(MAX)将数据保留在记录中,并使您能够像对待其他VARCHAR类型一样对待数据,例如使用GROUP BY和字符串函数( LENCHARINDEX等)。

For TEXT , you almost always have to convert it to VARCHAR to use functions against it. 对于TEXT ,您几乎总是必须将其转换为VARCHAR才能对其使用函数。

But back to the root of your question regarding efficiency, I don't think it's ever more efficient to use TEXT vs. VARCHAR(MAX) . 但是回到您关于效率的问题的根源,我认为使用TEXTVARCHAR(MAX)效率没有任何提高。 Looking at this MSDN article (search for "data types"), TEXT is deprecated, and should be replaced with VARCHAR(MAX). 查看此MSDN文章 (搜索“数据类型”),不赞成使用TEXT ,而应将其替换为VARCHAR(MAX)。

First of all don't use text at all. 首先,根本不使用文本。 MSDN says: MSDN说:

ntext, text, and image data types will be removed in a future version of Microsoft SQL Server. 在将来的Microsoft SQL Server版本中,将删除ntext,text和image数据类型。 Avoid using these data types in new development work, and plan to modify applications that currently use them. 避免在新的开发工作中使用这些数据类型,并计划修改当前使用它们的应用程序。 Use nvarchar(max), varchar(max), and varbinary(max) instead. 请改用nvarchar(max),varchar(max)和varbinary(max)。

varchar(max) is what you might need. varchar(max)是您可能需要的。

If you compare varchar(n) vs varchar(max), these are technically two different datatypes (stored differently): 如果比较varchar(n)和varchar(max),从技术上讲,这是两种不同的数据类型(存储方式不同):

  • varchar(n) value is always stored inside of the row. varchar(n)值始终存储在行内部。 Which means it cannot be greater than max row size, and row cannot be greater than page size, which is 8K. 这意味着它不能大于最大行大小,并且行不能大于页面大小,即8K。

  • varchar(max) is stored outsize the row. varchar(max)存储的行超出了行的大小。 Row has a pointer to a separate BLOB page. 行有一个指向单独的BLOB页的指针。 However, under certain condition varchar(max) can store data as a regular row, obviously it should at least fit to the row size. 但是,在某些情况下,varchar(max)可以将数据存储为常规行,显然,它至少应适合行大小。

So if your row is potentially greater than 8K, you have to use varchar(max). 因此,如果您的行可能大于8K,则必须使用varchar(max)。 If not, using varchar(n) will likely be preferable as it is faster to retrieve in-row data vs from outside page. 如果不是这样,则使用varchar(n)可能更可取,因为与从外部页面检索行内数据相比,检索速度更快。

MSDN says : MSDN说

Use varchar(max) when the sizes of the column data entries vary considerably, and the size might exceed 8,000 bytes. 当列数据条目的大小相差很大,并且大小可能超过8,000个字节时,请使用varchar(max)。

The main advantage of VARCHAR over TEXT is that you can run string manipulations and string functions on it. 与TEXT相比,VARCHAR的主要优点是可以在其上运行字符串操作和字符串函数。 With VARCHAR(max), now you basically have an awesome large (unrestricted) variable that you can manipulate how you want.. 使用VARCHAR(max),现在您基本上有了一个了不起的大(无限制)变量,您可以根据需要操作它。

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

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