Is varchar(MAX) always preferable?

前端 未结 4 1364
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-13 08:05

Regarding SQL Server, I understand :

  • var means the memory is lazy allocated, meaning it fits to the data exactly (on insertion).

  • <
4条回答
  •  無奈伤痛
    2020-12-13 08:39

    There is a very good article on this subject by SO User @Remus Rusanu. Here is a snippit that I've stolen but I suggest you read the whole thing:

    The code path that handles the MAX types (varchar, nvarchar and varbinary) is different from the code path that handles their equivalent non-max length types. The non-max types can internally be represented as an ordinary pointer-and-length structure. But the max types cannot be stored internally as a contiguous memory area, since they can possibly grow up to 2Gb. So they have to be represented by a streaming interface, similar to COM’s IStream. This carries over to every operation that involves the max types, including simple assignment and comparison, since these operations are more complicated over a streaming interface. The biggest impact is visible in the code that allocates and assign max-type variables (my first test), but the impact is visible on every operation.

    In the article he shows several examples that demonstrate that using varchar(n) typically improves performance.

    You can find the entire article here.

提交回复
热议问题