Question

After I changed the varchar limit on a column from 512 to max, in SQL Server 2008 R2, I noticed the file size of the table had more than doubled. I didn't expect it to increase this much, but since I was increasing it I thought it made sense. So I tried changing it to a more reasonable limit of 2500 only to find out that the size of my table again more than doubled in file size (now over 4x the original).

The only index is the Primary Key, and this is the script I ran:

ALTER TABLE dbo.Notes
ALTER COLUMN [note] VARCHAR(MAX) NULL

What is it that I'm not understanding?

Était-ce utile?

La solution

For the same dataset, a varchar(max) should not take more space than a varchar(2500).

Perhaps SQL Server needed temporary space while altering the table? Try to rebuild your table (I'm assuming you primary key is clustered):

alter index NameOfPrimaryKey on dbo.Notes rebuild

And check the table data usage again after that.

Autres conseils

varchar(max) is not stored within the row. Its stored in separate location. The original row has a pointer to the varchar(max) data location. So it is natural to expect the table size to grow if you changed the data type from varchar(512) to varchar(max).

If you want to keep data type varchar(max) and avoid the data storage in separate file then try a config which keeps a varchars of certain length inline with rows.

All above explanation doesn't explain table size getting 4x so please rebuild your primary index as suggested above.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top