Is varchar(MAX) always preferable?

前端 未结 4 1365
佛祖请我去吃肉
佛祖请我去吃肉 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:53

    Just to be quite explicit on this one, the answer is NO. It is always preferable to use varchar(N), and if you know the size will not vary, then char(N). The MAX types do not and cannot support most of the native SQL features so you cannot add indexes, perform joins nor do effective searches on those types. Incidentally, this is one reason why the full-text search capability exists in SQL Server.

    In addition, varchar(max) prevents the ability to perform online indexes against the entire table which contains the varchar(max) field. This will significantly impact performance of your system.

    Varchar(max) should only ever be used if the size of the field is known to be over 8K. In every other instance, the size must be specified. Failure to do so is poor design and will lead to performance issues on any but the most trivial of systems.

    Some references:

    • http://www.sqlservercentral.com/blogs/martin_catherall/2011/10/26/watch-those-varchar_2800_max_2900_-columns_2C00_-and-online-index-rebuilds/
    • http://blogs.msdn.com/b/psssql/archive/2012/12/03/how-it-works-gotcha-varchar-max-caused-my-queries-to-be-slower.aspx

提交回复
热议问题