暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

SQL Server页中行物理存储

SQLServer走起 2020-07-01
813

SQL Server页有很多类型:

1 – 数据页. 记录堆或者聚集索引叶子级的数据

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.   数据页行存储格式

数据页的基本格式:

数据页的基本格式:

信息

助记符

大小(Byte

状态A

TagA

1

状态B

TagB

1

固定长度大小

Fsize

2

固定长度数据

Fdata

Fsize-4

列数量

Ncol

2

NULL位图(表中每列一个位;1表示对应列为null)

Nullbits

Ceil(Ncol/8)

行中存储的可变长度列数

VarCount

2

可变长度的偏移阵列

VarOffset

2*VarCount

可变长数据

VarData

VarOffset[VarCount]-(Fsize+8-4+ Ceil(Ncol/8)+2*VarCount)

实例:

    USE db_TestEnv
    create table Index_test(id int,a varchar(10))
    go
    insert into Index_test select 100,'aaaaa'
    go 4000
    DBCC 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_COLUMNS
      Record Size = 20
      Memory Dump @0x613BC060

      00000000: 30000800 64000000 02000001 00140061 †0...d..........a
      00000010: 61616161 ††††††††††††††††††††††††††††aaaa

       

      a.      第一个字节 TagA = 0x30 是由2个部分组成0x10(第4个位) 和0x20(第5个位),其中0x10表示有null列,0x20表示有可变长,0x40(第6个位)表示有版本标记,0x80(第7个位)表示TagB是否有值。

      其中1-3位为行类型分别意思如下:

              0:primary record,堆上的数据页或者聚集索引的叶子页。

              1:forwarded record,  被转发页

              2:forwarding record,转发根存页(在行移动时会出现转发页和转发根存页如行溢出,可以查看《深入解析sql server 2008》 5.7.4.1 和6.7.4.1的相关内容)

              3:index record,聚集索引非叶子页或者非聚集索引记录

              4:blob record,blob记录

              5:ghost index record ,影子索引,被删除了没被清理,可以使用显示事务来观察

              6:ghost data record,影子记录,被删除了没被清理,可以使用显示事务来观察

              7:ghost version record,幻想记录,详细请看《深入解析 SQL Server 2008》 10.7.3.8

      b.      第二个字节TagB有2个取值0x00,0x01.如果是0x01说明是被转发页的幻影页。若为0x01则为TagA字节的解释

      其他的不需要解释了,更具上面的表格就可以。

       

      2.   索引页行存储格式

      索引页行存储格式分为2种:1.非叶子,2.叶子。但是会因为是堆表上的非聚集索引,还是聚集索引表上的非聚集索引有所不同。是否include对索引的存储格式没啥影响。

      1.堆表下

      实例:

        USE db_TestEnv
        create table Index_test(id INT IDENTITY,a char(10),b VARCHAR(10))
        go
        insert into Index_test select 'aaaaa','bbb'
        go 4000
        create nonclustered index ix_id_a on Index_test(id,a)

        1.1  叶子页

        基本格式

        基本格式

        信息

        助记符

        大小(Byte

        行头

        Header

        1

        定长建值

        Fkey

        定长大小

        表记录Rowid(fileid:page:slote)

        RowID

        8(4数据页,2页号,2槽号)

        索引记录包含的字段个数

        col

        2

        NULL位图(表中每列一个位;1表示对应列为null)

        Nullbits

        Ceil(可为空列数/8)

        行中存储的可变长度列数

        VarCount

        2

        可变长度的偏移阵列

        VarOffset

        2*VarCount

        可变长数据

        VarData

        VarOffset[VarCount]-(Fkey+12+ Ceil(可为空列/8)+2*VarCount)

          DBCC PAGE(db_TestEnv,1,41006,1)

          输出

            Slot 0, Offset 0x60, Length 26, DumpStyle BYTE
            Record Type = INDEX_RECORD Record Attributes = NULL_BITMAP Record Size = 26
            Memory Dump @0x6128C060
            00000000: 16220100 00616161 61612020 202020f2 †."...aaaaa .
            00000010: e1000001 00200003 0000†††††††††††††††..... ....

            a.      第一个字节header有以下的意义:

            0x40:对于记录类型为索引记录总为0

            0x20: 包含可变长字段

            0x10: 包含null位图数据

                    1-3bit 表示是否是索引记录

                     其他就不需要解释了,按照表格可以轻易的得出。

            1.2  非叶子页

            基本格式

            信息

            助记符

            大小(Byte

            行头

            Header

            1

            定长建值

            Fkey

            定长大小

            表记录Rowid(fileid:page:slote)

            RowID

            8(4数据页,2页号,2槽号)

            下一个页所在的叶子节点(fileid:page)

            KeyRowid

            6(4数据页,2页号)

            索引记录包含的字段个数

            col

            2

            NULL位图(表中每列一个位;1表示对应列为null)

            Nullbits

            Ceil(可为空列数/8)

            行中存储的可变长度列数

            VarCount

            2

            可变长度的偏移阵列

            VarOffset

            2*VarCount

            可变长数据

            VarData

            VarOffset[VarCount]-(Fkey+18+ Ceil(可为空列/8)+2*VarCount)

              DBCC PAGE(db_TestEnv,1,41007,1)

              输出

                Slot 0, Offset 0x60, Length 32, DumpStyle BYTE
                Record Type = INDEX_RECORD Record Attributes = NULL_BITMAP Record Size = 32
                Memory Dump @0x6095C060
                00000000: 16010000 00616161 61612020 202020f0 †.....aaaaa .
                00000010: e1000001 0000007c 8d010001 00030000 †.......|........

                叶子非叶子没有什么大的区别就是非叶子少了键值所在的叶子节点。需要注意的是唯一索引的非叶子比较特别没有表记录Rowid。

                2.在聚集索引下的非聚集索引

                2.1叶子节点

                信息

                助记符

                大小(Byte

                行头

                Header

                1

                定长建值+聚集索引定长值

                Fkey

                定长大小

                索引记录包含的字段个数

                col

                2

                NULL位图(表中每列一个位;1表示对应列为null)

                Nullbits

                Ceil(可为空列数/8)

                行中存储的可变长度列数

                VarCount

                2

                可变长度的偏移阵列

                VarOffset

                2*VarCount

                可变长数据

                VarData

                VarOffset[VarCount]-(Fkey+18+ Ceil(可为空列/8)+2*VarCount)

                  create table Index_test(id INT IDENTITY,a varchar(10),b VARCHAR(10),iid)
                  go
                  insert into Index_test select 'aaaaa','bb'
                  go 4000
                  UPDATE dbo.Index_test SET iid = id+1
                  create clustered index cix_id_a on Index_test(id,a)
                  GO
                  CREATE NONCLUSTERED INDEX [idx__iid] ON [dbo].[Index_test]([iid],[b])
                  DBCC PAGE(db_TestEnv,1,164560,1)

                  输出

                    Slot 0, Offset 0x60, Length 25, DumpStyle BYTE
                    Record Type = INDEX_RECORD Record Attributes = NULL_BITMAP VARIABLE_COLUMNS
                    Record Size = 25
                    Memory Dump @0x6083C060
                    00000000: 36214104 00204104 00050000 02001400 †6!A.. A.........
                    00000010: 19006262 61616161 61†††††††††††††††††..bbaaaaa

                    这个没什么难度,第一个字节 Header 和其他的都一样就是聚集索引上的非聚集索引会带上聚集索引的key,在实例中:21410400 为自己的iid,20410400为id主键,6262为key的键,6161616161为聚集索引key。

                    如果聚集索引是可重复的,sql server会产生一个消除重复的数字,被当成可变长存放在可变成区域

                    2.2非叶子节点

                    信息

                    助记符

                    大小(Byte

                    行头

                    Header

                    1

                    定长建值+聚集索引定长值

                    Fkey

                    定长大小

                    下一个页所在的叶子节点(fileid:page)

                    KeyRowid

                    6(4数据页,2页号)

                    索引记录包含的字段个数

                    col

                    2

                    NULL位图(表中每列一个位;1表示对应列为null)

                    Nullbits

                    Ceil(可为空列数/8)

                    行中存储的可变长度列数

                    VarCount

                    2

                    可变长度的偏移阵列

                    VarOffset

                    2*VarCount

                    可变长数据

                    VarData

                    VarOffset[VarCount]-(Fkey+18+ Ceil(可为空列/8)+2*VarCount)

                      UPDATE dbo.Index_test SET b = b+RTRIM(id) 重建索引
                      DBCC PAGE(db_TestEnv,1,177184,1)

                      输出

                        Slot 0, Offset 0x60, Length 32, DumpStyle BYTE
                        Record Type = INDEX_RECORD Record Attributes = NULL_BITMAP VARIABLE_COLUMNS
                        Record Size = 32
                        Memory Dump @0x607AC060
                        00000000: 36020000 00010000 00e0b302 00010005 †6...............
                        00000010: 00000200 1b002000 62623161 61616161 †...... .bb1aaaaa

                        第一个字节和其他的都一样不解释,其他的按表格都可以解析

                        如果聚集索引是可重复的,sql server会产生一个消除重复的数字,被当成可变长存放在可变成区域

                         

                        3.聚集索引中

                        在聚集索引下只有非叶子页才是索引页

                        基本格式

                        信息

                        助记符

                        大小(Byte

                        行头

                        Header

                        1

                        下层最小固定聚集索引建值

                        Fkey

                        固定聚集索引建大小

                        一下层页号(fileid:page)

                        KeyRowid

                        6(4数据页,2页号)

                        索引记录包含的字段个数

                        col

                        2

                        NULL位图(表中每列一个位;1表示对应列为null)

                        Nullbits

                        Ceil(可为空列数/8)

                        行中存储的可变长度列数

                        VarCount

                        2

                        可变长度的偏移阵列

                        VarOffset

                        2*VarCount

                        可变长数据

                        VarData

                        VarOffset[VarCount]-(Fkey+18+ Ceil(可为空列/8)+2*VarCount)

                        实例:

                          create table Index_test(id INT IDENTITY,a varchar(10),b VARCHAR(10))

                          go

                          insert into Index_test select 'aaaaa','bb'

                          go 4000

                          create clustered index cix_id_a on Index_test(id,a)

                          GO

                          DBCC IND(db_TestEnv,Index_test,1)

                          DBCC PAGE(db_TestEnv,1,51106,1)

                          输出

                            Slot 0, Offset 0x60, Length 23, DumpStyle BYTE

                            Record Type = INDEX_RECORD Record Attributes = NULL_BITMAP VARIABLE_COLUMNS

                            Record Size = 23

                            Memory Dump @0x6056C060

                            00000000: 36010000 0097b300 00010002 00000100 †6...............

                            00000010: 17006161 616161††††††††††††††††††††††..aaaaa

                            转载自:

                            https://www.cnblogs.com/Amaranthus/archive/2013/01/31/2886581.html

                            文章经作者授权转载,版权归原文作者所有

                            图片来源于网络,侵权必删!

                            文章转载自SQLServer走起,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

                            评论