Someone was reviewing my DDL code for creating tables and suggested, when they saw I saw using VARCHAR(256)
fields for text I expect to be pretty small, like a first name or whatever, that I should always just use VARCHAR(MAX)
and linked Why use anything but varchar(max). I read it but it seemed dated, as it was focusing on 2005, and didn't seem to offer any real justification to allocate potentially up to 2 GB per row on all text fields.
From a performance, storage, etc. standpoint, how should one go about deciding whether to use VARCHAR(MAX)
or a smaller more specific type for modern versions of SQL Server? (e.g., 2008, 2012, 2014)
Best Answer
No.
For SQL Server, the
max
data types should only be specified when there is no alternative. One should instead choose the correct base type (varchar
ornvarchar
) and specify an explicit maximum length that is appropriate to the data to be stored.Physical storage is identical whether the column is typed as
varchar(n)
orvarchar(max)
, so that is not the concern.The reasons to not choose
(n)varchar(max)
everywhere revolve around features, plan quality, and performance.An exhaustive list is probably not practical, but among other things,
max
columns:Features
max
data types. Not all limitations and side-effects are documented.Performance
max
to match column definitionsIn summary, there are so many subtle (and undesirable) side effects of unnecessarily using the
max
specifier that it makes no sense to do this. The minor 'convenience' of using a single declaration is no sort of compensation.Evaluate each type in context, use the correct base type (
varchar
ornvarchar
), and a sensible explicit length.Further reading: