rowid,object_id和data_object_id

本文深入探讨了Oracle数据库中ROWID的数据类型,包括其历史演变、组成结构、特性及用途,并通过示例展示了如何使用ROWID进行高效的数据检索。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[plain] view plain copy
  1. ROWID  
  2.   
  3. Before the release of Oracle8, ROWID datatype was used to store the physical address of each row of each table, as a hexadecimal number. The ROWID contained the physical address of the row and allowed you to retrieve the row in a single efficient block access.  
  4.   
  5. With Oracle8, the logical ROWID was introduced. Rows in Index-Organized tables do not have permanent physical addresses. The logical ROWID is accessed using the same syntax as the physical ROWID. For this reason, the physical ROWID was expanded in size to include a data object number (schema objects in the same segment).  
  6.   
  7. To support both logical and physical ROWIDs (as well as ROWIDs of non-Oracle tables) the universal ROWID was defined.  
  8.   
  9. You can use character host variables to store rowids in a readable format. When you SELECT or FETCH a rowid into a character host variable, Oracle converts the binary value to an 18-byte character string and returns it in the format  
  10.   
  11. BBBBBBBB.RRRR.FFFF   
  12.   
  13. where BBBBBBBB is the block in the database file, RRRR is the row in the block (the first row is 0), and FFFF is the database file. These numbers are hexadecimal. For example, the rowid  
  14.   
  15. 0000000E.000A.0007   
  16. points to the 11th row in the 15th block in the 7th database file.   

ROWID各列信息格式如下

数据对象编号 文件编号 块编号 行编号

OOOOOO             FFF            BBBBBB RRR

 ROWID 是一个类似于rownum的伪列,用于定位数据库中一条记录的一个相对唯一地址值。通常情况下,该值在该行数据插入到数据库表时即
被确定且唯一。而对于聚簇表,由于聚簇特性,不同表上的记录由于存储在相同的簇上,因此会拥有相同的ROWID。数据库的大多数操作都是通
过ROWID来完成的,而且使用ROWID来进行单记录定位速度是最快的。下面将给出ROWID的相关描述。 一、ROWID的特性组成及用途
  1、特性
      相对唯一性(聚簇表上不唯一)
      一旦确定,不可随意更改
      使用10个字节存储(扩展rowid),显示为18位的字符串
      特殊情况下,ROWID会发生变化(如下列情形)
         表的导入导出操作
         alter table tab_name move
         alter table tab_name shrink space
         flashback table tab_name
         拆分分区表
         分区表上更新一个值后记录被移入到新分区
         合并两个分区
  2、组成(扩展ROWID)
      数据库对象的对象编号
      数据库对象所在文件的文件编号
      数据库对象上块的编号
      块上的行编号(起始值为0)
  3、用途
      快速定位单行记录
      展示行在表上如何存储
      表上的一行的唯一标识符  
      用作数据类型 column_name rowid
  4、限制rowid,扩展rowid
      限制rowid用于早期Oracle版本(Oracle 8 以前),rowid由file#+block#+row#组成,占用6个bytes的空间
      扩展rowid,由data_object_id#+rfile#+block#+row#组成,占用10个bytes的空间

select ROWIDTOCHAR(rowid) vrowid ,id   from t_rover_alert where rowid=chartorowid('AAA5eEAAFAAABS2AAk');
select ROWIDTOCHAR(rowid) vrowid ,id   from t_rover_alert where rowid='AAA5eEAAFAAABS2AAk';
select ROWIDTOCHAR(rowid) vrowid,id  from t_rover_alert where ROWIDTOCHAR(rowid)=trim('AAA5eEAAFAAABS2AAk');


select ROWIDTOCHAR(rowid) vrowid ,id   from t_rover_alert where rowid in (chartorowid('AAA5eEAAFAAABS2AAk'));
select ROWIDTOCHAR(rowid) vrowid ,id   from t_rover_alert where rowid in ('AAA5eEAAFAAABS2AAk');
select ROWIDTOCHAR(rowid) vrowid,id  from t_rover_alert where ROWIDTOCHAR(rowid) in (trim('AAA5eEAAFAAABS2AAk'));


其实object_id和data_object_id同样是表示数据库对象的一个唯一标志,但是object_id表示的是逻辑id,data_object_id表示的是物理id。如果一些object没有物理属性的话那它就不存在data_object_id,例如procedure,function,package,data type,db link,mv定义,view定义,临时表,分区表定义等等这些object都是没有对应着某个segment,因此它们的data_object_id都为空。当表刚创建的时候它的object_id和data_object_id都是相等的,但是如果表经过move或truncate等,涉及到segment发生改变后data_object_id将会有变化。即object_id是唯一且不能为空的,而data_object_id是可以为空,且可变。

详细参考

http://www.xifenfei.com/1553.html

从下面结果可以知道,根据rowid算出来的是data_object_id,不是object_id。

  1. SQL>   
  2. SQL> select rowid,  
  3.   2         ROWIDTOCHAR(rowid) vrowid,  
  4.   3         chartorowid(ROWIDTOCHAR(rowid)) vvrowid,  
  5.   4         get_rowid(ROWIDTOCHAR(rowid)) msg,  
  6.   5         dbms_rowid.rowid_object(rowid) object_id,  
  7.   6         dbms_rowid.rowid_relative_fno(rowid) file_id,  
  8.   7         dbms_rowid.rowid_block_number(rowid) block_id,  
  9.   8         dbms_rowid.rowid_row_number(rowid) num  
  10.   9    from t_rover_alert  
  11.  10   where rownum < 2;  
  12.    
  13. ROWID              VROWID             VVROWID            MSG                                                                               OBJECT_ID    FILE_ID   BLOCK_ID        NUM  
  14. ------------------ ------------------ ------------------ -------------------------------------------------------------------------------- ---------- ---------- ---------- ----------  
  15. AAA5eEAAFAAABS6AAp AAA5eEAAFAAABS6AAp AAA5eEAAFAAABS6AAp Object# is      :235396                                                              235396          5       5306         41  
  16.                                                          Relative_fno is :5                                                                                                  
  17.                                                          Block number is :5306                                                                                               
  18.                                                          Row number is   :41                                                                                                 
  19.    
  20. SQL>   
  21. SQL> select * from user_objects where object_name=upper('t_rover_alert')  
  22.   2  ;  
  23.    
  24. OBJECT_NAME                                                                      SUBOBJECT_NAME                  OBJECT_ID DATA_OBJECT_ID OBJECT_TYPE         CREATED     LAST_DDL_TIME TIMESTAMP           STATUS  TEMPORARY GENERATED SECONDARY  
  25. -------------------------------------------------------------------------------- ------------------------------ ---------- -------------- ------------------- ----------- ------------- ------------------- ------- --------- --------- ---------  
  26. T_ROVER_ALERT                                                                                                        53241         235396 TABLE               2012/10/31  2013/6/13 15: 2012-10-31:19:59:47 VALID   N         N         N  
  27.    
  28. SQL>   


从下面两段执行计划来看,

  1. <span style="color:#FF0000"><strong>rowid='AAA5eEAAFAAABS2AAk'</strong></span>  
  1. <span style="color:#FF0000"><strong>ROWIDTOCHAR(rowid)=trim('AAA5eEAAFAAABS2AAk')</strong></span>  
的效率高。根据这个可以试着写批量delete和批量update。
  1. SQL> select ROWIDTOCHAR(rowid) vrowid ,id   from t_rover_alert where rowid=chartorowid('AAA5eEAAFAAABS2AAk');  
  2.   
  3. VROWID                                                         ID  
  4. ------------------------------------------------------ ----------  
  5. AAA5eEAAFAAABS2AAk                                         268042  
  6.   
  7.   
  8.   
  9. Execution Plan  
  10. ----------------------------------------------------------  
  11. select ROWIDTOCHAR(rowid) vrowid ,id   from t_rover_alert where rowid='AAA5eEAAFAAABS2AAk';  
  12. Plan hash value: 3520792594  
  13.   
  14. --------------------------------------------------------------------------------  
  15. ------------  
  16.   
  17. | Id  | Operation                  | Name          | Rows  | Bytes | Cost (%CPU)  
  18. Time     |  
  19.   
  20. --------------------------------------------------------------------------------  
  21. ------------  
  22.   
  23. |   0 | SELECT STATEMENT           |               |     1 |    17 |     1   (0)  
  24. | 00:00:01 |  
  25.   
  26. |   1 |  TABLE ACCESS BY USER ROWID| T_ROVER_ALERT |     1 |    17 |     1   (0)  
  27. | 00:00:01 |  
  28.   
  29. --------------------------------------------------------------------------------  
  30. ------------  
  31.   
  32.   
  33.   
  34. Statistics  
  35. ----------------------------------------------------------  
  36.           0  recursive calls  
  37.           0  db block gets  
  38.           1  consistent gets  
  39.           0  physical reads  
  40.           0  redo size  
  41.         594  bytes sent via SQL*Net to client  
  42.         469  bytes received via SQL*Net from client  
  43.           2  SQL*Net roundtrips to/from client  
  44.           0  sorts (memory)  
  45.           0  sorts (disk)  
  46.           1  rows processed  
  47.   
  48. SQL> SQL>   
  49. VROWID                                                         ID  
  50. ------------------------------------------------------ ----------  
  51. AAA5eEAAFAAABS2AAk                                         268042  
  52.   
  53.   
  54. Execution Plan  
  55. ----------------------------------------------------------  
  56.   
  57. Plan hash value: 3520792594  
  58.   
  59. --------------------------------------------------------------------------------  
  60. ------------  
  61.   
  62. | Id  | Operation                  | Name          | Rows  | Bytes | Cost (%CPU)  
  63. Time     |  
  64.   
  65. --------------------------------------------------------------------------------  
  66. ------------  
  67.   
  68. |   0 | SELECT STATEMENT           |               |     1 |    17 |     1   (0)  
  69. | 00:00:01 |  
  70.   
  71. |   1 |  TABLE ACCESS BY USER ROWID| T_ROVER_ALERT |     1 |    17 |     1   (0)  
  72. | 00:00:01 |  
  73.   
  74. --------------------------------------------------------------------------------  
  75. ------------  
  76.   
  77.   
  78.   
  79. Statistics  
  80. ----------------------------------------------------------  
  81.           0  recursive calls  
  82.           0  db block gets  
  83.           1  consistent gets  
  84.           0  physical reads  
  85.           0  redo size  
  86.         594  bytes sent via SQL*Net to client  
  87.         469  bytes received via SQL*Net from client  
  88.           2  SQL*Net roundtrips to/from client  
  89.           0  sorts (memory)  
  90.           0  sorts (disk)  
  91.           1  rows processed  
  92.   
  93. SQL> SQL>   
  94. SQL> select ROWIDTOCHAR(rowid) vrowid,id  from t_rover_alert where ROWIDTOCHAR(rowid)=trim('AAA5eEAAFAAABS2AAk');  
  95.   
  96. VROWID                                                         ID  
  97. ------------------------------------------------------ ----------  
  98. AAA5eEAAFAAABS2AAk                                         268042  
  99.   
  100.   
  101. Execution Plan  
  102. ----------------------------------------------------------  
  103. Plan hash value: 1380109762  
  104.   
  105. --------------------------------------------------------------------------------  
  106. -----  
  107.   
  108. | Id  | Operation        | Name             | Rows  | Bytes | Cost (%CPU)| Time  
  109.     |  
  110.   
  111. --------------------------------------------------------------------------------  
  112. -----  
  113.   
  114. |   0 | SELECT STATEMENT |                  |     2 |    34 |     1   (0)| 00:00  
  115. :01 |  
  116.   
  117. |*  1 |  INDEX FULL SCAN | T_ROVER_ALERT_PK |     2 |    34 |     1   (0)| 00:00  
  118. :01 |  
  119.   
  120. --------------------------------------------------------------------------------  
  121. -----  
  122.   
  123.   
  124. Predicate Information (identified by operation id):  
  125. ---------------------------------------------------  
  126.   
  127.    1 - filter(ROWIDTOCHAR(ROWID)='AAA5eEAAFAAABS2AAk')  
  128.   
  129.   
  130. Statistics  
  131. ----------------------------------------------------------  
  132.           0  recursive calls  
  133.           0  db block gets  
  134.           2  consistent gets  
  135.           0  physical reads  
  136.           0  redo size  
  137.         594  bytes sent via SQL*Net to client  
  138.         469  bytes received via SQL*Net from client  
  139.           2  SQL*Net roundtrips to/from client  
  140.           0  sorts (memory)  
  141.           0  sorts (disk)  
  142.           1  rows processed  
  143.   
  144. SQL>   


使用in关键字


  1. SQL>   
  2. SQL> select ROWIDTOCHAR(rowid) vrowid ,id   from t_rover_alert where rowid in (chartorowid('AAA5eEAAFAAABS2AAk'));  
  3.   
  4. VROWID                                                         ID  
  5. ------------------------------------------------------ ----------  
  6. AAA5eEAAFAAABS2AAk                                         268042  
  7.   
  8.   
  9.   
  10. Execution Plan  
  11. ----------------------------------------------------------  
  12. select ROWIDTOCHAR(rowid) vrowid ,id   from t_rover_alert where rowid in ('AAA5eEAAFAAABS2AAk');  
  13. Plan hash value: 3520792594  
  14.   
  15. --------------------------------------------------------------------------------  
  16. ------------  
  17.   
  18. | Id  | Operation                  | Name          | Rows  | Bytes | Cost (%CPU)  
  19. Time     |  
  20.   
  21. --------------------------------------------------------------------------------  
  22. ------------  
  23.   
  24. |   0 | SELECT STATEMENT           |               |     1 |    17 |     1   (0)  
  25. | 00:00:01 |  
  26.   
  27. |   1 |  TABLE ACCESS BY USER ROWID| T_ROVER_ALERT |     1 |    17 |     1   (0)  
  28. | 00:00:01 |  
  29.   
  30. --------------------------------------------------------------------------------  
  31. ------------  
  32.   
  33.   
  34.   
  35. Statistics  
  36. ----------------------------------------------------------  
  37.         173  recursive calls  
  38.           0  db block gets  
  39.          42  consistent gets  
  40.           0  physical reads  
  41.           0  redo size  
  42.         594  bytes sent via SQL*Net to client  
  43.         469  bytes received via SQL*Net from client  
  44.           2  SQL*Net roundtrips to/from client  
  45.           5  sorts (memory)  
  46.           0  sorts (disk)  
  47.           1  rows processed  
  48.   
  49. SQL> SQL>   
  50.   
  51. VROWID                                                         ID  
  52. ------------------------------------------------------ ----------  
  53. AAA5eEAAFAAABS2AAk                                         268042  
  54.   
  55.   
  56. Execution Plan  
  57. ----------------------------------------------------------  
  58.   
  59. Plan hash value: 3520792594  
  60.   
  61. --------------------------------------------------------------------------------  
  62. ------------  
  63.   
  64. | Id  | Operation                  | Name          | Rows  | Bytes | Cost (%CPU)  
  65. Time     |  
  66.   
  67. --------------------------------------------------------------------------------  
  68. ------------  
  69.   
  70. |   0 | SELECT STATEMENT           |               |     1 |    17 |     1   (0)  
  71. | 00:00:01 |  
  72.   
  73. |   1 |  TABLE ACCESS BY USER ROWID| T_ROVER_ALERT |     1 |    17 |     1   (0)  
  74. | 00:00:01 |  
  75.   
  76. --------------------------------------------------------------------------------  
  77. ------------  
  78.   
  79.   
  80.   
  81. Statistics  
  82. ----------------------------------------------------------  
  83.           1  recursive calls  
  84.           0  db block gets  
  85.           1  consistent gets  
  86.           0  physical reads  
  87.           0  redo size  
  88.         594  bytes sent via SQL*Net to client  
  89.         469  bytes received via SQL*Net from client  
  90.           2  SQL*Net roundtrips to/from client  
  91.           0  sorts (memory)  
  92.           0  sorts (disk)  
  93.           1  rows processed  
  94.   
  95. SQL> SQL> SQL> select ROWIDTOCHAR(rowid) vrowid,id  from t_rover_alert where ROWIDTOCHAR(rowid) in (trim('AAA5eEAAFAAABS2AAk'));  
  96.   
  97. VROWID                                                         ID  
  98. ------------------------------------------------------ ----------  
  99. AAA5eEAAFAAABS2AAk                                         268042  
  100.   
  101.   
  102. Execution Plan  
  103. ----------------------------------------------------------  
  104. Plan hash value: 1380109762  
  105.   
  106. --------------------------------------------------------------------------------  
  107. -----  
  108.   
  109. | Id  | Operation        | Name             | Rows  | Bytes | Cost (%CPU)| Time  
  110.     |  
  111.   
  112. --------------------------------------------------------------------------------  
  113. -----  
  114.   
  115. |   0 | SELECT STATEMENT |                  |     2 |    34 |     1   (0)| 00:00  
  116. :01 |  
  117.   
  118. |*  1 |  INDEX FULL SCAN | T_ROVER_ALERT_PK |     2 |    34 |     1   (0)| 00:00  
  119. :01 |  
  120.   
  121. --------------------------------------------------------------------------------  
  122. -----  
  123.   
  124.   
  125. Predicate Information (identified by operation id):  
  126. ---------------------------------------------------  
  127.   
  128.    1 - filter(ROWIDTOCHAR(ROWID)='AAA5eEAAFAAABS2AAk')  
  129.   
  130.   
  131. Statistics  
  132. ----------------------------------------------------------  
  133.           1  recursive calls  
  134.           0  db block gets  
  135.           2  consistent gets  
  136.           0  physical reads  
  137.           0  redo size  
  138.         594  bytes sent via SQL*Net to client  
  139.         469  bytes received via SQL*Net from client  
  140.           2  SQL*Net roundtrips to/from client  
  141.           0  sorts (memory)  
  142.           0  sorts (disk)  
  143.           1  rows processed  
  144.   
  145. SQL>   

附上解析rowid的函数

  1. create or replace function get_rowid  
  2. (l_rowid in varchar2) return varchar2 is  
  3.   ls_my_rowid   varchar2(200);  
  4.   rowid_type    number;  
  5.   object_number number;  
  6.   relative_fno  number;  
  7.   block_number  number;  
  8.   row_number    number;  
  9.   
  10. begin  
  11.   if length(trim(l_rowid))!=18 then  
  12.       return 'input error';  
  13.   end if;  
  14.   dbms_rowid.rowid_info(l_rowid,  
  15.                         rowid_type,  
  16.                         object_number,  
  17.                         relative_fno,  
  18.                         block_number,  
  19.                         row_number);  
  20.   ls_my_rowid := 'Object# is      :' || to_char(object_number) || chr(10) ||  
  21.                  'Relative_fno is :' || to_char(relative_fno) || chr(10) ||  
  22.                  'Block number is :' || to_char(block_number) || chr(10) ||  
  23.                  'Row number is   :' || to_char(row_number);  
  24.   return ls_my_rowid;  
  25. end;  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值