data block

偷窥Data block 的物理结构 

最近整理了一下Data block 的物理结构。但是其中对某些点介绍的比较模糊,或者甚至有出入,请大家及时指出。也欢迎大家针对某个概念展开并提出自己的看法,我们共同探讨和学习,谢谢!

 

data block物理结构的认识

 

1. Data Block 物理结构图:

 

   

2.一次对block的dump过程:

   

代码:——————————————————————————–

SQL> create table t9 (a varchar(10));

 

Table created.

 

SQL> insert into t9 values(’a');

 

1 row created.

 

SQL> commit;

 

Commit complete.

 

SQL> set serveroutput on

SQL> exec show_space(’T9′);

Free Blocks………………………..1

Total Blocks……………………….8

Total Bytes………………………..65536

Unused Blocks………………………6

Unused Bytes……………………….49152

Last Used Ext FileId………………..3

Last Used Ext BlockId……………….121

Last Used Block…………………….2

 

PL/SQL procedure successfully completed.

 

SQL> alter system dump datafile 3 block 122;

 

System altered.

 

SQL> select * from v$tablespace;

 

      TS# NAME                          INC

———- —————————— —

        0 SYSTEM                        YES

        1 UNDOTBS1                      YES

        8 USERS                         YES

       18 TEMP1                       ——————————————————————————–

Trace 文件:

*** 2004-07-25 15:48:01.000

Start dump data blocks tsn: 8 file#: 3 minblk 122 maxblk 122

buffer tsn: 8 rdba: 0×00c0007a (3/122)

scn: 0×0000.0068d716 seq: 0×01 flg: 0×02 tail: 0xd7160601

frmt: 0×02 chkval: 0×0000 type: 0×06=trans data

Block header dump: 0×00c0007a

Object id on Block? Y

seg/obj: 0×806d csc: 0×00.68d714 itc: 2 flg: O typ: 1 - DATA

fsl: 0 fnx: 0×0 ver: 0×01

 

Itl Xid Uba Flag Lck Scn/Fsc

0×01 0×0004.00b.00000fac 0×00801885.008c.56 –U- 1 fsc 0×0000.0068d716

0×02 0×0000.000.00000000 0×00000000.0000.00 —- 0 fsc 0×0000.00000000

 

data_block_dump,data header at 0×552105c

===============

tsiz: 0×1fa0

hsiz: 0×14

pbl: 0×0552105c

bdba: 0×00c0007a

76543210

flag=——–

ntab=1

nrow=1

frre=-1

fsbo=0×14

fseo=0×1f9b

avsp=0×1f83

tosp=0×1f83

0xe:pti[0] nrow=1 ffs=0

0×12:pri[0] ffs=0×1f9b

block_row_dump:

tab 0, row 0, @0×1f9b

tl: 5 fb: –H-FL– lb: 0×1 cc: 1

col 0: [ 1] 61

end_of_block_dump

End dump data blocks tsn: 8 file#: 3 minblk 122 maxblk 122

 

3.先介绍数据块中包括的3种头信息:

首先,数据块是通过data block buffer cache完成读和写操作的,所以它提供了20bytes的Cache Header和4bytes的 Tail给Cache,用来读取和管理。

 

2部分是为Transaction层提供的Header信息。它一共占据了48bytes,包括24bytes的控制信息,和一系列的Interested Transaction Slot (ITS)。

 

剩下的部分都叫Data Area,用来存储用户数据。Data Area也包括data header,和row data及剩余空间。但是Cluster blocks, table blocks, index block的data header,row data结构是不相同的,这里主要介绍table blocks.

    

5. 关于Minimum row length

Oracle确实有Minimun row length的要求,即5 bytes的实际数据。如果加上3 bytes的row header, 和在block hader 中占据的2 bytes的row directory entry ,还有1byte的column长度,应该一共是11 bytes

 

所以,一个block中最多包括 db_block_size/11 条rows

 

详情可参考(该贴也简单介绍了row的物理结构):

http://www.itpub.net/showthread.php…15&pagenumber=1

 

6.对于一个没有插入任何row的block来说,并且假设使用了默认的INITRANS,它的大小应该是:

db_block_size- (cache header+ transaction header +data header + Tail + table dictionary*ntab + row dictionary*row )=8192 - ( 20+48+14+4+4 +0)=8192 - 90 =8102 bytes

  

4.结合trace文件中的信息,详细介绍:

1) The Cache Header and Tail:

buffer tsn: 8 rdba: 0×00c0007a (3/122)

scn: 0×0000.0068d716 seq: 0×01 flg: 0×02 tail: 0xd7160601

frmt: 0×02 chkval: 0×0000 type: 0×06=trans data

 

Database block address: 占用4 bytes,Tablespace relative database block address(RDBA):包括Tablespace 所在数据文件的相对文件号file#=3, 和数据块的block_id=122

 

SCN: 占用6bytes,最后变化的scn。包括2bytes的高位字节(SCN wrap),和4bytes的低位字节(SCN base)

 

Sequence: 占用1byte,用途不明确,可能是辅助SCN的变化

Flag: 占用1byte

 

Format: 占用1byte,应该是用来区分版本。Oracle 8之前值为1,之后为2。

 

Checksum: 占用2byte,跟db_block_checksum 参数有关系。

引用oracle document 的解释:”DB_BLOCK_CHECKSUM determines whether DBWn and the direct loader will calculate a checksum (a number calculated from all the bytes stored in the block) and store it in the cache header of every data block when writing it to disk. Checksums are verified when a block is read-only if this parameter is true and the last write of the block stored a checksum. In addition, Oracle gives every log block a checksum before writing it to the current log.

If this parameter is set to false, DBWn calculates checksums only for the SYSTEM tablespace, but not for user tablespaces.”

 

Block type: 占用1byte,经常用到的有,1=undo segment header block; 2=undo data block; 5= data segment header block; 6=data block.

 

Unused: 占用4bytes,用来前后兼容。

 

—————————————–

Tail 包括了SCN中SCN base的低位(low-order)2bytes,然后是block type,还有Sequence number。每当block 被读的时候,都要检查Tail与 block header 是否一致,保证了这个block不是损坏的(corrupted)。

 

2) The Transaction Header:

一共占据48bytes,包括24bytes的控制信息,和一系列的Interested Transaction Slot (ITS)。这些ITS组合在一起称为Interested Transaction List (ITL)。初始的ITL slot 数量由 INITRANS 决定(index branch block 只有1个slot)。如果有足够的剩余空间,oracle会根据需要动态的分配这些slot,直到受到空间限制或者达到了MAXTRANS。

 

Block header dump: 0×00c0007a

Object id on Block? Y

seg/obj: 0×806d csc: 0×00.68d714 itc: 2 flg: O typ: 1 - DATA

fsl: 0 fnx: 0×0 ver: 0×01

 

Itl Xid Uba Flag Lck Scn/Fsc

0×01 0×0004.00b.00000fac 0×00801885.008c.56 –U- 1 fsc 0×0000.0068d716

0×02 0×0000.000.00000000 0×00000000.0000.00 —- 0 fsc 0×0000.00000000

 

24bytes的控制信息包括:

Object number(seg/obj): 占用4bytes,指在OBJ$中记录的segment 的 object number(0×806d=32877)

 

Cleanout SCN(csc): 占用6bytes,最后一次 full cleanout 的scn

 

ITL count(itc): 占用1byte,ITL 的slot数量。

 

Flag: 占用2bytes。O表示这个block在freelist 上。否则flag为”-”

 

Block type: 占用1byte。1=data; 2=index

 

ITL freelist slot(fsl): 占用1byte。Index to the first slot on the ITL freelist

 

Next freelist block(fnx): 占用4byte。Segment freelist中下一个block的RDBA

 

Version: 1 byte

 

Unused: 4bytes,用来前后兼容。

  

每个ITL entry包括以下的内容:

Transaction id(Xid): 8bytes。其中包括rollback segment number, transaction table中的slot number等。

 

Undo block address(Uba): 8bytes。其中包括rollback segment block的DBA,sequence number等。

 

Flags: 1nibble。

—- = transaction is active, or committed pending cleanout

C— = transaction has been committed and locks cleaned out

-B– = this undo record contains the undo for this ITL entry

–U- = transaction committed (maybe long ago); SCN is an upper bound

—T = transaction was still active at block cleanout SCN

 

Locks: 3nibbles. 也就是所谓的行级锁(row-level locks)

 

SCN or free space credit: 6bytes. 如果这个事务已经clean out,这个值就是SCN;否则,前两个字节表示由这个事务释放的此block中的空间数。

    

3)Data Area

包括14bytes的data header,4bytes/table的table dictionary,2bytes/row的row dictionary,即每增加一条row,row dictionary就多出2bytes用来记载该row,可以通过下面的hsiz反映出来。table dictionary主要用于cluster block中,只不过table block中的table dictionary只有一个table。

 

data_block_dump,data header at 0×552105c

===============

tsiz: 0×1fa0 ==> total data area size

hsiz: 0×14 ==> data header size (14+ntabs*4 + nrows*2)

pbl: 0×0552105c ==> pointer to buffer holding the block

bdba: 0×00c0007a ==> block dba / rdba

76543210

flag=——– ==> n=pctfree hit (clusters),f=don’t put on freelist, k=flushable cluster keys

ntab=1 ==> number of tables (>1 so this is a cluster)

nrow=1 ==> number of rows

frre=-1

fsbo=0×14 ==> free space begin offset

fseo=0×1f9b ==> free space end offset

avsp=0×1f83 ==> available space in the block

tosp=0×1f83 ==> total available space when all transactions commit

0xe:pti[0] nrow=1 ffs=0

0×12:pri[0] ffs=0×1f9b

block_row_dump:

tab 0, row 0, @0×1f9b ==> 3bytes row header

tl: 5 fb: –H-FL– lb: 0×1 cc: 1 ==> lb: 0×1 表示transaction 0×1,cc 表示column的个数

col 0: [ 1] 61 [B]==> column length(1 byte if length<250; otherwise 3 bytes) and values

end_of_block_dump

 

其中(摘自biti_rainy的关于block中数据的存储和重组的探究):

fb Flag Byte:

K = Cluster Key (Flags may change meaning if this is set to show HASH cluster)

C = Cluster table member

H = Head piece of row

D = Deleted row

F = First data piece

L = Last data piece

P = First column continues from previous piece

N = Last column continues in next piece

    

5. 关于Minimum row length

Oracle确实有Minimun row length的要求,即5 bytes的实际数据。如果加上3 bytes的row header, 和在block hader 中占据的2 bytes的row directory entry ,还有1byte的column长度,应该一共是11 bytes

 

所以,一个block中最多包括 db_block_size/11 条rows

 

详情可参考(该贴也简单介绍了row的物理结构):

http://www.itpub.net/showthread.php…15&pagenumber=1

 

6.对于一个没有插入任何row的block来说,并且假设使用了默认的INITRANS,它的大小应该是:

db_block_size- (cache header+ transaction header +data header + Tail + table dictionary*ntab + row dictionary*row )=8192 - ( 20+48+14+4+4 +0)=8192 - 90 =8102 bytes



评论暂缺

(Required)
(Required, will not be published)