However, it is required to create an Index on the tables to improve the performance of the query. Indexes on VARCHAR(N) and VARCHAR(MAX) columnsĪs a DBA, you might not design the table. In the following screenshot, you can note the following.Įmployee_varchar_2000 insertion time 0.08 SecondsĮmployee_varchar_4500 insertion time 0.19 SecondsĮmployee_varchar_8000 insertion time 0.31 SecondsĮmployee_varchar_Max insertion time 2.72 Seconds You can use the ApexSQL Generate tool to insert the data without writing the t-SQL code for it. We want to check the data insertion time. Let’s insert 10,000 records into each of the tables we created earlier. Performance comparison between varchar(max) and varchar(n) data type If the data grows beyond the 8000 bytes, SQL Server uses LOB_DATA page for the varchar(max) data type SQL Server uses the IN_ROW_DATA page for the varchar(max) data type if the data is less than or equal to 8000 bytes. We can get the following conclusion from this. We have a pointer to this data in the IN_Row_DATA allocation unit. We get the LOB_Data allocation unit to store the data more than 8000 bytes in the Employee_Varchar_Max table. Rerun the query to check the allocation unit. It successfully inserts records in the Employee_varchar_max table. SELECT REPLICATE ( CONVERT ( VARCHAR ( max ), 'x' ), 8001 ) We will also create a table with a varchar(max) data type.
Let’s create a few sample tables with different size in varchar data type. You might think of using the varchar(max) data type to store 2 GB data to resolve the string truncation issues. Due to this, we can store up to 8000 bytes of data using varchar (8000) data type.
Apart from this, page also contains row overhead and row offset and leaves 8000 bytes to use for data storage. We can store 8096 bytes ( 8192-96 bytes) for data in SQL Server. On a page, SQL Server uses 96 bytes for the page header. The page size is 8 KB (8192 byes) in SQL Server, and it is fixed. As you might be aware that the basic unit of storage in SQL Server is a page. All these data types can store data up to 2 GB. It replaces the large blob object Text, NText and Image data types. The SQL Server 2005 introduced this varchar(max) data type. Overview of the VARCHAR(max) SQL Server Data Type We will discuss the use of varchar max and its implications, comparison with the varchar (n) data type in this article. In order to gain understanding about this data type, read SQL varchar(n) article. We can define a specific range for the varchar (n) data type, and it is the recommended way to do so. Is it a good practice to use varchar(max) for each usage? We might not be sure about the data length, or we want to eliminate the string or binary truncation error. I have seen that SQL developers use varchar(max) data while designing the tables or temporary tables.