oracle rowid的理解

 

一. 官网说明

       owid的定义:A globally unique address for a row in a database.

       rowid 分为extended rowid 和 restricted rowid.

1.1 Restricted ROWID

       Internally, the ROWID is a structure that holds information that the database server needs to access a row.The restricted internal ROWID is 6 bytes on most platforms.

Each restricted rowid includes the following data:

       (1)Datafile identifier

       (2)Block identifier

       (3)Row identifier

       The restricted ROWID pseudocolumn is returned to client applications in the form of an 18-character string with a hexadecimal encoding of the datablock, row, and datafile components of the ROWID.

1.2  Extended ROWID

       The extended ROWID datatype includes the data in the restricted rowid plus a data object number. The data object number is an identification number assigned to every database segment. The extended internal ROWID is 10 bytes on most platforms.

       Data in an extended ROWID pseudocolumn is returned to the client application in the form of an 18-character string (for example, "AAAA8mAALAAAAQkAAA"), which represents a base 64 encoding of the components of the extended ROWID in a four-piece format, OOOOOOFFFBBBBBBRRR. Extended rowids are not available directly. You can use a supplied package, DBMS_ROWID, to interpret extended rowid contents. The package functions extract and provide information that would be available directly from a restricted rowid as well as information specific to extended rowids.

 

1.3 Rowid Format

       Oracle Database uses a rowid to uniquely identify a row. Internally, the rowid is a structure that holds information that the database needs to access a row. A rowid is not physically stored in the database, but is inferred from the file and block on which the data is stored.

       An extended rowid includes a data object number. This rowid type uses a base 64 encoding of the physical address for each row. The encoding characters are A-Z, a-z, 0-9, +, and /.

Example 12-1 queries the ROWID pseudocolumn to show the extended rowid of the row in the employees table for employee 100.
Example 12-1 ROWID Pseudocolumn

SQL> SELECT ROWID FROM employees WHERE employee_id = 100;

ROWID

------------------

AAAPecAAFAAAABSAAA

Figure 12-8 illustrates the format of an extended rowid.
Figure 12-8 ROWID Format

An extended rowid is displayed in a four-piece format, OOOOOOFFFBBBBBBRRR, with the format divided into the following components:

(1)OOOOOO

       The data object number identifies the segment (data object AAAPec in Example 12-1). A data object number is assigned to every database segment. Schema objects in the same segment, such as a table cluster, have the same data object number.

(2)FFF

       The tablespace-relative data file number identifies the data file that contains the row (file AAF in Example 12-1).

(3)BBBBBB

       The data block number identifies the block that contains the row (block AAAABS in Example 12-1). Block numbers are relative to their data file, not their tablespace. Thus, two rows with identical block numbers could reside in different data files of the same tablespace.

(4)RRR

       The row number identifies the row in the block (row AAA in Example 12-1).

  

       After a rowid is assigned to a row piece, the rowid can change in special circumstances. For example,

(1)if row movement is enabled, then the rowid can change because of partition key updates, Flashback Table operations, shrink table operations, and so on.

(2)If row movement is disabled, then a rowid can change if the row is exported and imported using Oracle Database utilities.

 

二、RowId 说明

      rowid 是伪列,用来唯一的定位某一数据行,rowid在数据库中不是物理存在的,是行的一个推导属性,在查询时根据数据行所在的文件和数据块构造出来的一个伪列

      rowid是存储在索引中的一个既定值(当行确定后),rowid是访问表中一行最快的方式。

 

三、RowId结构

         扩展的rowid格式:

             OOOOOO              FFF                BBBBBB          RRR

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

 

         受限的rowid格式:

             BBBBBBBB            RRRR        FFFF

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


     这里我们主要讨论oracle8i以后的扩展的rowid, 扩展的ROWID 在磁盘上需要10 个字节的存储空间,并使用18 个字符来显示。

      它包含下列组成元素:

       1. 数据对象编号:每个数据对象(如表或索引)在创建时都分配有此编号,并且此编号在数据库中是唯一的

       2. 文件编号:此编号对于表空间中的每个数据文件是唯一的

       3. 块编号:表示包含此行的块在数据文件中的位置

       4. 行编号:标识块头中行目录位置

       在内部,存储的10个字节(bytes),即80位(bit)又按如下规则进行划分:

       (1)数据对象编号需要32 bit    (每个数据库最多有4G个对象)

       (2)相关文件编号需要10 bit    (每个对象最多有1022个文件(2个文件预留))

       (3)块编号需要22 bit  (每个文件最多有4M个BLOCK)

       (4)行编号需要16 bit  (每个BLOCK最多有64K个ROW)

      结论:oracle数据库的对象数和数据文件数是受rowid结构格式限制的。

      扩展的ROWID 使用以64 为基数的编码方案来显示,该方案将六个位置用于数据对象编号、三个位置用于相关文件编号、六个位置用于块编号、三个位置用于行编号。 以64 为基数的编码方案使用字符“A-Z”、“a-z”、“0-9” 和“/”。共有64 个字符,

根据dbms_rowid包,我们还可以得到rowid相关的具体信息,如下例所示:

SQL> SELECT rowid,
  2   dbms_rowid.rowid_object(rowid) obj_id,
  3   dbms_rowid.rowid_relative_fno(rowid) datafile#,
  4   dbms_rowid.rowid_block_number(rowid) blocknum,
  5   dbms_rowid.rowid_row_number(rowid) rowno
  6   from td_s_resstate where rownum<5;
 
ROWID                  OBJ_ID  DATAFILE#   BLOCKNUM      ROWNO
------------------ ---------- ---------- ---------- ----------
AAALB3AA9AAACVkAAa      45175         61       9572         26
AAALB3AA9AAACVkAAb      45175         61       9572         27
AAALB3AA9AAACVkAAc      45175         61       9572         28
AAALB3AA9AAACVkAAd      45175         61       9572         29
 


上面例子的第一条记录rowid:AAALB3AA9AAACVkAAa:

      数据对象编号:AAALB3

      文件编号:AA9

      块编号:AAACVk

      行编号:AAa

 

四、RowId的改变与用途:

      对于有ROWID 的行(Oracle 中最常见的行“类型”;除了IOT 中的行之外,所有行都有ROWID), 以前ROWID 是不可变的。插入一行时,会为之关联一个ROWID(一个地址),而且这个ROWID 会一直与该行关联,直到这一行被删除(被物理地从数据库删除)。但是,后来情况发生了变化,因为现在有些操作可能会导致行的ROWID 改变,例如:
              1.在分区表中更新一行的分区键,使这一行必须从一个分区移至另一个分区。
              2.使用FLASHBACK TABLE 命令将一个数据库表恢复到以前的每个时间点。
              3.执行MOVE 操作以及许多分区操作,如分解或合并分区。
              4.使用ALTER TABLE SHRINK SPACE 命令执行段收缩。

              5.对一个表进行了EXP/IMP。

如今,由于ROWID 可能过一段时间会改变(因为它不再是不可变的),所以不建议把它们作为单独的列物理地存储在数据库表中。也就是说,使用ROWID 作为一个数据库列的数据类型被认为是一种不好的实践做法。应当避免这种做法,而应使用行的主键(这应该是不可变的),另外引用完整性可以确保数据的完整性。对此用ROWID 类型是做不到的,不能用ROWID 创建从子表到一个父表的外键,而且不能保证跨表的完整性。你必须使用主键约束。

那ROWID 类型有什么用呢?在允许最终用户与数据交互的应用中,ROWID 还是有用的。ROWID 作为行的一个物理地址,要访问任何表中的某一行,这是最快的方法。如果应用从数据库读出数据并将其提供给最终用户,它试图更新这一行时就可以使用ROWID。应用这种方式,只需最少的工作就可以更新当前行(例如,不需要索引查找再次寻找行),并通过验证行值未被修改来确保这一行与最初读出的行是同一行。所以,在采用乐观锁定的应用中ROWID 还是有用的。

 

五、RowId的常见sql使用:

 1、删除表中多余的重复记录(多个字段),只留有rowid最小的记录  
  delete   from   vitae   a  
  where   (a.peopleId,a.seq)   in     (select   peopleId,seq   from   vitae   group   by   peopleId,seq   having   count(*)   >   1)  
  and   rowid   not   in   (select   min(rowid)   from   vitae   group   by   peopleId,seq   having   count(*)>1)   
    
 2、查找表中多余的重复记录(多个字段),不包含rowid最小的记录  
  select   *   from   vitae   a  
  where   (a.peopleId,a.seq)   in     (select   peopleId,seq   from   vitae   group   by   peopleId,seq   having   count(*)   >   1)  
  and   rowid   not   in   (select   min(rowid)   from   vitae   group   by   peopleId,seq   having   count(*)>1)

 

附:UROWID(rowid的‘近亲’)

UROWID(Universal Rowid)表示一行数据的逻辑地址。一般情况下,索引组织表(IOT)和远程数据库(可以是非Oracle数据库)中的表需要用到UROWID。

UROWID 是行主键值的一个表示,因此,其大小不定,这取决于它指向的对象。

UROWID 基于行的主键生成,所以从某种意义上讲,好像它是随行存储的,但是事实上并非如此,因为UROWID 并不作为一个单独的列存在,而只是作为现有列的一个函数。


UROWID的子类型

  • 物理的(实际上就是物理ROWID加一个子类型标记字节)
  • 逻辑的(基于主键,用于IOT)
  • 远程的(其他库中的表)

 


 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值