TEXT
and VarChar(MAX)
are non-Unicode large variable length character data type, which can store maximum of 2,147,483,647 non-Unicode characters (i.e. maximum storage capacity is: 2GB).
As per MSDN, Microsoft is suggesting to avoid using the TEXT
datatype and it will be removed in a future version of SQL Server. VarChar(MAX)
is the suggested data type for storing large string values instead of the TEXT
data type.
- In-Row or Out-of-Row Storage
Data of a TEXT
type column is stored out-of-row in a separate LOB data pages. The row in the table data page will only have a 16 byte pointer to the LOB data page where the actual data is present. The data of a VarChar(MAX)
type column is stored in-row if it is less than or equal to 8000 bytes. If the value of a VarChar(MAX)
column is greater than 8000 bytes, then the VarChar(MAX)
column value is stored in a separate LOB data pages and row will only have a 16 byte pointer to the LOB data page where the actual data is present. So "in-row" VarChar(MAX)
is good for searches and retrieval.
- Supported/Unsupported Functionalities
Some string functions, operators and constructs don't work on a TEXT
type column, but they do work on a VarChar(MAX)
type column.
=
Equal to operator on VarChar(MAX)
type column
GROUP BY
clause on VarChar(MAX)
type column
As we know, the VarChar(MAX)
type column values are stored out-of-row only when the length of the value is greater than 8000 bytes or there is not enough space in the row, otherwise it will store it in-row. So if most of the values stored in the VarChar(MAX)
column are large and stored out-of-row, the data retrieval behavior will almost similar to a TEXT
type column.
If most of the values stored in VarChar(MAX)
type columns are small enough to store in-row, then retrieval of data where LOB columns are not included requires more data pages to be read, since the LOB column value is stored in-row in the same data page where the non-LOB column values are stored. But if the SELECT
query includes a LOB column, then it requires less pages to be read for the data retrieval compared to the TEXT
type columns.
Conclusion
Use VarChar(MAX)
data type rather than TEXT
for better performance.
Source
TEXT
andNTEXT
(andIMAGE
) are deprecated. – Workshop