SQLSERVER NULL和空字符串的区别 使用NULL是否节省空间
SQLSERVER NULL和空字符串的区别 使用NULL是否节省空间
这里只讨论字符串类型,int、datetime、text这些数据类型就不讨论了,因为是否节省空间是根据数据类型来定的
在写这篇文章之前,本人一直以为这个问题很简单的,看一下数据页就行了,但是后来写着写着,也修改了几次
发现需要对SQSERVER的数据页内容很熟悉您才能知道SQLSERVER内部空间占用是怎样的,希望大家在继续往下看之前先看一下下面文章
在往下看之前请各位先看一下下面的文章
char nchar varchar nvarchar的区别 :char nchar varchar nvarchar数据类型所占用长度
SQL Server误区30日谈-Day6-有关NULL位图的三个误区
如果不看上面的文章,对于刚入门的人来说可能只会是一知半解,为了文章的篇幅不要过长,我就在文章里不解释一些重要名词了
大家看一下给出的文章就可以了o(∩_∩)o
先建立下面表格并插入测试数据
1 USE [pratice] 2 GO 3 4 5 --允许空,varchar类型 6 CREATE TABLE testnullvarchar(id INT ,NAME VARCHAR(20) NULL) 7 GO 8 --允许空,char类型 9 CREATE TABLE testnullchar(id INT,NAME CHAR(20) NULL) 10 GO 11 --不允许空,varchar类型 12 CREATE TABLE testnotnullvarchar(id INT ,NAME VARCHAR(20) NOT NULL) 13 GO 14 --不允许空,char类型 15 CREATE TABLE testnotnullchar(id INT ,NAME CHAR(20) NOT NULL) 16 GO 17 18 --插入数据 19 INSERT INTO [dbo].[testnullvarchar] ( [id],[Name] ) 20 SELECT 1 ,NULL UNION ALL 21 SELECT 2,'你' 22 GO 23 24 INSERT INTO [dbo].[testnullchar] ( [id],[Name] ) 25 SELECT 1,NULL UNION ALL 26 SELECT 2,'你' 27 GO 28 29 INSERT INTO [dbo].[testnotnullchar] ( [id],[NAME] ) 30 SELECT 1,'' UNION ALL 31 SELECT 2,'你' 32 GO 33 34 INSERT INTO [dbo].[testnotnullvarchar] ( [id],[NAME] ) 35 SELECT 1,'' UNION ALL 36 SELECT 2,'你' 37 GO 38 39 SELECT * FROM testnullvarchar 40 SELECT * FROM testnullchar 41 SELECT * FROM testnotnullchar 42 SELECT * FROM testnotnullvarchar
建立一个DBCCResult表,保存DBCC IND的结果
1 CREATE TABLE DBCCResult ( 2 PageFID NVARCHAR(200), 3 PagePID NVARCHAR(200), 4 IAMFID NVARCHAR(200), 5 IAMPID NVARCHAR(200), 6 ObjectID NVARCHAR(200), 7 IndexID NVARCHAR(200), 8 PartitionNumber NVARCHAR(200), 9 PartitionID NVARCHAR(200), 10 iam_chain_type NVARCHAR(200), 11 PageType NVARCHAR(200), 12 IndexLevel NVARCHAR(200), 13 NextPageFID NVARCHAR(200), 14 NextPagePID NVARCHAR(200), 15 PrevPageFID NVARCHAR(200), 16 PrevPagePID NVARCHAR(200) 17 ) 18 GO
查看各张表的情况
VARCHAR类型的情况
testnullvarchar表
1 --TRUNCATE TABLE DBCCResult 2 INSERT INTO DBCCResult EXEC ('DBCC IND(pratice,testnullvarchar,-1) ') 3 4 SELECT * FROM [dbo].[DBCCResult] ORDER BY [PageType] DESC 5 6 7 DBCC TRACEON(3604,-1) 8 GO 9 DBCC PAGE([pratice],1,8370,3) 10 GO 11 12 13 14 SELECT LEN(name) FROM testnullvarchar WHERE [id]=1
数据页内容
1 DBCC 执行完毕。如果 DBCC 输出了错误信息,请与系统管理员联系。 2 3 PAGE: (1:8370) 4 5 6 BUFFER: 7 8 9 BUF @0x03CF4E64 10 11 bpage = 0x16F16000 bhash = 0x00000000 bpageno = (1:8370) 12 bdbid = 5 breferences = 0 bUse1 = 9390 13 bstat = 0x2c0000b blog = 0x32159bb bnext = 0x00000000 14 15 PAGE HEADER: 16 17 18 Page @0x16F16000 19 20 m_pageId = (1:8370) m_headerVersion = 1 m_type = 1 21 m_typeFlagBits = 0x4 m_level = 0 m_flagBits = 0x8000 22 m_objId (AllocUnitId.idObj) = 521 m_indexId (AllocUnitId.idInd) = 256 23 Metadata: AllocUnitId = 72057594072072192 24 Metadata: PartitionId = 72057594059882496 Metadata: IndexId = 0 25 Metadata: ObjectId = 1207675350 m_prevPage = (0:0) m_nextPage = (0:0) 26 pminlen = 8 m_slotCnt = 2 m_freeCnt = 8064 27 m_freeData = 124 m_reservedCnt = 0 m_lsn = (3045:22651:20) 28 m_xactReserved = 0 m_xdesId = (0:0) m_ghostRecCnt = 0 29 m_tornBits = 0 30 31 Allocation Status 32 33 GAM (1:2) = ALLOCATED SGAM (1:3) = ALLOCATED 34 PFS (1:8088) = 0x61 MIXED_EXT ALLOCATED 50_PCT_FULL DIFF (1:6) = CHANGED 35 ML (1:7) = NOT MIN_LOGGED 36 37 Slot 0 Offset 0x60 Length 11 38 39 Record Type = PRIMARY_RECORD Record Attributes = NULL_BITMAP 40 Memory Dump @0x0A16C060 41 42 00000000: 10000800 01000000 0200fe†††††††††††††........... 43 44 Slot 0 Column 0 Offset 0x4 Length 4 45 46 id = 1 47 NAME = [NULL] 48 49 Slot 1 Offset 0x6b Length 17 50 51 Record Type = PRIMARY_RECORD Record Attributes = NULL_BITMAP VARIABLE_COLUMNS 52 53 Memory Dump @0x0A16C06B 54 55 00000000: 30000800 02000000 0200fc01 001100c4 †0............... 56 00000010: e3†††††††††††††††††††††††††††††††††††. 57 58 Slot 1 Column 0 Offset 0x4 Length 4 59 60 id = 2 61 62 Slot 1 Column 1 Offset 0xf Length 2 63 64 NAME = 你 65 66 67 DBCC 执行完毕。如果 DBCC 输出了错误信息,请与系统管理员联系。
1 Slot 0 Offset 0x60 Length 11 2 3 Record Type = PRIMARY_RECORD Record Attributes = NULL_BITMAP 4 Memory Dump @0x0A16C060 5 6 00000000: 10000800 01000000 0200fe†††††††††††††........... 7 8 Slot 0 Column 0 Offset 0x4 Length 4 9 10 id = 1 11 NAME = [NULL] 12 13 Slot 1 Offset 0x6b Length 17 14 15 Record Type = PRIMARY_RECORD Record Attributes = NULL_BITMAP VARIABLE_COLUMNS 16 17 Memory Dump @0x0A16C06B 18 19 00000000: 30000800 02000000 0200fc01 001100c4 †0............... 20 00000010: e3†††††††††††††††††††††††††††††††††††. 21 22 Slot 1 Column 0 Offset 0x4 Length 4 23 24 id = 2 25 26 Slot 1 Column 1 Offset 0xf Length 2 27 28 NAME = 你
我们看第一行记录长度11怎麽得出来的
在SQL Server页中行物理存储里对数据行的各段进行了解释
2个字节行标头存储了状态A和状态B的信息(2 bytes row header)
2个字节存储固定长度大小,因为一行记录了有varchar这些不固定长度的数据类型(2 bytes for length of fixed length columns)
SQLSERVER需要知道int、datetime、decimal这些固定长度数据类型的大小
2个字节的列数,用来存储这个表一共有多少列(2 bytes for number of columns in the table)
1个字节的null bitmap,(1 byte for null bitmap)
4个字节存储int型数据(4 bytes for int (1st column))
2+2+2+1+4=11
换言之,第一行记录中name字段不占用任何空间,因为第一行记录中的name值为NULL
-------------------------------------------------------------------------------------------------------
我们看第二行记录长度17怎麽得出来的
2个字节行标头存储了状态A和状态B的信息(2 bytes row header)
2个字节存储固定长度大小,因为一行记录了有varchar这些不固定长度的数据类型(2 bytes for length of fixed length columns)
SQLSERVER需要知道int、datetime、decimal这些固定长度数据类型的大小
2个字节的列数,用来存储这个表一共有多少列(2 bytes for number of columns in the table)
1个字节的null bitmap,(1 byte for null bitmap)
4个字节存储int型数据(4 bytes for int (1st column))
2个字节存储数据行中的可变长度列数量,统计数据行中一共有多少列是nvarchar ,varchar类型的列( 2 bytes for number of variable length columns in the table)
2个字节存储可变长度偏移阵列,可变长度偏移阵列的公式
2*表格中可变长度数据类型的列数量,这个表只有一列varchar,所以2*1=2,为什麽要有可变长度偏移阵列?我估计是因为可变长度的数据类型
存储的数据是不固定的,所以要预留一些位置,当update varchar值的时候有足够的位置(2 bytes for name column offset)
2个字节存储name列的值,为什麽用两个字节大家可以看一下char nchar varchar nvarchar的区别 2 bytes for name (你)
2+2+2+1+4+2+2+2=17
前11个字节跟第一行记录是一样的长度,关键在于后面的6个字节,在这6个字节中只有2个字节实际存储数据的
为什麽在第一行记录里没有这4个字节呢?
2个字节存储数据行中的可变长度列数量
2个字节存储可变长度偏移阵列
想法:
我估计是因为,第一行记录中没有一个可变长度数据类型的列是有数据的,全部都是NULL,
既然这样SQLSERVER就没有必要再用4个字节去存储2个字节存储数据行中的可变长度列数量和2个字节存储可变长度偏移阵列
我们来验证一下这个想法:
代码如下:
1 USE [pratice] 2 GO 3 -- 4 CREATE TABLE testnullandnotnullvarchar(id INT ,NAME1 VARCHAR(20) NULL,NAME2 VARCHAR(20) NULL) 5 GO 6 7 --插入数据 8 INSERT INTO [dbo].[testnullandnotnullvarchar] ( [id],[Name1],[NAME2] ) 9 SELECT 1 ,NULL,'你' 10 GO 11 12 SELECT * FROM testnullandnotnullvarchar 13 ----------------------------------------------------------- 14 --TRUNCATE TABLE DBCCResult 15 INSERT INTO DBCCResult EXEC ('DBCC IND(pratice,testnullandnotnullvarchar,-1) ') 16 17 SELECT * FROM [dbo].[DBCCResult] ORDER BY [PageType] DESC 18 19 20 DBCC TRACEON(3604,-1) 21 GO 22 DBCC PAGE([pratice],1,15656,3) 23 GO
数据页内容
1 DBCC 执行完毕。如果 DBCC 输出了错误信息,请与系统管理员联系。 2 3 PAGE: (1:15656) 4 5 6 BUFFER: 7 8 9 BUF @0x03D42838 10 11 bpage = 0x196A6000 bhash = 0x00000000 bpageno = (1:15656) 12 bdbid = 5 breferences = 0 bUse1 = 32718 13 bstat = 0xc0000b blog = 0xbbbbbbbb bnext = 0x00000000 14 15 PAGE HEADER: 16 17 18 Page @0x196A6000 19 20 m_pageId = (1:15656) m_headerVersion = 1 m_type = 1 21 m_typeFlagBits = 0x4 m_level = 0 m_flagBits = 0x8000 22 m_objId (AllocUnitId.idObj) = 531 m_indexId (AllocUnitId.idInd) = 256 23 Metadata: AllocUnitId = 72057594072727552 24 Metadata: PartitionId = 72057594060537856 Metadata: IndexId = 0 25 Metadata: ObjectId = 1383675977 m_prevPage = (0:0) m_nextPage = (0:0) 26 pminlen = 8 m_slotCnt = 1 m_freeCnt = 8075 27 m_freeData = 115 m_reservedCnt = 0 m_lsn = (3045:22996:18) 28 m_xactReserved = 0 m_xdesId = (0:0) m_ghostRecCnt = 0 29 m_tornBits = 0 30 31 Allocation Status 32 33 GAM (1:2) = ALLOCATED SGAM (1:3) = ALLOCATED 34 PFS (1:8088) = 0x61 MIXED_EXT ALLOCATED 50_PCT_FULL DIFF (1:6) = CHANGED 35 ML (1:7) = NOT MIN_LOGGED 36 37 Slot 0 Offset 0x60 Length 19 38 39 Record Type = PRIMARY_RECORD Record Attributes = NULL_BITMAP VARIABLE_COLUMNS 40 41 Memory Dump @0x0855C060 42 43 00000000: 30000800 01000000 0300fa02 00110013 †0............... 44 00000010: 00c4e3†††††††††††††††††††††††††††††††... 45 46 Slot 0 Column 0 Offset 0x4 Length 4 47 48 id = 1 49 NAME1 = [NULL] 50 51 Slot 0 Column 2 Offset 0x11 Length 2 52 53 NAME2 = 你 54 55 56 DBCC 执行完毕。如果 DBCC 输出了错误信息,请与系统管理员联系。
我们看第一行记录长度19怎麽得出来的
2个字节行标头存储了状态A和状态B的信息
2个字节存储固定长度大小
2个字节的列数
1个字节的null bitmap
4个字节存储int型数据
2个字节存储数据行中的可变长度列数量
4个字节存储可变长度偏移阵列 2*2=4
2个字节存储name列的值
2+2+2+1+4+2+4+2=19
也就是说,一行记录中全部的可变长度数据列的数据全部为NULL,才不会有这4个字节
2个字节存储数据行中的可变长度列数量
2个字节存储可变长度偏移阵列
其实SQLSERVER也做了一下标记,区分开一行记录中全部的可变长度类型列的数据全部为NULL还是一些为NULL一些不为NULL,还是全部不为NULL
这里可以在行记录属性中看出,testnullvarchar表的第一行和第二行
第一行NULL_BITMAP表明一行记录中全部的可变长度类型列的数据全部为NULL
第二行NULL_BITMAP VARIABLE_COLUMNS表明一些为NULL一些不为NULL或者全部不为NULL
小结:
VARCHAR类型NULL值不占用任何空间
testnotnullvarchar表
1 --TRUNCATE TABLE DBCCResult 2 INSERT INTO DBCCResult EXEC ('DBCC IND(pratice,testnotnullvarchar,-1) ') 3 4 SELECT * FROM [dbo].[DBCCResult] ORDER BY [PageType] DESC 5 6 7 DBCC TRACEON(3604,-1) 8 GO 9 DBCC PAGE([pratice],1,14495,3) 10 GO 11 12 13 SELECT LEN(name) FROM testnotnullvarchar WHERE [id]=1
数据页内容
1 DBCC 执行完毕。如果 DBCC 输出了错误信息,请与系统管理员联系。 2 3 PAGE: (1:14495) 4 5 6 BUFFER: 7 8 9 BUF @0x03D4258C 10 11 bpage = 0x196D2000 bhash = 0x00000000 bpageno = (1:14495) 12 bdbid = 5 breferences = 0 bUse1 = 38447 13 bstat = 0xc0000b blog = 0xbbbbbbbb bnext = 0x00000000 14 15 PAGE HEADER: 16 17 18 Page @0x196D2000 19 20 m_pageId = (1:14495) m_headerVersion = 1 m_type = 1 21 m_typeFlagBits = 0x4 m_level = 0 m_flagBits = 0x8000 22 m_objId (AllocUnitId.idObj) = 532 m_indexId (AllocUnitId.idInd) = 256 23 Metadata: AllocUnitId = 72057594072793088 24 Metadata: PartitionId = 72057594060603392 Metadata: IndexId = 0 25 Metadata: ObjectId = 1399676034 m_prevPage = (0:0) m_nextPage = (0:0) 26 pminlen = 8 m_slotCnt = 2 m_freeCnt = 8064 27 m_freeData = 124 m_reservedCnt = 0 m_lsn = (3045:23036:20) 28 m_xactReserved = 0 m_xdesId = (0:0) m_ghostRecCnt = 0 29 m_tornBits = 0 30 31 Allocation Status 32 33 GAM (1:2) = ALLOCATED SGAM (1:3) = ALLOCATED 34 PFS (1:8088) = 0x61 MIXED_EXT ALLOCATED 50_PCT_FULL DIFF (1:6) = CHANGED 35 ML (1:7) = NOT MIN_LOGGED 36 37 Slot 0 Offset 0x60 Length 11 38 39 Record Type = PRIMARY_RECORD Record Attributes = NULL_BITMAP 40 Memory Dump @0x0855C060 41 42 00000000: 10000800 01000000 0200fc†††††††††††††........... 43 44 Slot 0 Column 0 Offset 0x4 Length 4 45 46 id = 1 47 NAME = [NULL] 48 49 Slot 1 Offset 0x6b Length 17 50 51 Record Type = PRIMARY_RECORD Record Attributes = NULL_BITMAP VARIABLE_COLUMNS 52 53 Memory Dump @0x0855C06B 54 55 00000000: 30000800 02000000 0200fc01 001100c4 †0............... 56 00000010: e3†††††††††††††††††††††††††††††††††††. 57 58 Slot 1 Column 0 Offset 0x4 Length 4 59 60 id = 2 61 62 Slot 1 Column 1 Offset 0xf Length 2 63 64 NAME = 你 65 66 67 DBCC 执行完毕。如果 DBCC 输出了错误信息,请与系统管理员联系。
1 Slot 0 Offset 0x60 Length 11 2 3 Record Type = PRIMARY_RECORD Record Attributes = NULL_BITMAP 4 Memory Dump @0x0855C060 5 6 00000000: 10000800 01000000 0200fc†††††††††††††........... 7 8 Slot 0 Column 0 Offset 0x4 Length 4 9 10 id = 1 11 NAME = [NULL] 12 13 Slot 1 Offset 0x6b Length 17 14 15 Record Type = PRIMARY_RECORD Record Attributes = NULL_BITMAP VARIABLE_COLUMNS 16 17 Memory Dump @0x0855C06B 18 19 00000000: 30000800 02000000 0200fc01 001100c4 †0............... 20 00000010: e3†††††††††††††††††††††††††††††††††††. 21 22 Slot 1 Column 0 Offset 0x4 Length 4 23 24 id = 2 25 26 Slot 1 Column 1 Offset 0xf Length 2 27 28 NAME = 你
testnotnullvarchar表的数据页和testnullvarchar表的数据页对比一下
看到上面的对比图我也不再对testnotnullvarchar表做详细分析了
情况跟testnullvarchar表是一样的
只有一个地方不一样,就是LEN()函数
小结:
对于varchar数据类型,无论是空字符串还是NULL值都不占用任何空间
CHAR类型的情况
testnullchar表
1 --TRUNCATE TABLE DBCCResult 2 INSERT INTO DBCCResult EXEC ('DBCC IND(pratice,testnullchar,-1) ') 3 4 SELECT * FROM [dbo].[DBCCResult] ORDER BY [PageType] DESC 5 6 7 DBCC TRACEON(3604,-1) 8 GO 9 DBCC PAGE([pratice],1,8353,3) 10 GO 11 12 13 14 SELECT LEN(name) FROM testnullchar WHERE [id]=1
数据页内容
1 DBCC 执行完毕。如果 DBCC 输出了错误信息,请与系统管理员联系。 2 3 PAGE: (1:8353) 4 5 6 BUFFER: 7 8 9 BUF @0x03CF68D0 10 11 bpage = 0x16FA8000 bhash = 0x00000000 bpageno = (1:8353) 12 bdbid = 5 breferences = 0 bUse1 = 39861 13 bstat = 0x2c0000b blog = 0x59bbbbbb bnext = 0x00000000 14 15 PAGE HEADER: 16 17 18 Page @0x16FA8000 19 20 m_pageId = (1:8353) m_headerVersion = 1 m_type = 1 21 m_typeFlagBits = 0x4 m_level = 0 m_flagBits = 0x8000 22 m_objId (AllocUnitId.idObj) = 533 m_indexId (AllocUnitId.idInd) = 256 23 Metadata: AllocUnitId = 72057594072858624 24 Metadata: PartitionId = 72057594060668928 Metadata: IndexId = 0 25 Metadata: ObjectId = 1415676091 m_prevPage = (0:0) m_nextPage = (0:0) 26 pminlen = 28 m_slotCnt = 2 m_freeCnt = 8030 27 m_freeData = 158 m_reservedCnt = 0 m_lsn = (3045:23074:20) 28 m_xactReserved = 0 m_xdesId = (0:0) m_ghostRecCnt = 0 29 m_tornBits = 0 30 31 Allocation Status 32 33 GAM (1:2) = ALLOCATED SGAM (1:3) = ALLOCATED 34 PFS (1:8088) = 0x61 MIXED_EXT ALLOCATED 50_PCT_FULL DIFF (1:6) = CHANGED 35 ML (1:7) = NOT MIN_LOGGED 36 37 Slot 0 Offset 0x60 Length 31 38 39 Record Type = PRIMARY_RECORD Record Attributes = NULL_BITMAP 40 Memory Dump @0x0A27C060 41 42 00000000: 10001c00 01000000 00000000 00000000 †................ 43 00000010: 00000000 00000000 00000000 0200fe††††............... 44 45 Slot 0 Column 0 Offset 0x4 Length 4 46 47 id = 1 48 NAME = [NULL] 49 50 Slot 1 Offset 0x7f Length 31 51 52 Record Type = PRIMARY_RECORD Record Attributes = NULL_BITMAP 53 Memory Dump @0x0A27C07F 54 55 00000000: 10001c00 02000000 c4e32020 20202020 †.......... 56 00000010: 20202020 20202020 20202020 0200fc†††† ... 57 58 Slot 1 Column 0 Offset 0x4 Length 4 59 60 id = 2 61 62 Slot 1 Column 1 Offset 0x8 Length 20 63 64 NAME = 你 65 66 67 DBCC 执行完毕。如果 DBCC 输出了错误信息,请与系统管理员联系。
1 Slot 0 Offset 0x60 Length 31 2 3 Record Type = PRIMARY_RECORD Record Attributes = NULL_BITMAP 4 Memory Dump @0x0A27C060 5 6 00000000: 10001c00 01000000 00000000 00000000 †................ 7 00000010: 00000000 00000000 00000000 0200fe††††............... 8 9 Slot 0 Column 0 Offset 0x4 Length 4 10 11 id = 1 12 NAME = [NULL] 13 14 Slot 1 Offset 0x7f Length 31 15 16 Record Type = PRIMARY_RECORD Record Attributes = NULL_BITMAP 17 Memory Dump @0x0A27C07F 18 19 00000000: 10001c00 02000000 c4e32020 20202020 †.......... 20 00000010: 20202020 20202020 20202020 0200fc†††† ... 21 22 Slot 1 Column 0 Offset 0x4 Length 4 23 24 id = 2 25 26 Slot 1 Column 1 Offset 0x8 Length 20 27 28 NAME = 你
我们看第一行记录长度31怎麽得出来的
2个字节行标头存储了状态A和状态B的信息(2 bytes row header)
2个字节存储固定长度大小,因为一行记录了有varchar这些不固定长度的数据类型(2 bytes for length of fixed length columns)
4个字节存储int型数据(4 bytes for int (1st column))
2个字节的列数,用来存储这个表一共有多少列(2 bytes for number of columns in the table)
1个字节的null bitmap,(1 byte for null bitmap)
20个字节存储name列的值,为什麽用20个字节大家可以看一下char nchar varchar nvarchar的区别 20 bytes for char(20) (2nd column)
2+2+4+2+1+20=31
换言之,第一行记录中name字段是否为NULL,都占用20个字节的空间
-----------------------------------------------------------------------------------------
我们看第二行记录长度31怎麽得出来的
实际上第二行记录和第一行记录是一样的,只不过第二行记录里的name列存储了实际的值“你”,
而不管存储的值大小如何都占用20个字节
2个字节行标头存储了状态A和状态B的信息(2 bytes row header)
2个字节存储固定长度大小,因为一行记录了有varchar这些不固定长度的数据类型(2 bytes for length of fixed length columns)
4个字节存储int型数据(4 bytes for int (1st column))
2个字节的列数,用来存储这个表一共有多少列(2 bytes for number of columns in the table)
1个字节的null bitmap,(1 byte for null bitmap)
20个字节存储name列的值,为什麽用20个字节大家可以看一下char nchar varchar nvarchar的区别 20 bytes for char(20) (2nd column)
2+2+4+2+1+20=31
小结:
CHAR类型NULL值会占用空间,所占用空间大小取决于建表时候指定的char数据类型的大小
例如:
1 --允许空,char类型 2 CREATE TABLE testnullchar(id INT,NAME CHAR(20) NULL) 3 GO
指定char为20,那么就占用20个字节的空间
testnotnullchar表
1 --TRUNCATE TABLE DBCCResult 2 INSERT INTO DBCCResult EXEC ('DBCC IND(pratice,testnotnullchar,-1) ') 3 4 SELECT * FROM [dbo].[DBCCResult] ORDER BY [PageType] DESC 5 6 7 DBCC TRACEON(3604,-1) 8 GO 9 DBCC PAGE([pratice],1,37266,3) 10 GO 11 12 13 14 SELECT LEN(name) FROM testnotnullchar WHERE [id]=1
数据页内容
1 DBCC 执行完毕。如果 DBCC 输出了错误信息,请与系统管理员联系。 2 3 PAGE: (1:37266) 4 5 6 BUFFER: 7 8 9 BUF @0x03D669F0 10 11 bpage = 0x1A500000 bhash = 0x00000000 bpageno = (1:37266) 12 bdbid = 5 breferences = 0 bUse1 = 42107 13 bstat = 0xc0000b blog = 0x9bbbbbbb bnext = 0x00000000 14 15 PAGE HEADER: 16 17 18 Page @0x1A500000 19 20 m_pageId = (1:37266) m_headerVersion = 1 m_type = 1 21 m_typeFlagBits = 0x4 m_level = 0 m_flagBits = 0x8000 22 m_objId (AllocUnitId.idObj) = 534 m_indexId (AllocUnitId.idInd) = 256 23 Metadata: AllocUnitId = 72057594072924160 24 Metadata: PartitionId = 72057594060734464 Metadata: IndexId = 0 25 Metadata: ObjectId = 1431676148 m_prevPage = (0:0) m_nextPage = (0:0) 26 pminlen = 28 m_slotCnt = 2 m_freeCnt = 8030 27 m_freeData = 158 m_reservedCnt = 0 m_lsn = (3045:23137:20) 28 m_xactReserved = 0 m_xdesId = (0:0) m_ghostRecCnt = 0 29 m_tornBits = 0 30 31 Allocation Status 32 33 GAM (1:2) = ALLOCATED SGAM (1:3) = ALLOCATED 34 PFS (1:32352) = 0x61 MIXED_EXT ALLOCATED 50_PCT_FULL DIFF (1:6) = CHANGED 35 ML (1:7) = NOT MIN_LOGGED 36 37 Slot 0 Offset 0x60 Length 31 38 39 Record Type = PRIMARY_RECORD Record Attributes = NULL_BITMAP 40 Memory Dump @0x0A01C060 41 42 00000000: 10001c00 01000000 20202020 20202020 †........ 43 00000010: 20202020 20202020 20202020 0200fc†††† ... 44 45 Slot 0 Column 0 Offset 0x4 Length 4 46 47 id = 1 48 49 Slot 0 Column 1 Offset 0x8 Length 20 50 51 NAME = 52 53 Slot 1 Offset 0x7f Length 31 54 55 Record Type = PRIMARY_RECORD Record Attributes = NULL_BITMAP 56 Memory Dump @0x0A01C07F 57 58 00000000: 10001c00 02000000 c4e32020 20202020 †.......... 59 00000010: 20202020 20202020 20202020 0200fc†††† ... 60 61 Slot 1 Column 0 Offset 0x4 Length 4 62 63 id = 2 64 65 Slot 1 Column 1 Offset 0x8 Length 20 66 67 NAME = 你 68 69 70 DBCC 执行完毕。如果 DBCC 输出了错误信息,请与系统管理员联系。
1 Slot 0 Offset 0x60 Length 31 2 3 Record Type = PRIMARY_RECORD Record Attributes = NULL_BITMAP 4 Memory Dump @0x0A01C060 5 6 00000000: 10001c00 01000000 20202020 20202020 †........ 7 00000010: 20202020 20202020 20202020 0200fc†††† ... 8 9 Slot 0 Column 0 Offset 0x4 Length 4 10 11 id = 1 12 13 Slot 0 Column 1 Offset 0x8 Length 20 14 15 NAME = 16 17 Slot 1 Offset 0x7f Length 31 18 19 Record Type = PRIMARY_RECORD Record Attributes = NULL_BITMAP 20 Memory Dump @0x0A01C07F 21 22 00000000: 10001c00 02000000 c4e32020 20202020 †.......... 23 00000010: 20202020 20202020 20202020 0200fc†††† ... 24 25 Slot 1 Column 0 Offset 0x4 Length 4 26 27 id = 2 28 29 Slot 1 Column 1 Offset 0x8 Length 20 30 31 NAME = 你
testnotnullchar表的数据页和testnullchar表的数据页对比一下
看到上面的对比图我也不再对testnotnullchar表做详细分析了
情况跟testnullchar表是一样的
只有两个地方不一样,testnotnullchar表的第一行记录的name字段存储的是空字符串
而testnullchar表的第一行记录的name字段存储的是NULL
不过无论是空字符串还是NULL都占用了31个字节
LEN()函数返回的值不一样,这里跟varchar类型的情况也是一样的
跟varchar类型不一样的是,testnotnullchar表的第一行记录的name字段存储的是空字符串,而testnullchar表的第一行记录的name字段存储的是NULL
varchar情况,testnotnullvarchar表和testnullvarchar表的第一行记录的name字段存储的都是NULL
而奇怪的是testnotnullvarchar表返回的不是NULL值,而是空字符串
小结:
对于char数据类型,无论是空字符串还是NULL值都占用空间,所占用空间大小取决于建表时候指定的char数据类型的大小
例如:
1 --允许空,char类型 2 CREATE TABLE testnullchar(id INT,NAME CHAR(20) NULL) 3 GO
指定char为20,那么就占用20个字节的空间
总结
对于varchar数据类型,无论是空字符串还是NULL值都不占用任何空间
对于char数据类型,无论是空字符串还是NULL值都占用空间,所占用空间大小取决于建表时候指定的char数据类型的大小
从上面的实验来看,是否节省空间是根据数据类型来决定的而不是是否是NULL还是空字符串
撇开数据类型来比较是没有意义的,就像DATETIME数据类型的数据列填入NULL值和VARCHAR数据类型的数据列填入NULL值,
两个NULL值进行比较,哪一个大?如果不对两种数据类型进行分析,单独比较这两个NULL值,这种比较是没有意义的
而且也不平等,因为这两种数据类型一点关系都没有,一个datetime类型,一个是varchar类型
而char和varchar也是一样
只有同一种数据类型的比较才有意义,就像同样都是varchar数据类型,空字符串和NULL值进行比较
同样都是char数据类型,空字符串和NULL值进行比较
所以平时要对SQLSERVER中的数据类型要有一定认识,才能对系统中的表空间的使用情况有大概的掌握
如有不对的地方,欢迎大家拍砖o(∩_∩)o