sql server中varchar(500)和varchar(max)的区别 [英] difference between varchar(500) vs varchar(max) in sql server

查看:55
本文介绍了sql server中varchar(500)和varchar(max)的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

  • 两者会使用相同数量的存储空间吗?

对于 sql server 2000/2005/2008,答案是否不同?

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

推荐答案

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

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.

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

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.

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

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 中使用 MAX 数据类型而不是 BLOB.事实上,在 SQL Server 的未来版本中,BLOB 已被弃用.

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.

来源:http://www.teratrax.com/articles/varchar_max.html

在 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.

In SQL Server 2005 and 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.

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

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

参考:http://msdn.microsoft.com/en-us/图书馆/ms143432.aspx

这篇关于sql server中varchar(500)和varchar(max)的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆