先对 rowid 有个感官认识:
SQL> select ROWID from Bruce_test where rownum<2;
ROWID
------------------ ----------
AAABnlAAFAAAAAPAAA
ROWID
------------------ ----------
AAABnlAAFAAAAAPAAA
ROWID 的格式如下:
数据对象编号 文件编号 块编号 行编号
OOOOOO FFF BBBBBB RRR
我们可以看出,从上面的 rowid 可以得知:
AAABnl
是数据对象编号
AAF
是相关文件编号
AAAAAP
是块编号
AAA
是行编号
怎么依据这些编号得到具体的十进制的编码值呢,这是经常遇到的问题。这里需要明白 rowid 的是基于 64 位编码的 18 个字符显示(数据对象编号 (6) + 文件编号 (3) + 块编号 (6)+ 行编号 (3)=18 位 ),其中
A-Z <==> 0 - 25 (26)
a-z <==> 26 - 51 (26)
0-9 <==> 52 - 61 (10)
+/ <==> 62 - 63 (2)
a-z <==> 26 - 51 (26)
0-9 <==> 52 - 61 (10)
+/ <==> 62 - 63 (2)
共 64 位,明白这个后,就可以计算出 10 进制的编码值,计算公式如下:
d * (b ^ p)
其中:
b
就是基数,这里就是
64
,
p
就是从右到左,已
0
开始的位置数
比如:上面的例子
文件号
AAF,
具体的计算应该是
:
5*(64^0)=5
;
0*(64^1)=0
;
0*(64^2)=0
;
文件号就是
0+0+5=5
刚才提到的是
rowid
的显示方式:
基于 64 位编码的 18 个字符显示,其实 rowid 的存储方式是: 10 个字 节即80位存储,其中 数据 对象编号 需 要 32 位,相关文件 编号 需 要 10 位,块 编号 需 要 22, 位行 编号 需 要 16 位,由此,我们可以得出:
32bit 的 object number ,每个数据库最多有 4G 个对象
10bit 的 file number ,每个对象最多有 1022 个文件( 2 个文件预留)
22bit 的 block number ,每个文件最多有 4M 个 BLOCK
16bit 的 row number ,每个 BLOCK 最多有 64K 个 ROWS
2.
rowid 相关的有用的 sql
最简单的基于 rowid 的显示方式得到的响应的 64 位编码对应值的 sql :
select rowid ,
substr(rowid,1,6) "OBJECT",
substr(rowid,7,3) "FILE",
substr(rowid,10,6) "BLOCK",
substr(rowid,16,3) "ROW"
from TableName;
OWID OBJECT FILE BLOCK ROW
------------------ ------------ ------ ------------ ------
AAABc4AADAAAGLUAAA AAABc4 AAD AAAGLU AAA
AAABc4AADAAAGLUAAB AAABc4 AAD AAAGLU AAB
AAABc4AADAAAGLUAAC AAABc4 AAD AAAGLU AAC
AAABc4AADAAAGLUAAD AAABc4 AAD AAAGLU AAD
AAABc4AADAAAGLUAAE AAABc4 AAD AAAGLU AAE
通过 dbms_rowid 这个包,可以直接的得到具体的 rowid 包含的信息:
select 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 bruce_t where rownum< 5;
dbms_rowid.rowid_block_number(rowid) block_id ,dbms_rowid.rowid_row_number(rowid) num from bruce_t where rownum< 5;
OBJECT_ID FILE_ID BLOCK_ID NUM
---------- ---------- ---------- ----------
5944 3 25300 0
5944 3 25300 1
5944 3 25300 2
5944 3 25300 3
一些使用
ROWID
的函数
ROWIDTOCHAR(rowid) :将 ROWID 转换成 STRING
CHARTOROWID('rowid_string') :将 STRING 转换成 ROWID
ROWIDTOCHAR(rowid) :将 ROWID 转换成 STRING
CHARTOROWID('rowid_string') :将 STRING 转换成 ROWID
另外,就是自己写的一些函数:(下面的函数是网友
eygle
提供)
create or replace function get_rowid
(l_rowid in varchar2)
return varchar2
is
ls_my_rowid varchar2(200);
rowid_type number;
object_number number;
relative_fno number;
block_number number;
row_number number;
begin
dbms_rowid.rowid_info(l_rowid,rowid_type,object_number,relative_fno, block_number, row_number);
ls_my_rowid := 'Object# is :'||to_char(object_number)||chr(10)||
'Relative_fno is :'||to_char(relative_fno)||chr(10)||
'Block number is :'||to_char(block_number)||chr(10)||
'Row number is :'||to_char(row_number);
return ls_my_rowid ;
end;
/
应用上面的函数如下:
SQL> select get_rowid(rowid), name from bruce_t;
GET_ROWID(ROWID) NAME
-------------------------------------------------------------------------------- --------------------------------
Object# is :5944 BruceLau
Relative_fno is :3
Block number is :25300
Row number is :0
Object# is :5944 MabelTang
Relative_fno is :3
Block number is :25300
Row number is :1