I have variable length character data and want to store in SQL Server (2005) database. I want to learn some best practices about how to choose TEXT SQL type or choose VARCHAR SQL type, pros and cons in performance/footprint/function.
我有可变长度字符数据,并希望存储在SQL Server(2005)数据库中。我想学习一些关于如何选择文本SQL类型的最佳实践,或者选择VARCHAR SQL类型,在性能/内存占用/功能方面的优点和缺点。
4 个解决方案
#1
179
If you're using SQL Server 2005 or later, use varchar(MAX)
. The text
datatype is deprecated and should not be used for new development work. From the docs:
如果您使用的是SQL Server 2005或更高版本,请使用varchar(MAX)。文本数据类型被弃用,不应该用于新的开发工作。从文档:
Important
ntext
,text
, andimage
data types will be removed in a future version of Microsoft SQL Server. 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.在未来版本的Microsoft SQL Server中,将删除ntext、text和图像数据类型。避免在新的开发工作中使用这些数据类型,并计划修改当前使用它们的应用程序。使用nvarchar(max)、varchar(max)和varbinary(max)代替。
#2
267
TEXT
is used for large pieces of string data. If the length of the field exceeed a certain threshold, the text is stored out of row.
文本用于大型字符串数据。如果字段的长度超过某个阈值,文本就会被存储在行中。
VARCHAR
is always stored in row and has a limit of 8000 characters. If you try to create a VARCHAR(x)
, where x > 8000, you get an error:
VARCHAR总是存储在行中,并且有8000个字符的限制。如果你试图创建一个VARCHAR(x),其中x > 8000,你会得到一个错误:
Server: Msg 131, Level 15, State 3, Line 1
服务器:Msg 131,第15级,状态3,第1行。
The size () given to the type ‘varchar’ exceeds the maximum allowed for any data type (8000)
给定类型“varchar”的大小()超过了任何数据类型(8000)所允许的最大值
These length limitations do not concern VARCHAR(MAX)
in SQL Server 2005, which may be stored out of row, just like TEXT
.
这些长度限制不涉及SQL Server 2005中的VARCHAR(MAX),它可能被存储在行中,就像文本一样。
Note that MAX
is not a kind of constant here, VARCHAR
and VARCHAR(MAX)
are very different types, the latter being very close to TEXT
.
注意,MAX在这里不是一种常量,VARCHAR和VARCHAR(MAX)是非常不同的类型,后者非常接近于文本。
In prior versions of SQL Server you could not access the TEXT
directly, you only could get a TEXTPTR
and use it in READTEXT
and WRITETEXT
functions.
在以前版本的SQL Server中,不能直接访问文本,只能获得一个TEXTPTR,并在READTEXT和WRITETEXT函数中使用它。
In SQL Server 2005 you can directly access TEXT
columns (though you still need an explicit cast to VARCHAR
to assign a value for them).
在SQL Server 2005中,您可以直接访问文本列(尽管您仍然需要向VARCHAR显式转换以为它们分配一个值)。
TEXT
is good:
文本是好的:
- If you need to store large texts in your database
- 如果需要在数据库中存储大型文本。
- If you do not search on the value of the column
- 如果您不搜索列的值。
- If you select this column rarely and do not join on it.
- 如果您很少选择这一列,并且不参与其中。
VARCHAR
is good:
VARCHAR是好的。
- If you store little strings
- 如果你存储小字符串。
- If you search on the string value
- 如果你搜索字符串值。
- If you always select it or use it in joins.
- 如果您总是选择它或在连接中使用它。
By selecting here I mean issuing any queries that return the value of the column.
通过选择这里,我的意思是发出任何返回该列值的查询。
By searching here I mean issuing any queries whose result depends on the value of the TEXT
or VARCHAR
column. This includes using it in any JOIN
or WHERE
condition.
通过搜索,我的意思是发出任何查询,其结果取决于文本或VARCHAR列的值。这包括在任何连接或条件下使用它。
As the TEXT
is stored out of row, the queries not involving the TEXT
column are usually faster.
由于文本存储在行中,所以不涉及文本列的查询通常更快。
Some examples of what TEXT
is good for:
一些例子说明了什么是文本的好处:
- Blog comments
- 博客评论
- Wiki pages
- Wiki页面
- Code source
- 代码源
Some examples of what VARCHAR
is good for:
VARCHAR的一些例子很好:
- Usernames
- 用户名
- Page titles
- 页面标题
- Filenames
- 文件名
As a rule of thumb, if you ever need you text value to exceed 200 characters AND do not use join on this column, use TEXT
.
根据经验,如果您需要您的文本值超过200个字符,并且不使用该列的连接,请使用文本。
Otherwise use VARCHAR
.
否则使用VARCHAR。
P.S. The same applies to UNICODE
enabled NTEXT
and NVARCHAR
as well, which you should use for examples above.
同样适用于UNICODE的NTEXT和NVARCHAR,您应该使用上面的例子。
P.P.S. The same applies to VARCHAR(MAX)
and NVARCHAR(MAX)
that SQL Server 2005+ uses instead of TEXT
and NTEXT
. You'll need to enable large value types out of row
for them with sp_tableoption
if you want them to be always stored out of row.
这同样适用于VARCHAR(MAX)和NVARCHAR(MAX), SQL Server 2005+使用而不是文本和NTEXT。如果希望它们始终存储在行中,则需要使用sp_tableoption为它们启用大型值类型。
As mentioned above and here, TEXT
is going to be deprecated in future releases:
如前所述,在未来的版本中,文本将被弃用:
The
text in row
option will be removed in a future version of SQL Server. Avoid using this option in new development work, and plan to modify applications that currently usetext in row
. We recommend that you store large data by using thevarchar(max)
,nvarchar(max)
, orvarbinary(max)
data types. To control in-row and out-of-row behavior of these data types, use thelarge value types out of row
option.行选项中的文本将在SQL Server的未来版本中删除。避免在新的开发工作中使用此选项,并计划修改当前使用文本的应用程序。我们建议您使用varchar(max)、nvarchar(max)或varbinary(max)数据类型存储大数据。为了控制这些数据类型的行和行外行为,请使用row选项中的大值类型。
#3
37
In SQL server 2005 new datatypes were introduced: varchar(max)
and nvarchar(max)
They have the advantages of the old text type: they can contain op to 2GB of data, but they also have most of the advantages of varchar
and nvarchar
. Among these advantages are the ability to use string manipulation functions such as substring().
在SQL server 2005中引入了新的数据类型:varchar(max)和nvarchar(max),它们具有旧文本类型的优点:它们可以包含op到2GB的数据,但是它们也有varchar和nvarchar的大部分优点。这些优点包括使用字符串操作函数(如子字符串())的能力。
Also, varchar(max) is stored in the table's (disk/memory) space while the size is below 8Kb. Only when you place more data in the field, it's is stored out of the table's space. Data stored in the table's space is (usually) retrieved quicker.
此外,varchar(max)存储在表的(磁盘/内存)空间中,而大小低于8Kb。只有在字段中放置更多数据时,它才会被存储在表空间中。存储在表空间中的数据(通常)检索得更快。
In short, never use Text, as there is a better alternative: (n)varchar(max). And only use varchar(max) when a regular varchar is not big enough, ie if you expect teh string that you're going to store will exceed 8000 characters.
总之,永远不要使用文本,因为有更好的选择:(n)varchar(max)。只有当常规的varchar不够大的时候才使用varchar(max),如果你想要存储的teh字符串将超过8000个字符。
As was noted, you can use SUBSTRING on the TEXT datatype,but only as long the TEXT fields contains less than 8000 characters.
如前所述,您可以在文本数据类型上使用子字符串,但只有在文本字段包含少于8000个字符的情况下才可以使用。
#4
6
There has been some major changes in ms 2008 -> Might be worth considering the following article when making a decisions on what data type to use. http://msdn.microsoft.com/en-us/library/ms143432.aspx
在2008年ms ->中有一些重大的变化,在决定使用哪种数据类型时,可能值得考虑下面这篇文章。http://msdn.microsoft.com/en-us/library/ms143432.aspx
Bytes per
字节每
- varchar(max), varbinary(max), xml, text, or image column 2^31-1 2^31-1
- varchar(max),varbinary(max)、xml、文本或图像列2 ^ ^还有2还有
- nvarchar(max) column 2^30-1 2^30-1
- nvarchar(max)列2 ^ 2 ^比率是30 - 1。比率是30 - 1
#1
179
If you're using SQL Server 2005 or later, use varchar(MAX)
. The text
datatype is deprecated and should not be used for new development work. From the docs:
如果您使用的是SQL Server 2005或更高版本,请使用varchar(MAX)。文本数据类型被弃用,不应该用于新的开发工作。从文档:
Important
ntext
,text
, andimage
data types will be removed in a future version of Microsoft SQL Server. 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.在未来版本的Microsoft SQL Server中,将删除ntext、text和图像数据类型。避免在新的开发工作中使用这些数据类型,并计划修改当前使用它们的应用程序。使用nvarchar(max)、varchar(max)和varbinary(max)代替。
#2
267
TEXT
is used for large pieces of string data. If the length of the field exceeed a certain threshold, the text is stored out of row.
文本用于大型字符串数据。如果字段的长度超过某个阈值,文本就会被存储在行中。
VARCHAR
is always stored in row and has a limit of 8000 characters. If you try to create a VARCHAR(x)
, where x > 8000, you get an error:
VARCHAR总是存储在行中,并且有8000个字符的限制。如果你试图创建一个VARCHAR(x),其中x > 8000,你会得到一个错误:
Server: Msg 131, Level 15, State 3, Line 1
服务器:Msg 131,第15级,状态3,第1行。
The size () given to the type ‘varchar’ exceeds the maximum allowed for any data type (8000)
给定类型“varchar”的大小()超过了任何数据类型(8000)所允许的最大值
These length limitations do not concern VARCHAR(MAX)
in SQL Server 2005, which may be stored out of row, just like TEXT
.
这些长度限制不涉及SQL Server 2005中的VARCHAR(MAX),它可能被存储在行中,就像文本一样。
Note that MAX
is not a kind of constant here, VARCHAR
and VARCHAR(MAX)
are very different types, the latter being very close to TEXT
.
注意,MAX在这里不是一种常量,VARCHAR和VARCHAR(MAX)是非常不同的类型,后者非常接近于文本。
In prior versions of SQL Server you could not access the TEXT
directly, you only could get a TEXTPTR
and use it in READTEXT
and WRITETEXT
functions.
在以前版本的SQL Server中,不能直接访问文本,只能获得一个TEXTPTR,并在READTEXT和WRITETEXT函数中使用它。
In SQL Server 2005 you can directly access TEXT
columns (though you still need an explicit cast to VARCHAR
to assign a value for them).
在SQL Server 2005中,您可以直接访问文本列(尽管您仍然需要向VARCHAR显式转换以为它们分配一个值)。
TEXT
is good:
文本是好的:
- If you need to store large texts in your database
- 如果需要在数据库中存储大型文本。
- If you do not search on the value of the column
- 如果您不搜索列的值。
- If you select this column rarely and do not join on it.
- 如果您很少选择这一列,并且不参与其中。
VARCHAR
is good:
VARCHAR是好的。
- If you store little strings
- 如果你存储小字符串。
- If you search on the string value
- 如果你搜索字符串值。
- If you always select it or use it in joins.
- 如果您总是选择它或在连接中使用它。
By selecting here I mean issuing any queries that return the value of the column.
通过选择这里,我的意思是发出任何返回该列值的查询。
By searching here I mean issuing any queries whose result depends on the value of the TEXT
or VARCHAR
column. This includes using it in any JOIN
or WHERE
condition.
通过搜索,我的意思是发出任何查询,其结果取决于文本或VARCHAR列的值。这包括在任何连接或条件下使用它。
As the TEXT
is stored out of row, the queries not involving the TEXT
column are usually faster.
由于文本存储在行中,所以不涉及文本列的查询通常更快。
Some examples of what TEXT
is good for:
一些例子说明了什么是文本的好处:
- Blog comments
- 博客评论
- Wiki pages
- Wiki页面
- Code source
- 代码源
Some examples of what VARCHAR
is good for:
VARCHAR的一些例子很好:
- Usernames
- 用户名
- Page titles
- 页面标题
- Filenames
- 文件名
As a rule of thumb, if you ever need you text value to exceed 200 characters AND do not use join on this column, use TEXT
.
根据经验,如果您需要您的文本值超过200个字符,并且不使用该列的连接,请使用文本。
Otherwise use VARCHAR
.
否则使用VARCHAR。
P.S. The same applies to UNICODE
enabled NTEXT
and NVARCHAR
as well, which you should use for examples above.
同样适用于UNICODE的NTEXT和NVARCHAR,您应该使用上面的例子。
P.P.S. The same applies to VARCHAR(MAX)
and NVARCHAR(MAX)
that SQL Server 2005+ uses instead of TEXT
and NTEXT
. You'll need to enable large value types out of row
for them with sp_tableoption
if you want them to be always stored out of row.
这同样适用于VARCHAR(MAX)和NVARCHAR(MAX), SQL Server 2005+使用而不是文本和NTEXT。如果希望它们始终存储在行中,则需要使用sp_tableoption为它们启用大型值类型。
As mentioned above and here, TEXT
is going to be deprecated in future releases:
如前所述,在未来的版本中,文本将被弃用:
The
text in row
option will be removed in a future version of SQL Server. Avoid using this option in new development work, and plan to modify applications that currently usetext in row
. We recommend that you store large data by using thevarchar(max)
,nvarchar(max)
, orvarbinary(max)
data types. To control in-row and out-of-row behavior of these data types, use thelarge value types out of row
option.行选项中的文本将在SQL Server的未来版本中删除。避免在新的开发工作中使用此选项,并计划修改当前使用文本的应用程序。我们建议您使用varchar(max)、nvarchar(max)或varbinary(max)数据类型存储大数据。为了控制这些数据类型的行和行外行为,请使用row选项中的大值类型。
#3
37
In SQL server 2005 new datatypes were introduced: varchar(max)
and nvarchar(max)
They have the advantages of the old text type: they can contain op to 2GB of data, but they also have most of the advantages of varchar
and nvarchar
. Among these advantages are the ability to use string manipulation functions such as substring().
在SQL server 2005中引入了新的数据类型:varchar(max)和nvarchar(max),它们具有旧文本类型的优点:它们可以包含op到2GB的数据,但是它们也有varchar和nvarchar的大部分优点。这些优点包括使用字符串操作函数(如子字符串())的能力。
Also, varchar(max) is stored in the table's (disk/memory) space while the size is below 8Kb. Only when you place more data in the field, it's is stored out of the table's space. Data stored in the table's space is (usually) retrieved quicker.
此外,varchar(max)存储在表的(磁盘/内存)空间中,而大小低于8Kb。只有在字段中放置更多数据时,它才会被存储在表空间中。存储在表空间中的数据(通常)检索得更快。
In short, never use Text, as there is a better alternative: (n)varchar(max). And only use varchar(max) when a regular varchar is not big enough, ie if you expect teh string that you're going to store will exceed 8000 characters.
总之,永远不要使用文本,因为有更好的选择:(n)varchar(max)。只有当常规的varchar不够大的时候才使用varchar(max),如果你想要存储的teh字符串将超过8000个字符。
As was noted, you can use SUBSTRING on the TEXT datatype,but only as long the TEXT fields contains less than 8000 characters.
如前所述,您可以在文本数据类型上使用子字符串,但只有在文本字段包含少于8000个字符的情况下才可以使用。
#4
6
There has been some major changes in ms 2008 -> Might be worth considering the following article when making a decisions on what data type to use. http://msdn.microsoft.com/en-us/library/ms143432.aspx
在2008年ms ->中有一些重大的变化,在决定使用哪种数据类型时,可能值得考虑下面这篇文章。http://msdn.microsoft.com/en-us/library/ms143432.aspx
Bytes per
字节每
- varchar(max), varbinary(max), xml, text, or image column 2^31-1 2^31-1
- varchar(max),varbinary(max)、xml、文本或图像列2 ^ ^还有2还有
- nvarchar(max) column 2^30-1 2^30-1
- nvarchar(max)列2 ^ 2 ^比率是30 - 1。比率是30 - 1