sqlserver查看索引_SQL Server页中行物理存储
SQL Server頁有很多類型:
1 – 數(shù)據(jù)頁. 記錄堆或者聚集索引葉子級的數(shù)據(jù)
2 – 索引頁. 用于保存聚集索引中的中間頁和根頁,或者非聚集索引的所有頁
3 – text mix page. A text page that holds small chunks of LOB values plus internal parts of text tree. These can be shared between LOB values in the same partition of an index or heap.
4 – text tree page. A text page that holds large chunks of LOB values from a single column value.
7 – sort page. A page that stores intermediate results during a sort operation.
8 – GAM page. Holds global allocation information about extents in a GAM interval (every data file is split into 4GB chunks – the number of extents that can be represented in a bitmap on a single database page). Basically whether an extent is allocated or not. GAM = Global Allocation Map. The first one is page 2 in each file. More on these in a later post.
9 – SGAM page. Holds global allocation information about extents in a GAM interval. Basically whether an extent is available for allocating mixed-pages. SGAM = Shared GAM. the first one is page 3 in each file. More on these in a later post.
10 – IAM page. Holds allocation information about which extents within a GAM interval are allocated to an index or allocation unit, in SQL Server 2000 and 2005 respectively. IAM = Index Allocation Map. More on these in a later post.
11 – PFS page. Holds allocation and free space information about pages within a PFS interval (every data file is also split into approx 64MB chunks – the number of pages that can be represented in a byte-map on a single database page. PFS = Page FreeSpace. The first one is page 1 in each file. More on these in a later post.
13 – boot page. Holds information about the database. There's only one of these in the database. It's page 9 in file 1.
15 – file header page. Holds information about the file. There's one per file and it's page 0 in the file.
16 – diff map page. Holds information about which extents in a GAM interval have changed since the last full or differential backup. The first one is page 6 in each file.
17 – ML map page. Holds information about which extents in a GAM interval have changed while in bulk-logged mode since the last backup. This is what allows you to switch to bulk-logged mode for bulk-loads and index rebuilds without worrying about breaking a backup chain. The first one is page 7 in each file.
PFS頁= 96+4+8088+4 間隔8088, 96頁頭,4行頭,slot0 8088 ,4slotlist
GAM/SGAM=96+4+90+4+7988+10 間隔 7988*8,96頁頭,4行頭,90slot0,4行頭,7988slot1,10slotlist
IAM頁 = ? ? ?96+4+90+4+7988+10 96頁頭,4行頭,90slot0,4行頭,7988slot1,10slotlist?
1. ? 數(shù)據(jù)頁行存儲格式
數(shù)據(jù)頁的基本格式:
數(shù)據(jù)頁的基本格式:
信息 | 助記符 | 大小(Byte) |
狀態(tài)A | TagA | 1 |
狀態(tài)B | TagB | 1 |
固定長度大小 | Fsize | 2 |
固定長度數(shù)據(jù) | Fdata | Fsize-4 |
列數(shù)量 | Ncol | 2 |
NULL位圖(表中每列一個(gè)位;1表示對應(yīng)列為null) | Nullbits | Ceil(Ncol/8) |
行中存儲的可變長度列數(shù) | VarCount | 2 |
可變長度的偏移陣列 | VarOffset | 2*VarCount |
可變長數(shù)據(jù) | VarData | VarOffset[VarCount]-(Fsize+8-4+ Ceil(Ncol/8)+2*VarCount) |
實(shí)例:
USE db_TestEnvcreate table Index_test(id int,a varchar(10))goinsert into Index_test select 100,'aaaaa'go 4000DBCC IND(db_TestEnv,Index_test,1)DBCC PAGE(db_TestEnv,1,45969,1)輸出
Slot 0, Offset 0x60, Length 20, DumpStyle BYTE Record Type = PRIMARY_RECORD Record Attributes = NULL_BITMAP VARIABLE_COLUMNSRecord Size = 20 Memory Dump @0x613BC060 00000000: 30000800 64000000 02000001 00140061 ?0...d..........a 00000010: 61616161 ????????????????????????????aaaaa. ? ? ?第一個(gè)字節(jié) TagA = 0x30 是由2個(gè)部分組成0x10(第4個(gè)位) 和0x20(第5個(gè)位),其中0x10表示有null列,0x20表示有可變長,0x40(第6個(gè)位)表示有版本標(biāo)記,0x80(第7個(gè)位)表示TagB是否有值。
其中1-3位為行類型分別意思如下:
? ? ? ? 0:primary record,堆上的數(shù)據(jù)頁或者聚集索引的葉子頁。
? ? ? ? 1:forwarded record, ?被轉(zhuǎn)發(fā)頁
? ? ? ? 2:forwarding record,轉(zhuǎn)發(fā)根存頁(在行移動時(shí)會出現(xiàn)轉(zhuǎn)發(fā)頁和轉(zhuǎn)發(fā)根存頁如行溢出,可以查看《深入解析sql server 2008》 5.7.4.1 和6.7.4.1的相關(guān)內(nèi)容)
? ? ? ? 3:index record,聚集索引非葉子頁或者非聚集索引記錄
? ? ? ? 4:blob record,blob記錄
? ? ? ? 5:ghost index record ,影子索引,被刪除了沒被清理,可以使用顯示事務(wù)來觀察
? ? ? ? 6:ghost data record,影子記錄,被刪除了沒被清理,可以使用顯示事務(wù)來觀察
? ? ? ? 7:ghost version record,幻想記錄,詳細(xì)請看《深入解析 SQL Server 2008》 10.7.3.8
b. ? ? ?第二個(gè)字節(jié)TagB有2個(gè)取值0x00,0x01.如果是0x01說明是被轉(zhuǎn)發(fā)頁的幻影頁。若為0x01則為TagA字節(jié)的解釋
其他的不需要解釋了,更具上面的表格就可以。
2. ? 索引頁行存儲格式
索引頁行存儲格式分為2種:1.非葉子,2.葉子。但是會因?yàn)槭嵌驯砩系姆蔷奂饕?#xff0c;還是聚集索引表上的非聚集索引有所不同。是否include對索引的存儲格式?jīng)]啥影響。
1.堆表下
實(shí)例:
USE db_TestEnvcreate table Index_test(id INT IDENTITY,a char(10),b VARCHAR(10))goinsert into Index_test select 'aaaaa','bbb'go 4000create nonclustered index ix_id_a on Index_test(id,a)1.1 ?葉子頁
基本格式
基本格式
信息 | 助記符 | 大小(Byte) |
行頭 | Header | 1 |
定長建值 | Fkey | 定長大小 |
表記錄Rowid(fileid:page:slote) | RowID | 8(4數(shù)據(jù)頁,2頁號,2槽號) |
索引記錄包含的字段個(gè)數(shù) | col | 2 |
NULL位圖(表中每列一個(gè)位;1表示對應(yīng)列為null) | Nullbits | Ceil(可為空列數(shù)/8) |
行中存儲的可變長度列數(shù) | VarCount | 2 |
可變長度的偏移陣列 | VarOffset | 2*VarCount |
可變長數(shù)據(jù) | VarData | VarOffset[VarCount]-(Fkey+12+ Ceil(可為空列/8)+2*VarCount) |
輸出
Slot 0, Offset 0x60, Length 26, DumpStyle BYTERecord Type = INDEX_RECORD Record Attributes = NULL_BITMAP Record Size = 26Memory Dump @0x6128C06000000000: 16220100 00616161 61612020 202020f2 ?."...aaaaa . 00000010: e1000001 00200003 0000???????????????..... ....a. ? ? ?第一個(gè)字節(jié)header有以下的意義:
0x40:對于記錄類型為索引記錄總為0
0x20: 包含可變長字段
0x10: 包含null位圖數(shù)據(jù)
? ? ? ? 1-3bit 表示是否是索引記錄
? ? ? ? ?其他就不需要解釋了,按照表格可以輕易的得出。
1.2 ?非葉子頁
基本格式
信息 | 助記符 | 大小(Byte) |
行頭 | Header | 1 |
定長建值 | Fkey | 定長大小 |
表記錄Rowid(fileid:page:slote) | RowID | 8(4數(shù)據(jù)頁,2頁號,2槽號) |
下一個(gè)頁所在的葉子節(jié)點(diǎn)(fileid:page) | KeyRowid | 6(4數(shù)據(jù)頁,2頁號) |
索引記錄包含的字段個(gè)數(shù) | col | 2 |
NULL位圖(表中每列一個(gè)位;1表示對應(yīng)列為null) | Nullbits | Ceil(可為空列數(shù)/8) |
行中存儲的可變長度列數(shù) | VarCount | 2 |
可變長度的偏移陣列 | VarOffset | 2*VarCount |
可變長數(shù)據(jù) | VarData | VarOffset[VarCount]-(Fkey+18+ Ceil(可為空列/8)+2*VarCount) |
輸出
Slot 0, Offset 0x60, Length 32, DumpStyle BYTERecord Type = INDEX_RECORD Record Attributes = NULL_BITMAP Record Size = 32Memory Dump @0x6095C06000000000: 16010000 00616161 61612020 202020f0 ?.....aaaaa . 00000010: e1000001 0000007c 8d010001 00030000 ?.......|........葉子非葉子沒有什么大的區(qū)別就是非葉子少了鍵值所在的葉子節(jié)點(diǎn)。需要注意的是唯一索引的非葉子比較特別沒有表記錄Rowid。
2.在聚集索引下的非聚集索引
2.1葉子節(jié)點(diǎn)
信息 | 助記符 | 大小(Byte) |
行頭 | Header | 1 |
定長建值+聚集索引定長值 | Fkey | 定長大小 |
索引記錄包含的字段個(gè)數(shù) | col | 2 |
NULL位圖(表中每列一個(gè)位;1表示對應(yīng)列為null) | Nullbits | Ceil(可為空列數(shù)/8) |
行中存儲的可變長度列數(shù) | VarCount | 2 |
可變長度的偏移陣列 | VarOffset | 2*VarCount |
可變長數(shù)據(jù) | VarData | VarOffset[VarCount]-(Fkey+18+ Ceil(可為空列/8)+2*VarCount) |
輸出
Slot 0, Offset 0x60, Length 25, DumpStyle BYTERecord Type = INDEX_RECORD Record Attributes = NULL_BITMAP VARIABLE_COLUMNSRecord Size = 25 Memory Dump @0x6083C06000000000: 36214104 00204104 00050000 02001400 ?6!A.. A......... 00000010: 19006262 61616161 61?????????????????..bbaaaaa這個(gè)沒什么難度,第一個(gè)字節(jié) Header 和其他的都一樣就是聚集索引上的非聚集索引會帶上聚集索引的key,在實(shí)例中:21410400 為自己的iid,20410400為id主鍵,6262為key的鍵,6161616161為聚集索引key。
如果聚集索引是可重復(fù)的,sql server會產(chǎn)生一個(gè)消除重復(fù)的數(shù)字,被當(dāng)成可變長存放在可變成區(qū)域
2.2非葉子節(jié)點(diǎn)
信息 | 助記符 | 大小(Byte) |
行頭 | Header | 1 |
定長建值+聚集索引定長值 | Fkey | 定長大小 |
下一個(gè)頁所在的葉子節(jié)點(diǎn)(fileid:page) | KeyRowid | 6(4數(shù)據(jù)頁,2頁號) |
索引記錄包含的字段個(gè)數(shù) | col | 2 |
NULL位圖(表中每列一個(gè)位;1表示對應(yīng)列為null) | Nullbits | Ceil(可為空列數(shù)/8) |
行中存儲的可變長度列數(shù) | VarCount | 2 |
可變長度的偏移陣列 | VarOffset | 2*VarCount |
可變長數(shù)據(jù) | VarData | VarOffset[VarCount]-(Fkey+18+ Ceil(可為空列/8)+2*VarCount) |
輸出
Slot 0, Offset 0x60, Length 32, DumpStyle BYTERecord Type = INDEX_RECORD Record Attributes = NULL_BITMAP VARIABLE_COLUMNSRecord Size = 32 Memory Dump @0x607AC06000000000: 36020000 00010000 00e0b302 00010005 ?6............... 00000010: 00000200 1b002000 62623161 61616161 ?...... .bb1aaaaa第一個(gè)字節(jié)和其他的都一樣不解釋,其他的按表格都可以解析
如果聚集索引是可重復(fù)的,sql server會產(chǎn)生一個(gè)消除重復(fù)的數(shù)字,被當(dāng)成可變長存放在可變成區(qū)域
3.聚集索引中
在聚集索引下只有非葉子頁才是索引頁
基本格式
信息 | 助記符 | 大小(Byte) |
行頭 | Header | 1 |
一下層最小固定聚集索引建值 | Fkey | 固定聚集索引建大小 |
一下層頁號(fileid:page) | KeyRowid | 6(4數(shù)據(jù)頁,2頁號) |
索引記錄包含的字段個(gè)數(shù) | col | 2 |
NULL位圖(表中每列一個(gè)位;1表示對應(yīng)列為null) | Nullbits | Ceil(可為空列數(shù)/8) |
行中存儲的可變長度列數(shù) | VarCount | 2 |
可變長度的偏移陣列 | VarOffset | 2*VarCount |
可變長數(shù)據(jù) | VarData | VarOffset[VarCount]-(Fkey+18+ Ceil(可為空列/8)+2*VarCount) |
實(shí)例:
create table Index_test(id INT IDENTITY,a varchar(10),b VARCHAR(10))goinsert into Index_test select 'aaaaa','bb'go 4000create clustered index cix_id_a on Index_test(id,a)GODBCC IND(db_TestEnv,Index_test,1)DBCC PAGE(db_TestEnv,1,51106,1)輸出
Slot 0, Offset 0x60, Length 23, DumpStyle BYTERecord Type = INDEX_RECORD Record Attributes = NULL_BITMAP VARIABLE_COLUMNSRecord Size = 23 Memory Dump @0x6056C06000000000: 36010000 0097b300 00010002 00000100 ?6............... 00000010: 17006161 616161??????????????????????..aaaaa轉(zhuǎn)載自:
https://www.cnblogs.com/Amaranthus/archive/2013/01/31/2886581.html
文章經(jīng)作者授權(quán)轉(zhuǎn)載,版權(quán)歸原文作者所有
圖片來源于網(wǎng)絡(luò),侵權(quán)必刪!
總結(jié)
以上是生活随笔為你收集整理的sqlserver查看索引_SQL Server页中行物理存储的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 83998 连接服务器出错_来申请一个阿
- 下一篇: JAVA入门级教学之(整数型)