SQL Server Space Taken By Empty Strings? -


what penalty in space taken varchar types in sql server 2012 , above? is, if have 3 columns of type nvarchar (or varchar matter), , empty, how space taken in table?

create table dbo.referringurl  (      id int identity(1, 1) not null,      requesturl nvarchar(384) collate sql_latin1_general_cp1_ci_as null,      referringurlname nvarchar(384) collate sql_latin1_general_cp1_ci_as null,      referringipaddress nvarchar(64) collate sql_latin1_general_cp1_ci_as null, ) 

you can test out dbcc page or sql server internals viewer.

some example cases below illustrate following points.

  1. a varchar column null or empty takes no space @ except 2 bytes in column offset array (rows 4 , 5).
  2. if variable length column null or empty , followed other null/empty values none of these trailing empty columns need 2 bytes in column offset array (rows 6 , 7).
  3. if variable length columns null or empty row has no variable length section @ (rows 2 , 3).

insert dbo.referringurl        (id,         requesturl,         referringurlname,         referringipaddress) values (1,n'aaa','bbb','ccc'),        (2,null,null,null),        (3,'','',''),        (4,null,null,'ccc'),        (5,'','','ccc'),        (6,'aaa',null,null),        (7,'aaa','',''),        (8,n'aaa','bbb','ccc')  

1 - columns have values

enter image description here

the image above sql server internals viewer - helpfully provides key various components in row shown below.

enter image description here

2 - varchar columns null

enter image description here

3 - varchar columns have empty strings

enter image description here

4 - varchar columns null except final one

enter image description here

5 - varchar columns empty except final one

enter image description here

6 - varchar columns null except first one

enter image description here

7 - varchar columns empty except first one

(due bug in internals viewer not highlighting notice same length previous row)

enter image description here


Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -