Varchar (500)比 varchar (8000)有优势吗?

我已经在 MSDN 论坛和这里阅读了这一点,我仍然不清楚。我认为这是正确的: Varchar (max)将作为文本数据类型存储,因此有缺点。因此,假设您的字段将可靠地小于8000个字符。类似于数据库表中的 BusinessName 字段。实际上,一个企业名称可能总是在500个字符之下(从帽子里抽出一个数字)。似乎我遇到的很多 varchar 字段都低于8k 字符计数。

那么我应该将该字段设置为 varchar (500)而不是 varchar (8000)吗?根据我对 SQL 的理解,这两者之间没有区别。因此,为了方便起见,我希望将所有 varchar 字段定义为 varchar (8000)。有什么缺点吗?

相关阅读: Varchar 列的大小(我不觉得这个回答了我的问题)。

45794 次浏览

Ideally you'd want to go smaller than that, down to a reasonably sized length (500 isn't reasonably sized) and make sure the client validation catches when the data is going to be too large and send a useful error.

While the varchar isn't actually going to reserve space in the database for the unused space, I recall versions of SQL Server having a snit about database rows being wider than some number of bytes (do not recall the exact count) and actually throwing out whatever data didn't fit. A certain number of those bytes were reserved for things internal to SQL Server.

From a processing standpoint, it will not make a difference to use varchar(8000) vs varchar(500). It's more of a "good practice" kind of thing to define a maximum length that a field should hold and make your varchar that length. It's something that can be used to assist with data validation. For instance, making a state abbreviation be 2 characters or a postal/zip code as 5 or 9 characters. This used to be a more important distinction for when your data interacted with other systems or user interfaces where field length was critical (e.g. a mainframe flat file dataset), but nowadays I think it's more habit than anything else.

Apart from best practices (BBlake's answer)

  • You get warnings about maximum row size (8060) bytes and index width (900 bytes) with DDL
  • DML will die if you exceed these limits
  • ANSI PADDING ON is the default so you could end up storing a wholeload of whitespace

One example where this can make a difference is that it can prevent a performance optimization that avoids adding row versioning information to tables with after triggers.

This is covered by Paul White here

The actual size of the data stored is immaterial – it is the potential size that matters.

Similarly if using memory optimised tables since 2016 it has been possible to use LOB columns or combinations of column widths that could potentially exceed the inrow limit but with a penalty.

(Max) columns are always stored off-row. For other columns, if the data row size in the table definition can exceed 8,060 bytes, SQL Server pushes largest variable-length column(s) off-row. Again, it does not depend on amount of the data you store there.

This can have a large negative effect on memory consumption and performance

Another case where over declaring column widths can make a big difference is if the table will ever be processed using SSIS. The memory allocated for variable length (non BLOB) columns is fixed for each row in an execution tree and is per the columns' declared maximum length which can lead to inefficient usage of memory buffers (example). Whilst the SSIS package developer can declare a smaller column size than the source this analysis is best done up front and enforced there.

Back in the SQL Server engine itself a similar case is that when calculating the memory grant to allocate for SORT operations SQL Server assumes that varchar(x) columns will on average consume x/2 bytes.

If most of your varchar columns are fuller than that this can lead to the sort operations spilling to tempdb.

In your case if your varchar columns are declared as 8000 bytes but actually have contents much less than that your query will be allocated memory that it doesn't require which is obviously inefficient and can lead to waits for memory grants.

This is covered in Part 2 of SQL Workshops Webcast 1 downloadable from here or see below.

use tempdb;


CREATE TABLE T(
id INT IDENTITY(1,1) PRIMARY KEY,
number int,
name8000 VARCHAR(8000),
name500 VARCHAR(500))


INSERT INTO  T
(number,name8000,name500)
SELECT number, name, name /*<--Same contents in both cols*/
FROM master..spt_values


SELECT id,name500
FROM T
ORDER BY number

Screenshot

SELECT id,name8000
FROM T
ORDER BY number

Screenshot

There are some disadvantages to large columns that are a bit less obvious and might catch you a little later:

  • All columns you use in an INDEX - must not exceed 900 bytes
  • All the columns in an ORDER BY clause may not exceed 8060 bytes. This is a bit difficult to grasp since this only applies to some columns. See SQL 2008 R2 Row size limit exceeded for details)
  • If the total row size exceeds 8060 bytes, you get a "page spill" for that row. This might affect performance (A page is an allocation unit in SQLServer and is fixed at 8000 bytes+some overhead. Exceeding this will not be severe, but it's noticable and you should try to avoid it if you easily can)
  • Many other internal datastructures, buffers and last-not-least your own varaibles and table-variables all need to mirror these sizes. With excessive sizes, excessive memory allocation can affect performance

As a general rule, try to be conservative with the column width. If it becomes a problem, you can easily expand it to fit the needs. If you notice memory issues later, shrinking a wide column later may become impossible without losing data and you won't know where to begin.

In your example of the business names, think about where you get to display them. Is there really space for 500 characters?? If not, there is little point in storing them as such. http://en.wikipedia.org/wiki/List_of_companies_of_the_United_States lists some company names and the max is about 50 characters. So I'd use 100 for the column max. Maybe more like 80.