SQLSERVER NULL和空字符串的区别 使用NULL是否节省空间
这里只讨论字符串类型,int、datetime、text这些数据类型就不讨论了,因为是否节省空间是根据数据类型来定的
在写这篇文章之前,本人一直以为这个问题很简单的,看一下数据页就行了,但是后来写着写着,也修改了几次
发现需要对SQSERVER的数据页内容很熟悉您才能知道SQLSERVER内部空间占用是怎样的,希望大家在继续往下看之前先看一下下面文章
在往下看之前请各位先看一下下面的文章
char nchar varchar nvarchar的区别 :char nchar varchar nvarchar数据类型所占用长度
SQL Server误区30日谈-Day6-有关NULL位图的三个误区
如果不看上面的文章,对于刚入门的人来说可能只会是一知半解,为了文章的篇幅不要过长,我就在文章里不解释一些重要名词了
大家看一下给出的文章就可以了o(∩_∩)o
先建立下面表格并插入测试数据
USE [pratice]
GO --允许空,varchar类型
CREATE TABLE testnullvarchar(id INT ,NAME VARCHAR(20) NULL)
GO
--允许空,char类型
CREATE TABLE testnullchar(id INT,NAME CHAR(20) NULL)
GO
--不允许空,varchar类型
CREATE TABLE testnotnullvarchar(id INT ,NAME VARCHAR(20) NOT NULL)
GO
--不允许空,char类型
CREATE TABLE testnotnullchar(id INT ,NAME CHAR(20) NOT NULL)
GO --插入数据
INSERT INTO [dbo].[testnullvarchar] ( [id],[Name] )
SELECT 1 ,NULL UNION ALL
SELECT 2,'你'
GO INSERT INTO [dbo].[testnullchar] ( [id],[Name] )
SELECT 1,NULL UNION ALL
SELECT 2,'你'
GO INSERT INTO [dbo].[testnotnullchar] ( [id],[NAME] )
SELECT 1,'' UNION ALL
SELECT 2,'你'
GO INSERT INTO [dbo].[testnotnullvarchar] ( [id],[NAME] )
SELECT 1,'' UNION ALL
SELECT 2,'你'
GO SELECT * FROM testnullvarchar
SELECT * FROM testnullchar
SELECT * FROM testnotnullchar
SELECT * FROM testnotnullvarchar
建立一个DBCCResult表,保存DBCC IND的结果
CREATE TABLE DBCCResult (
PageFID NVARCHAR(200),
PagePID NVARCHAR(200),
IAMFID NVARCHAR(200),
IAMPID NVARCHAR(200),
ObjectID NVARCHAR(200),
IndexID NVARCHAR(200),
PartitionNumber NVARCHAR(200),
PartitionID NVARCHAR(200),
iam_chain_type NVARCHAR(200),
PageType NVARCHAR(200),
IndexLevel NVARCHAR(200),
NextPageFID NVARCHAR(200),
NextPagePID NVARCHAR(200),
PrevPageFID NVARCHAR(200),
PrevPagePID NVARCHAR(200)
)
GO
查看各张表的情况
VARCHAR类型的情况
testnullvarchar表
--TRUNCATE TABLE DBCCResult
INSERT INTO DBCCResult EXEC ('DBCC IND(pratice,testnullvarchar,-1) ') SELECT * FROM [dbo].[DBCCResult] ORDER BY [PageType] DESC DBCC TRACEON(3604,-1)
GO
DBCC PAGE([pratice],1,8370,3)
GO SELECT LEN(name) FROM testnullvarchar WHERE [id]=1
数据页内容
DBCC 执行完毕。如果 DBCC 输出了错误信息,请与系统管理员联系。 PAGE: (1:8370) BUFFER: BUF @0x03CF4E64 bpage = 0x16F16000 bhash = 0x00000000 bpageno = (1:8370)
bdbid = 5 breferences = 0 bUse1 = 9390
bstat = 0x2c0000b blog = 0x32159bb bnext = 0x00000000 PAGE HEADER: Page @0x16F16000 m_pageId = (1:8370) m_headerVersion = 1 m_type = 1
m_typeFlagBits = 0x4 m_level = 0 m_flagBits = 0x8000
m_objId (AllocUnitId.idObj) = 521 m_indexId (AllocUnitId.idInd) = 256
Metadata: AllocUnitId = 72057594072072192
Metadata: PartitionId = 72057594059882496 Metadata: IndexId = 0
Metadata: ObjectId = 1207675350 m_prevPage = (0:0) m_nextPage = (0:0)
pminlen = 8 m_slotCnt = 2 m_freeCnt = 8064
m_freeData = 124 m_reservedCnt = 0 m_lsn = (3045:22651:20)
m_xactReserved = 0 m_xdesId = (0:0) m_ghostRecCnt = 0
m_tornBits = 0 Allocation Status GAM (1:2) = ALLOCATED SGAM (1:3) = ALLOCATED
PFS (1:8088) = 0x61 MIXED_EXT ALLOCATED 50_PCT_FULL DIFF (1:6) = CHANGED
ML (1:7) = NOT MIN_LOGGED Slot 0 Offset 0x60 Length 11 Record Type = PRIMARY_RECORD Record Attributes = NULL_BITMAP
Memory Dump @0x0A16C060 00000000: 10000800 01000000 0200fe†††††††††††††........... Slot 0 Column 0 Offset 0x4 Length 4 id = 1
NAME = [NULL] Slot 1 Offset 0x6b Length 17 Record Type = PRIMARY_RECORD Record Attributes = NULL_BITMAP VARIABLE_COLUMNS Memory Dump @0x0A16C06B 00000000: 30000800 02000000 0200fc01 001100c4 †0...............
00000010: e3†††††††††††††††††††††††††††††††††††. Slot 1 Column 0 Offset 0x4 Length 4 id = 2 Slot 1 Column 1 Offset 0xf Length 2 NAME = 你 DBCC 执行完毕。如果 DBCC 输出了错误信息,请与系统管理员联系。
Slot 0 Offset 0x60 Length 11 Record Type = PRIMARY_RECORD Record Attributes = NULL_BITMAP
Memory Dump @0x0A16C060 00000000: 10000800 01000000 0200fe†††††††††††††........... Slot 0 Column 0 Offset 0x4 Length 4 id = 1
NAME = [NULL] Slot 1 Offset 0x6b Length 17 Record Type = PRIMARY_RECORD Record Attributes = NULL_BITMAP VARIABLE_COLUMNS Memory Dump @0x0A16C06B 00000000: 30000800 02000000 0200fc01 001100c4 †0...............
00000010: e3†††††††††††††††††††††††††††††††††††. Slot 1 Column 0 Offset 0x4 Length 4 id = 2 Slot 1 Column 1 Offset 0xf Length 2 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个字节存储可变长度偏移阵列
我们来验证一下这个想法:
代码如下:
USE [pratice]
GO
--
CREATE TABLE testnullandnotnullvarchar(id INT ,NAME1 VARCHAR(20) NULL,NAME2 VARCHAR(20) NULL)
GO --插入数据
INSERT INTO [dbo].[testnullandnotnullvarchar] ( [id],[Name1],[NAME2] )
SELECT 1 ,NULL,'你'
GO SELECT * FROM testnullandnotnullvarchar
-----------------------------------------------------------
--TRUNCATE TABLE DBCCResult
INSERT INTO DBCCResult EXEC ('DBCC IND(pratice,testnullandnotnullvarchar,-1) ') SELECT * FROM [dbo].[DBCCResult] ORDER BY [PageType] DESC DBCC TRACEON(3604,-1)
GO
DBCC PAGE([pratice],1,15656,3)
GO
数据页内容
DBCC 执行完毕。如果 DBCC 输出了错误信息,请与系统管理员联系。 PAGE: (1:15656) BUFFER: BUF @0x03D42838 bpage = 0x196A6000 bhash = 0x00000000 bpageno = (1:15656)
bdbid = 5 breferences = 0 bUse1 = 32718
bstat = 0xc0000b blog = 0xbbbbbbbb bnext = 0x00000000 PAGE HEADER: Page @0x196A6000 m_pageId = (1:15656) m_headerVersion = 1 m_type = 1
m_typeFlagBits = 0x4 m_level = 0 m_flagBits = 0x8000
m_objId (AllocUnitId.idObj) = 531 m_indexId (AllocUnitId.idInd) = 256
Metadata: AllocUnitId = 72057594072727552
Metadata: PartitionId = 72057594060537856 Metadata: IndexId = 0
Metadata: ObjectId = 1383675977 m_prevPage = (0:0) m_nextPage = (0:0)
pminlen = 8 m_slotCnt = 1 m_freeCnt = 8075
m_freeData = 115 m_reservedCnt = 0 m_lsn = (3045:22996:18)
m_xactReserved = 0 m_xdesId = (0:0) m_ghostRecCnt = 0
m_tornBits = 0 Allocation Status GAM (1:2) = ALLOCATED SGAM (1:3) = ALLOCATED
PFS (1:8088) = 0x61 MIXED_EXT ALLOCATED 50_PCT_FULL DIFF (1:6) = CHANGED
ML (1:7) = NOT MIN_LOGGED Slot 0 Offset 0x60 Length 19 Record Type = PRIMARY_RECORD Record Attributes = NULL_BITMAP VARIABLE_COLUMNS Memory Dump @0x0855C060 00000000: 30000800 01000000 0300fa02 00110013 †0...............
00000010: 00c4e3†††††††††††††††††††††††††††††††... Slot 0 Column 0 Offset 0x4 Length 4 id = 1
NAME1 = [NULL] Slot 0 Column 2 Offset 0x11 Length 2 NAME2 = 你 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表
--TRUNCATE TABLE DBCCResult
INSERT INTO DBCCResult EXEC ('DBCC IND(pratice,testnotnullvarchar,-1) ') SELECT * FROM [dbo].[DBCCResult] ORDER BY [PageType] DESC DBCC TRACEON(3604,-1)
GO
DBCC PAGE([pratice],1,14495,3)
GO SELECT LEN(name) FROM testnotnullvarchar WHERE [id]=1
数据页内容
DBCC 执行完毕。如果 DBCC 输出了错误信息,请与系统管理员联系。 PAGE: (1:14495) BUFFER: BUF @0x03D4258C bpage = 0x196D2000 bhash = 0x00000000 bpageno = (1:14495)
bdbid = 5 breferences = 0 bUse1 = 38447
bstat = 0xc0000b blog = 0xbbbbbbbb bnext = 0x00000000 PAGE HEADER: Page @0x196D2000 m_pageId = (1:14495) m_headerVersion = 1 m_type = 1
m_typeFlagBits = 0x4 m_level = 0 m_flagBits = 0x8000
m_objId (AllocUnitId.idObj) = 532 m_indexId (AllocUnitId.idInd) = 256
Metadata: AllocUnitId = 72057594072793088
Metadata: PartitionId = 72057594060603392 Metadata: IndexId = 0
Metadata: ObjectId = 1399676034 m_prevPage = (0:0) m_nextPage = (0:0)
pminlen = 8 m_slotCnt = 2 m_freeCnt = 8064
m_freeData = 124 m_reservedCnt = 0 m_lsn = (3045:23036:20)
m_xactReserved = 0 m_xdesId = (0:0) m_ghostRecCnt = 0
m_tornBits = 0 Allocation Status GAM (1:2) = ALLOCATED SGAM (1:3) = ALLOCATED
PFS (1:8088) = 0x61 MIXED_EXT ALLOCATED 50_PCT_FULL DIFF (1:6) = CHANGED
ML (1:7) = NOT MIN_LOGGED Slot 0 Offset 0x60 Length 11 Record Type = PRIMARY_RECORD Record Attributes = NULL_BITMAP
Memory Dump @0x0855C060 00000000: 10000800 01000000 0200fc†††††††††††††........... Slot 0 Column 0 Offset 0x4 Length 4 id = 1
NAME = [NULL] Slot 1 Offset 0x6b Length 17 Record Type = PRIMARY_RECORD Record Attributes = NULL_BITMAP VARIABLE_COLUMNS Memory Dump @0x0855C06B 00000000: 30000800 02000000 0200fc01 001100c4 †0...............
00000010: e3†††††††††††††††††††††††††††††††††††. Slot 1 Column 0 Offset 0x4 Length 4 id = 2 Slot 1 Column 1 Offset 0xf Length 2 NAME = 你 DBCC 执行完毕。如果 DBCC 输出了错误信息,请与系统管理员联系。
Slot 0 Offset 0x60 Length 11 Record Type = PRIMARY_RECORD Record Attributes = NULL_BITMAP
Memory Dump @0x0855C060 00000000: 10000800 01000000 0200fc†††††††††††††........... Slot 0 Column 0 Offset 0x4 Length 4 id = 1
NAME = [NULL] Slot 1 Offset 0x6b Length 17 Record Type = PRIMARY_RECORD Record Attributes = NULL_BITMAP VARIABLE_COLUMNS Memory Dump @0x0855C06B 00000000: 30000800 02000000 0200fc01 001100c4 †0...............
00000010: e3†††††††††††††††††††††††††††††††††††. Slot 1 Column 0 Offset 0x4 Length 4 id = 2 Slot 1 Column 1 Offset 0xf Length 2 NAME = 你
testnotnullvarchar表的数据页和testnullvarchar表的数据页对比一下
看到上面的对比图我也不再对testnotnullvarchar表做详细分析了
情况跟testnullvarchar表是一样的
只有一个地方不一样,就是LEN()函数
小结:
对于varchar数据类型,无论是空字符串还是NULL值都不占用任何空间
CHAR类型的情况
testnullchar表
--TRUNCATE TABLE DBCCResult
INSERT INTO DBCCResult EXEC ('DBCC IND(pratice,testnullchar,-1) ') SELECT * FROM [dbo].[DBCCResult] ORDER BY [PageType] DESC DBCC TRACEON(3604,-1)
GO
DBCC PAGE([pratice],1,8353,3)
GO SELECT LEN(name) FROM testnullchar WHERE [id]=1
数据页内容
DBCC 执行完毕。如果 DBCC 输出了错误信息,请与系统管理员联系。 PAGE: (1:8353) BUFFER: BUF @0x03CF68D0 bpage = 0x16FA8000 bhash = 0x00000000 bpageno = (1:8353)
bdbid = 5 breferences = 0 bUse1 = 39861
bstat = 0x2c0000b blog = 0x59bbbbbb bnext = 0x00000000 PAGE HEADER: Page @0x16FA8000 m_pageId = (1:8353) m_headerVersion = 1 m_type = 1
m_typeFlagBits = 0x4 m_level = 0 m_flagBits = 0x8000
m_objId (AllocUnitId.idObj) = 533 m_indexId (AllocUnitId.idInd) = 256
Metadata: AllocUnitId = 72057594072858624
Metadata: PartitionId = 72057594060668928 Metadata: IndexId = 0
Metadata: ObjectId = 1415676091 m_prevPage = (0:0) m_nextPage = (0:0)
pminlen = 28 m_slotCnt = 2 m_freeCnt = 8030
m_freeData = 158 m_reservedCnt = 0 m_lsn = (3045:23074:20)
m_xactReserved = 0 m_xdesId = (0:0) m_ghostRecCnt = 0
m_tornBits = 0 Allocation Status GAM (1:2) = ALLOCATED SGAM (1:3) = ALLOCATED
PFS (1:8088) = 0x61 MIXED_EXT ALLOCATED 50_PCT_FULL DIFF (1:6) = CHANGED
ML (1:7) = NOT MIN_LOGGED Slot 0 Offset 0x60 Length 31 Record Type = PRIMARY_RECORD Record Attributes = NULL_BITMAP
Memory Dump @0x0A27C060 00000000: 10001c00 01000000 00000000 00000000 †................
00000010: 00000000 00000000 00000000 0200fe††††............... Slot 0 Column 0 Offset 0x4 Length 4 id = 1
NAME = [NULL] Slot 1 Offset 0x7f Length 31 Record Type = PRIMARY_RECORD Record Attributes = NULL_BITMAP
Memory Dump @0x0A27C07F 00000000: 10001c00 02000000 c4e32020 20202020 †..........
00000010: 20202020 20202020 20202020 0200fc†††† ... Slot 1 Column 0 Offset 0x4 Length 4 id = 2 Slot 1 Column 1 Offset 0x8 Length 20 NAME = 你 DBCC 执行完毕。如果 DBCC 输出了错误信息,请与系统管理员联系。
Slot 0 Offset 0x60 Length 31 Record Type = PRIMARY_RECORD Record Attributes = NULL_BITMAP
Memory Dump @0x0A27C060 00000000: 10001c00 01000000 00000000 00000000 †................
00000010: 00000000 00000000 00000000 0200fe††††............... Slot 0 Column 0 Offset 0x4 Length 4 id = 1
NAME = [NULL] Slot 1 Offset 0x7f Length 31 Record Type = PRIMARY_RECORD Record Attributes = NULL_BITMAP
Memory Dump @0x0A27C07F 00000000: 10001c00 02000000 c4e32020 20202020 †..........
00000010: 20202020 20202020 20202020 0200fc†††† ... Slot 1 Column 0 Offset 0x4 Length 4 id = 2 Slot 1 Column 1 Offset 0x8 Length 20 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数据类型的大小
例如:
--允许空,char类型
CREATE TABLE testnullchar(id INT,NAME CHAR(20) NULL)
GO
指定char为20,那么就占用20个字节的空间
testnotnullchar表
--TRUNCATE TABLE DBCCResult
INSERT INTO DBCCResult EXEC ('DBCC IND(pratice,testnotnullchar,-1) ') SELECT * FROM [dbo].[DBCCResult] ORDER BY [PageType] DESC DBCC TRACEON(3604,-1)
GO
DBCC PAGE([pratice],1,37266,3)
GO SELECT LEN(name) FROM testnotnullchar WHERE [id]=1
数据页内容
DBCC 执行完毕。如果 DBCC 输出了错误信息,请与系统管理员联系。 PAGE: (1:37266) BUFFER: BUF @0x03D669F0 bpage = 0x1A500000 bhash = 0x00000000 bpageno = (1:37266)
bdbid = 5 breferences = 0 bUse1 = 42107
bstat = 0xc0000b blog = 0x9bbbbbbb bnext = 0x00000000 PAGE HEADER: Page @0x1A500000 m_pageId = (1:37266) m_headerVersion = 1 m_type = 1
m_typeFlagBits = 0x4 m_level = 0 m_flagBits = 0x8000
m_objId (AllocUnitId.idObj) = 534 m_indexId (AllocUnitId.idInd) = 256
Metadata: AllocUnitId = 72057594072924160
Metadata: PartitionId = 72057594060734464 Metadata: IndexId = 0
Metadata: ObjectId = 1431676148 m_prevPage = (0:0) m_nextPage = (0:0)
pminlen = 28 m_slotCnt = 2 m_freeCnt = 8030
m_freeData = 158 m_reservedCnt = 0 m_lsn = (3045:23137:20)
m_xactReserved = 0 m_xdesId = (0:0) m_ghostRecCnt = 0
m_tornBits = 0 Allocation Status GAM (1:2) = ALLOCATED SGAM (1:3) = ALLOCATED
PFS (1:32352) = 0x61 MIXED_EXT ALLOCATED 50_PCT_FULL DIFF (1:6) = CHANGED
ML (1:7) = NOT MIN_LOGGED Slot 0 Offset 0x60 Length 31 Record Type = PRIMARY_RECORD Record Attributes = NULL_BITMAP
Memory Dump @0x0A01C060 00000000: 10001c00 01000000 20202020 20202020 †........
00000010: 20202020 20202020 20202020 0200fc†††† ... Slot 0 Column 0 Offset 0x4 Length 4 id = 1 Slot 0 Column 1 Offset 0x8 Length 20 NAME = Slot 1 Offset 0x7f Length 31 Record Type = PRIMARY_RECORD Record Attributes = NULL_BITMAP
Memory Dump @0x0A01C07F 00000000: 10001c00 02000000 c4e32020 20202020 †..........
00000010: 20202020 20202020 20202020 0200fc†††† ... Slot 1 Column 0 Offset 0x4 Length 4 id = 2 Slot 1 Column 1 Offset 0x8 Length 20 NAME = 你 DBCC 执行完毕。如果 DBCC 输出了错误信息,请与系统管理员联系。
Slot 0 Offset 0x60 Length 31 Record Type = PRIMARY_RECORD Record Attributes = NULL_BITMAP
Memory Dump @0x0A01C060 00000000: 10001c00 01000000 20202020 20202020 †........
00000010: 20202020 20202020 20202020 0200fc†††† ... Slot 0 Column 0 Offset 0x4 Length 4 id = 1 Slot 0 Column 1 Offset 0x8 Length 20 NAME = Slot 1 Offset 0x7f Length 31 Record Type = PRIMARY_RECORD Record Attributes = NULL_BITMAP
Memory Dump @0x0A01C07F 00000000: 10001c00 02000000 c4e32020 20202020 †..........
00000010: 20202020 20202020 20202020 0200fc†††† ... Slot 1 Column 0 Offset 0x4 Length 4 id = 2 Slot 1 Column 1 Offset 0x8 Length 20 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数据类型的大小
例如:
--允许空,char类型
CREATE TABLE testnullchar(id INT,NAME CHAR(20) NULL)
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