首页 > 代码库 > 探究oracle clob字段是如何存储的

探究oracle clob字段是如何存储的

    clob字段存储的是文本,如果存储少于4000个字节则clob字段会和记录存放在一起,如果存储超过4000个字节,则不会与记录存放在一起。值得注意的是,这里的4000个字节并不是等同于varchar2(4000),那相当于什么呢,请看下列实验,数据库版本是10.2.0.1.0:

drop table test_clob purge;
create table test_clob
(
  id number,
  clob1  clob
);

insert into test_clob values(1,Lpad(‘111‘,100,‘1‘));--236
insert into test_clob values(2,Lpad(‘222‘,500,‘1‘));--1036
insert into test_clob values(3,Lpad(‘333‘,1000,‘1‘));--2036
insert into test_clob values(4,Lpad(‘444‘,1982,‘1‘));--4000
insert into test_clob values(5,Lpad(‘555‘,1983,‘1‘));--4002
commit;

select rowid,
       dbms_rowid.rowid_object(rowid) object_id,
       dbms_rowid.rowid_relative_fno(rowid) file_id,
       dbms_rowid.rowid_block_number(rowid) block_id,
       dbms_rowid.rowid_row_number(rowid) num
  from test_clob;
ROWID               OBJECT_ID    FILE_ID   BLOCK_ID        NUM
------------------ ---------- ---------- ---------- ----------
AAAYQbAAFAADRpcAAA      99355          5     858716          0
AAAYQbAAFAADRpcAAB      99355          5     858716          1
AAAYQbAAFAADRpcAAC      99355          5     858716          2
AAAYQbAAFAADRpdAAA      99355          5     858717          0
AAAYQbAAFAADRpgAAA      99355          5     858720          0


alter system dump datafile 5 block 858716;
alter system dump datafile 5 block 858717;
alter system dump datafile 5 block 858720;

查看dump文件可以看到:

col  0: [ 2]  c1 02  --记录id=1  为啥是1 请看我以前的帖子:http://blog.csdn.net/stevendbaguo/article/details/8010105
col  1: [236]
 00 54 00 01 02 0c 80 00 00 02 00 00 00 01 00 00 00 02 1a 85 00 d8 09 00 00
 00 00 00 00 c8 00 00 00 00 00 01 00 31 00 31 00 31 00 31 00 31 00 31 00 31
 ...........................................................................
 ...........................................................................
 
col  0: [ 2]  c1 03   --记录id=2
col  1: [1036]
 00 54 00 01 02 0c 80 00 00 02 00 00 00 01 00 00 00 02 1a 86 03 f8 09 00 00
 00 00 00 03 e8 00 00 00 00 00 01 00 31 00 31 00 31 00 31 00 31 00 31 00 31
 ...........................................................................
 ...........................................................................


col  0: [ 2]  c1 04   --记录id=3
col  1: [2036]
 00 54 00 01 02 0c 80 00 00 02 00 00 00 01 00 00 00 02 1a 87 07 e0 09 00 00
 00 00 00 07 d0 00 00 00 00 00 01 00 31 00 31 00 31 00 31 00 31 00 31 00 31
 ...........................................................................
 ...........................................................................
  
col  0: [ 2]  c1 05   --记录id=4
col  1: [4000]
 00 54 00 01 02 0c 80 00 00 02 00 00 00 01 00 00 00 02 1a 88 0f 8c 09 00 00
 00 00 00 0f 7c 00 00 00 00 00 01 00 31 00 31 00 31 00 31 00 31 00 31 00 31
 ...........................................................................
 ........................................................................... 


col  0: [ 2]  c1 06   --记录id=5
col  1: [40]
 00 54 00 01 02 0c 80 00 00 02 00 00 00 01 00 00 00 02 1a 89 00 14 05 00 00
 00 00 00 0f 7e 00 00 00 00 00 02 01 4d 98 ac


记录 Lpad(‘111‘,100,‘1‘));--236
记录 Lpad(‘222‘,500,‘1‘)--1036
记录 Lpad(‘333‘,1000,‘1‘)--2036
记录 Lpad(‘444‘,1982,‘1‘)--4000
记录 Lpad(‘555‘,1983,‘1‘)--4002

   看出规律来了,第一条记录我们认为的长度是100,存在block中的长度是236,就是236=100*2+36,其他的规律都是这样。同时也可以看到存储超过4000个字节,就是长度为1982时这个就是临界值,大于这个之后就clob就存到其他地方了。