我试图弄清楚 SQL Server 如何存储Tinyint
(应该是 1 字节长)列。
-- Create table
CREATE TABLE MyTest.dbo.TempTable
(
Col1 Tinyint NOT NULL
);
-- Fill it up
INSERT INTO dbo.TempTable VALUES (3);
-- Get page info
dbcc ind
(
'MyTest' /*Database Name*/
,'dbo.TempTable' /*Table Name*/
,-1 /*Display information for all pages of all indenxes*/
);
-- Get page data
dbcc traceon(3604)
dbcc page
(
'MyTest' /*Database Name*/
,1 /*File ID*/
,182 /*Page ID*/
,3 /*Output mode: 3 - display page header and row details */
)
结果如下:
DBCC 执行完成。如果 DBCC 打印错误消息,请联系您的系统管理员。
页:(1:182)…………
插槽 0 偏移量 0x60长度 9
记录类型 = PRIMARY_RECORD 记录属性 = NULL_BITMAP记录大小 = 9
内存转储@0x000000000545A060
0000000000000000: 10000600 03000100 00 †††††††††††††††††.........
插槽 0 列 1 偏移量 0x4 长度 2 长度(物理)2
Col1 = 3
DBCC 执行完成。如果 DBCC 打印错误消息,请联系您的系统管理员。
解释: 实际数据行为 10 00 0600 0300 0100 00 为:
10:状态位 A
00:状态位 B
0600:存储列数的位置
0300:Tinyint 数据
0100:列数
00:Null 位图
总字节数: 1 + 1 + 2 + 2 + 2 + 1 = 9 字节
与“Smallint”比较:将
“Col1”类型更改为“
Smallint
”(2 字节长)产生完全相同的结果。问题 为什么 SQL Server 将 2 个字节专用于 '
Tinyint
' 列?为什么它在商店规模上不区分“Tinyint”和“Smallint”?