January 23, 2009 at 7:02 am
I'm trying to find out how much space is used in a varchar(MAX) when a NULL value is stored in it. How different is it to storing a zero length string?
_________________________________________________________________________________SQLGeordieWeb:- Jarrin ConsultancyBlog:- www.chrisjarrintaylor.co.ukTwitter:- @SQLGeordie
January 24, 2009 at 6:17 am
Storing NULL value does not take any additional storage space.
Storage of a value in a variable length column (VARCHAR/NVARCHAR etc) takes the number of bytes needed to store the actual value plus a couple of bytes to store the length of the string internally. So I think, even an empty string (zero length) will also be using some storage space.
.
January 24, 2009 at 7:23 am
thanks for the reply. If that is the case then why is there the "Sparse" column feature in 2008? If NULL takes up not space at all then surely there is no need for this feature...?
The reason i'm asking this question was because a while ago i read somewhere about NULL's taking up the full storage of a varchar set becasue SQL cannot determine what size it is so for example a varchar(255) with a NULL value in it would take 255bytes? Now i've tested this and the results obviously don't match what i read but i cannot find the site/thread that i was reading.....
_________________________________________________________________________________SQLGeordieWeb:- Jarrin ConsultancyBlog:- www.chrisjarrintaylor.co.ukTwitter:- @SQLGeordie
January 24, 2009 at 7:32 am
SQL Server uses a NULL bitmap to indicate which columns are NULL. So when the value is NULL, SQL Sever does not really need to store anything, instead just set the specific BIT to indicate the value is NULL.
See this article
http://blogs.msdn.com/sqlserverstorageengine/archive/2006/06/29/650349.aspx
.
January 24, 2009 at 7:37 am
Swirl80 (1/24/2009)
thanks for the reply. If that is the case then why is there the "Sparse" column feature in 2008? If NULL takes up not space at all then surely there is no need for this feature...?
The sparse column feature helps in a number of different ways. The maximum number of columns a table can have is 1024. One of the advantages of using sparse columns is that, you can have upto 30,000 columns in a table.
.
January 24, 2009 at 9:15 am
jacob sebastian (1/24/2009)
Swirl80 (1/24/2009)
thanks for the reply. If that is the case then why is there the "Sparse" column feature in 2008? If NULL takes up not space at all then surely there is no need for this feature...?The sparse column feature helps in a number of different ways. The maximum number of columns a table can have is 1024. One of the advantages of using sparse columns is that, you can have upto 30,000 columns in a table.
Heh... sure... but if you do that, I'm thinking you have bigger problems with your database than worrying about NULLs... MUCH bigger problems. 😛
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply