數據庫中每一行到底占用多少空間,在這里,做個小測試~~~這里只做了堆表的計算,如果包含索引,按照文檔把索引的列也加上就OK啦~
建個測試表,然后用DBCC PAGE打印出Slot的信息
CREATE TABLE TstA( col1 CHAR(5) NULL, col2 CHAR(5) NULL, col3 VARCHAR(5) NULL, col4 VARCHAR(10) NULL, col5 VARCHAR(50) NULL)INSERT INTO TstA (col1,col2,col3,col4,col5) VALUES ('aa1','2222222222','joan2','aoaonfoen','1111111')dbcc ind(TestDB, Object_id('TstA'),-1)
DBCC PAGE(TestDB,1,11957,1)
然后我就直接把slot的消息貼出來分析吧~~
-------------------------------------------------------------------------------------------------------------------------------------
Slot 0, Offset 0x60, Length 51, DumpStyle BYTE
Record Type = PRIMARY_RECORD Record Attributes = NULL_BITMAP VARIABLE_COLUMNS
Record Size = 51
Memory Dump @0x0A93C060
00000000: 30000e00 61613120 206a6f61 6e320500 †0...aa1 joan2..
00000010: 00030023 002c0033 00323232 32323232 †...#.,.3.2222222
00000020: 32323261 6f616f6e 666f656e 31313131 †222aoaonfoen1111
00000030: 313131†††††††††††††††††††††††††††††††111
4 8 12 16 20 24 28 32 36 40 44 48 52
30000e0061613120 206a6f61 6e32050000030023 002c0033 00323232 323232332323261 6f616f6e 666f656e31313131313131
1 2 3 4 5 6 7 8 9
1: 固定長度列結束位置
2: 固定長度第一列(col1)
3: 固定長度第二列(col3) --如果之后還有更多的列,繼續往后排
4: 行包含的欄位總數,本例為5
5: 空值維護:計算方式為 2+ (可為空列數+7)/8
6: 每一個可變列的結束位置。長度計算 2 + (可變列列數*2)
7+:變長列的每一個填充列
驗證了這一句話,每行的開銷
-----------------------------------------------------------------------------------------------------
經過測試,無論列的順序怎么排,首先會先把定長的字段先排出來,然后再排變長字段~好吧~測試結束
小弟不才~歡迎大家指出不足
參考:
http://technet.microsoft.com/zh-cn/library/ms189124.aspx
PS:上面2個DBCC 的語法
dbcc ind(dbname|dbid, tbname|tbid,-1)dbcc page ( {'dbname' | dbid}, filenum, pagenum [, printopt={0|1|2|3} ])
The filenum and pagenum parameters are taken from the page IDs that come from various system tables and appear in DBCC or other system error messages. A page ID of, say, (1:354) has filenum = 1 and pagenum = 354.
The printopt parameter has the following meanings:
0 - print just the page header1 - page header plus per-row hex dumps and a dump of the page slot array (unless its a page that doesn't have one, like allocation bitmaps)2 - page header plus whole page hex dump3 - page header plus detailed per-row interpretation
新聞熱點
疑難解答