sql server中varchar(500)和varchar(max)的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3682821/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 07:30:22  来源:igfitidea点击:

difference between varchar(500) vs varchar(max) in sql server

sqlsql-server

提问by Prashant Lakhlani

I want to know what are pros and cons while using varchar(500) vs varchar(max) in terms of performance, memory and anything else to consider?

我想知道在性能、内存和其他需要考虑的方面,使用 varchar(500) 和 varchar(max) 的优缺点是什么?

  • Will both use same amount of storage space?
  • 两者会使用相同数量的存储空间吗?

Is the answer differ in case of sql server 2000/2005/2008?

在 sql server 2000/2005/2008 的情况下,答案是否不同?

回答by shamittomar

In SQL Server 2000 and SQL Server 7, a row cannot exceed 8000 bytes in size. This means that a VARBINARY column can only store 8000 bytes (assuming it is the only column in a table), a VARCHAR column can store up to 8000 characters and an NVARCHAR column can store up to 4000 characters (2 bytes per unicode character). This limitation stems from the 8 KB internal page size SQL Server uses to save data to disk.

在 SQL Server 2000 和 SQL Server 7 中,一行的大小不能超过 8000 字节。这意味着一个 VARBINARY 列只能存储 8000 个字节(假设它是表中唯一的列),一个 VARCHAR 列最多可以存储 8000 个字符,一个 NVARCHAR 列最多可以存储 4000 个字符(每个 unicode 字符 2 个字节)。此限制源于 SQL Server 用于将数据保存到磁盘的 8 KB 内部页面大小。

To store more data in a single column, you needed to use the TEXT, NTEXT, or IMAGE data types (BLOBs) which are stored in a collection of 8 KB data pages that are separate from the data pages that store the other data in the same table. These data pages are arranged in a B-tree structure. BLOBs are hard to work with and manipulate. They cannot be used as variables in a procedure or a function and they cannot be used inside string functions such as REPLACE, CHARINDEX or SUBSTRING. In most cases, you have to use READTEXT, WRITETEXT, and UPDATETEXT commands to manipulate BLOBs.

要在单个列中存储更多数据,您需要使用 TEXT、NTEXT 或 IMAGE 数据类型 (BLOB),这些数据类型存储在 8 KB 数据页的集合中,这些数据页与在列中存储其他数据的数据页是分开的。同一张桌子。这些数据页以 B 树结构排列。BLOB 很难使用和操作。它们不能在过程或函数中用作变量,也不能在字符串函数(如 REPLACE、CHARINDEX 或 SUBSTRING)中使用。在大多数情况下,您必须使用 READTEXT、WRITETEXT 和 UPDATETEXT 命令来操作 BLOB。

To solve this problem, Microsoft introduced the VARCHAR(MAX), NVARCHAR(MAX), and VARBINARY(MAX) data types in SQL Server 2005. These data types can hold the same amount of data BLOBs can hold (2 GB) and they are stored in the same type of data pages used for other data types. When data in a MAX data type exceeds 8 KB, an over-flow page is used. SQL Server 2005 automatically assigns an over-flow indicator to the page and knows how to manipulate data rows the same way it manipulates other data types. You can declare variables of MAX data types inside a stored procedure or function and even pass them as variables. You can also use them inside string functions.

为了解决这个问题,Microsoft 在 SQL Server 2005 中引入了 VARCHAR(MAX)、NVARCHAR(MAX) 和 VARBINARY(MAX) 数据类型。这些数据类型可以容纳 BLOB 可以容纳的相同数量的数据(2 GB),它们是存储在相同类型的数据页中,用于其他数据类型。当 MAX 数据类型中的数据超过 8 KB 时,使用溢出页面。SQL Server 2005 自动为页面分配一个溢出指示器,并且知道如何像处理其他数据类型一样处理数据行。您可以在存储过程或函数中声明 MAX 数据类型的变量,甚至可以将它们作为变量传递。您也可以在字符串函数中使用它们。

Microsoft recommend using MAX data types instead of BLOBs in SQL Server 2005. In fact, BLOBs are being deprecated in future releases of SQL Server.

Microsoft 建议在 SQL Server 2005 中使用 MAX 数据类型而不是 BLOB。事实上,在 SQL Server 的未来版本中,BLOB 已被弃用。

Credit: http://www.teratrax.com/articles/varchar_max.html

信用:http: //www.teratrax.com/articles/varchar_max.html



In SQL Server 2005and SQL Server 2008, The maximum storage size for VARCHAR(MAX) is 2^31-1 bytes (2,147,483,647 bytes or 2GB - 1 bytes). The storage size is the actual length of data entered + 2 bytes. The data entered can be 0 characters in length. Since each character in a VARCHAR data type uses one byte, the maximum length for a VARCHAR(MAX) data type is 2,147,483,645.

在 SQL Server 2005和 SQL Server 2008 中,VARCHAR(MAX) 的最大存储大小为 2^31-1 字节(2,147,483,647 字节或 2GB - 1 字节)。存储大小为输入数据的实际长度+2 个字节。输入的数据长度可以为 0 个字符。由于 VARCHAR 数据类型中的每个字符使用一个字节,因此 VARCHAR(MAX) 数据类型的最大长度为 2,147,483,645。

Full Interesting read for you: http://www.sql-server-helper.com/faq/sql-server-2005-varchar-max-p01.aspx

完整有趣的阅读:http: //www.sql-server-helper.com/faq/sql-server-2005-varchar-max-p01.aspx

Reference: http://msdn.microsoft.com/en-us/library/ms143432.aspx

参考:http: //msdn.microsoft.com/en-us/library/ms143432.aspx

回答by onedaywhen

A VARCHAR(MAX)column will accept a value of 501 characters or more whereas a VARCHAR(500)column will not. So if you have a business rule that restricts a value to 500 characters, VARCHAR(500)will be more appropriate.

VARCHAR(MAX)列将接受 501 个或更多字符的值,而一VARCHAR(500)列则不会。因此,如果您有一个将值限制为 500 个字符的业务规则,VARCHAR(500)将更合适。