Question

If I declare a column as nvarchar(max), I understand that this will allocate 2Gb of space, but does it actually take the 2Gb, of disk space, straight away once I save the changes to the table? Or, is it that it makes note that this column will allow 2Gb of data to be populated in the column?

Was it helpful?

Solution

As I understand it, space isn't allocated until it is needed.

Try the following queries:

CREATE TABLE SizeTest (

    MyID int primary key
)

INSERT INTO SizeTest SELECT 1
UNION SELECT 2 
UNION SELECT 3
UNION SELECT 4
UNION SELECT 5

EXEC sp_spaceused 'SizeTest'

ALTER TABLE SizeTest ADD MyBigText nvarchar(max)

EXEC sp_spaceused 'SizeTest'

UPDATE SizeTest SET MyBigText = 'This is big text' WHERE MyID = 1

EXEC sp_spaceused 'SizeTest'

DROP TABLE SizeTest

By executing this statement, you should get the following for all three sp_spaceused calls:

name        rows      reserved  data  index_size unused
SizeTest    5           16 KB   8 KB        8 KB    0 KB

At no point is the 2GB allocated.

OTHER TIPS

Will allow 2Gb of data :) I think so.. It allows until 2Gb but it will not allocate this space straight away

The maximum storage size for VARCHAR(MAX) is 2^31-1 bytes (2,147,483,647 bytes or 2GB - 1 bytes). The storage size is the actual length of data entered + 2 bytes. The data entered can be 0 characters in length. Since each character in a VARCHAR data type uses one byte, the maximum length for a VARCHAR(MAX) data type is 2,147,483,645.

The maximum storage size for NVARCHAR(MAX) is also 2^31-1 bytes (2,147,483,647 bytes or 2GB - 1 bytes). The storage size, in bytes, is two times the number of characters entered + 2 bytes. The data entered can be 0 characters in length. Since each Unicode character in an NVARCHAR data type uses two bytes, the maximum length for an NVARCHAR(MAX) data type is 1,073,741,822.

The maximum storage size for VARBINARY(MAX) is the same as the maximum storage size for VARCHAR(MAX) and NVARCHAR(MAX), which is 2^31-1 (2,147,483,647 bytes or 2GB - 1 bytes). The storage size is the actual length of the data entered + 2 bytes. The data that is entered can be 0 bytes in length.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top