I was wondering what would be the consequences of setting NVARCHAR fields to MAX instead of a specific size in SQL Server 2008, and limiting the input from the application's logic. Also, would these be a bad design practice?
You shouldn't set all your fields to NVARCHAR(MAX) if you know they will never hold more than a finite number of characters due to the way SQL stores this kind of data - data small enough to fit in the page will be stored in the page, but when it grows too large it will be moved off the page and be stored separately.
Also, are you sure you need NVARCHAR as this stores unicode data which takes up twice the space of standard VARCHAR? If you know you will be using standard characters then use VARCHAR instead.
Slo, think of the uses of your application. If you have an address field that has no theoretical limit on its size, how would you print it on your envelope? You say you will implement logic in the front end applciation, but why still allow the database to have data that is too large? And what happens if data gets into the database that breaks the logic in your front end?
There are a lot of performance implications. In particular, in a general purpose string padding scalar UDF, I noticed huge performance differences where the output was declared as VARCHAR(MAX), even though the inputs were never > 40 characters. Changing to VARCHAR(50) made a HUGE improvement.
Main drawback is that NVARCHAR(MAX)
cannot be indexed with plain indexes.
Also, there are some issues with variables of type NVARCHAR(MAX)
and with performance of functions parsing these variables.
If you just want to store and retrieve data as is, and not parse it on SQL Server
's side, then NVARCHAR(MAX)
is fine.
NVARCHAR(MAX) is a lot better at performing with smaller data than the old NTEXT data type, however, NVARCHAR(n) will always be more efficient in some areas.
As a general rule, using the datatype that best represents the data you are storing is almost always the best practice.
Seting a limit has a side effect of some validation on the max length
Just to complement all the other answers: be also careful of scenarios where the data can come from other sources, like - just as an example - text files imported from outside your applications; this could bypass any application logic, unless you duplicate it in the import routines...