nvarchar(max) vs nvarchar(N)
MS SQL Server offers several data types for storing strings.
The most popular are nvarchar(N)
(1 ≤ N ≤ 4000) and nvarchar(max)
, which allow storing Unicode-encoded string data.
Application developers often transfer their experience with the string
type from programming languages like C# or Java to databases and automatically choose nvarchar(max)
, which can store strings up to 2GB.
However, databases store and process data in fundamentally different ways.
In this article, I will explain and demonstrate the consequences of unjustified use of the nvarchar(max)
type.
nvarchar(max) vs nvarchar(N)
MS SQL Server предоставляет нам на выбор несколько типов данных для хранения строк. Самый популярный - nvarchar(N)
(где 1 ≤ N ≤ 4000) или nvarchar(max)
, который позволяет хранить строковые данные в кодировке Юникод. При этом, часто прикладные разработчики переносят опыт использования типа string
из языка приложения (C# или Java, например) в базы данных и не задумываясь выбирают тип nvarchar(max)
, позволяющий хранить строки размером до 2ГБ. Но базы данных хранят и работают с данными совершенно другим способом. В этой статье я расскажу и покажу на практике, к чему может приводить неоправданное использование типа nvarchar(max)
.
List initializer
We all used to type new List<int> { 1, 2, 3, 4 }
or new int[] { 1, 2, 3, 4}
to initialize collections with some values. It looks similar in syntax but differs in behavior and you should be careful if you are concerned about performance.
Harmful collection transformations. Part 3: collections
Starting with string
in the first post we continue to study examples with collection transformations and how they affect our applications.
Harmful collection transformations. Part 2: automatic diagnostics
The previous post contains code examples that can be simplified by removing ToArray()
or ToCharArray()
calls. And an interesting example with List<char>
constructor. But we need to be very attentive to find such places in our code. It would be much safer to do it automatically. This post shows us about automatic compiler diagnostics that can detect issues.