[20190312]视图v$datafile字段OFFLINE_CHANGE#, ONLINE_CHANGE#.txt

时间:2021-10-04 09:49:00

[20190312]视图v$datafile字段OFFLINE_CHANGE#, ONLINE_CHANGE#.txt

--//视图v$datafile存在2个字段OFFLINE_CHANGE#, ONLINE_CHANGE#,想当然会认为数据文件offline时记录scn号的改变.
--//真的吗?通过例子说明:

1.环境:
SYS@book> @ ver1
PORT_STRING                    VERSION        BANNER
------------------------------ -------------- --------------------------------------------------------------------------------
x86_64/Linux 2.4.xx            11.2.0.4.0     Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production

SYS@book> SELECT file#, CHECKPOINT_CHANGE#,CHECKPOINT_TIME,UNRECOVERABLE_CHANGE#,UNRECOVERABLE_TIME,LAST_CHANGE#,LAST_TIME, OFFLINE_CHANGE#, ONLINE_CHANGE#,FIRST_NONLOGGED_SCN,FIRST_NONLOGGED_TIME,status,name FROM v$datafile;
FILE# CHECKPOINT_CHANGE# CHECKPOINT_TIME     UNRECOVERABLE_CHANGE# UNRECOVERABLE_TIME  LAST_CHANGE# LAST_TIME           OFFLINE_CHANGE# ONLINE_CHANGE# FIRST_NONLOGGED_SCN FIRST_NONLOGGED_TIM STATUS  NAME
----- ------------------ ------------------- --------------------- ------------------- ------------ ------------------- --------------- -------------- ------------------- ------------------- ------- --------------------------------------------------
    1        13279958095 2019-03-12 16:40:08                     0                                                                    0              0                   0                     SYSTEM  /mnt/ramdisk/book/system01.dbf
    2        13279958095 2019-03-12 16:40:08                     0                                                                    0              0                   0                     ONLINE  /mnt/ramdisk/book/sysaux01.dbf
    3        13279958095 2019-03-12 16:40:08                     0                                                                    0              0                   0                     ONLINE  /mnt/ramdisk/book/undotbs01.dbf
    4        13279958095 2019-03-12 16:40:08                     0                                                                    0              0                   0                     ONLINE  /mnt/ramdisk/book/users01.dbf
    5        13279958095 2019-03-12 16:40:08                     0                                                                    0              0                   0                     ONLINE  /mnt/ramdisk/book/example01.dbf
    6        13279958095 2019-03-12 16:40:08                     0                                                                    0              0                   0                     ONLINE  /mnt/ramdisk/book/tea01.dbf
6 rows selected.

--//我重建了控制文件,许多字段当前是空的.

2.测试:
SYS@book> alter database datafile 6 offline ;
Database altered.

SYS@book>  SELECT file#, CHECKPOINT_CHANGE#,CHECKPOINT_TIME,UNRECOVERABLE_CHANGE#,UNRECOVERABLE_TIME,LAST_CHANGE#,LAST_TIME, OFFLINE_CHANGE#, ONLINE_CHANGE#,FIRST_NONLOGGED_SCN,FIRST_NONLOGGED_TIME,status,name FROM v$datafile where file# in (1,6);
FILE# CHECKPOINT_CHANGE# CHECKPOINT_TIME     UNRECOVERABLE_CHANGE# UNRECOVERABLE_TIME  LAST_CHANGE# LAST_TIME           OFFLINE_CHANGE# ONLINE_CHANGE# FIRST_NONLOGGED_SCN FIRST_NONLOGGED_TIM STATUS  NAME
----- ------------------ ------------------- --------------------- ------------------- ------------ ------------------- --------------- -------------- ------------------- ------------------- ------- --------------------------------------------------
    1        13279958095 2019-03-12 16:40:08                     0                                                                    0              0                   0                     SYSTEM  /mnt/ramdisk/book/system01.dbf
    6        13279958095 2019-03-12 16:40:08                     0                      13279959313 2019-03-12 16:53:06               0              0                   0                     RECOVER /mnt/ramdisk/book/tea01.dbf

--//实际上offline 数据文件时,在控制文件记录的是LAST_CHANGE#,LAST_TIME.

SYS@book> recover datafile 6;
Media recovery complete.
--//注无法直接online,要执行recover.所以如果有需求要offline,应该养成随手执行recover datafile N的习惯.

SYS@book>  SELECT file#, CHECKPOINT_CHANGE#,CHECKPOINT_TIME,UNRECOVERABLE_CHANGE#,UNRECOVERABLE_TIME,LAST_CHANGE#,LAST_TIME, OFFLINE_CHANGE#, ONLINE_CHANGE#,FIRST_NONLOGGED_SCN,FIRST_NONLOGGED_TIME,status,name FROM v$datafile where file# in (1,6);
FILE# CHECKPOINT_CHANGE# CHECKPOINT_TIME     UNRECOVERABLE_CHANGE# UNRECOVERABLE_TIME  LAST_CHANGE# LAST_TIME           OFFLINE_CHANGE# ONLINE_CHANGE# FIRST_NONLOGGED_SCN FIRST_NONLOGGED_TIM STATUS  NAME
----- ------------------ ------------------- --------------------- ------------------- ------------ ------------------- --------------- -------------- ------------------- ------------------- ------- --------------------------------------------------
    1        13279958095 2019-03-12 16:40:08                     0                                                                    0              0                   0                     SYSTEM  /mnt/ramdisk/book/system01.dbf
    6        13279959313 2019-03-12 16:53:06                     0                      13279959313 2019-03-12 16:53:06               0              0                   0                     OFFLINE /mnt/ramdisk/book/tea01.dbf

--//CHECKPOINT_CHANGE#=LAST_CHANGE#.status 从RECOVER=>OFFLINE.  看看看看文件头的情况:

SYS@book>  SELECT file#, CHECKPOINT_CHANGE#, CHECKPOINT_TIME,CREATION_CHANGE#  , RESETLOGS_CHANGE#,status, CHECKPOINT_COUNT,fuzzy,name,tablespace_name  FROM v$datafile_header where file# in (1,6);
FILE# CHECKPOINT_CHANGE# CHECKPOINT_TIME     CREATION_CHANGE# RESETLOGS_CHANGE# STATUS  CHECKPOINT_COUNT FUZ NAME                                               TABLESPACE_NAME
----- ------------------ ------------------- ---------------- ----------------- ------- ---------------- --- -------------------------------------------------- ------------------------------
    1        13279958095 2019-03-12 16:40:08                7            925702 ONLINE              1224 YES /mnt/ramdisk/book/system01.dbf                     SYSTEM
    6        13279959313 2019-03-12 16:53:06      13276257767            925702 OFFLINE              607 NO  /mnt/ramdisk/book/tea01.dbf                        TEA

--//文件头的CHECKPOINT_CHANGE#与控制文件CHECKPOINT_CHANGE#一致.
--//注:v$datafile的信息来自控制文件,v$datafile_header的信息来自数据文件头,不要搞混了.

SYS@book> alter database datafile 6 online ;
Database altered.

SYS@book>  SELECT file#, CHECKPOINT_CHANGE#,CHECKPOINT_TIME,UNRECOVERABLE_CHANGE#,UNRECOVERABLE_TIME,LAST_CHANGE#,LAST_TIME, OFFLINE_CHANGE#, ONLINE_CHANGE#,FIRST_NONLOGGED_SCN,FIRST_NONLOGGED_TIME,status,name FROM v$datafile where file# in (1,6);
FILE# CHECKPOINT_CHANGE# CHECKPOINT_TIME     UNRECOVERABLE_CHANGE# UNRECOVERABLE_TIME  LAST_CHANGE# LAST_TIME           OFFLINE_CHANGE# ONLINE_CHANGE# FIRST_NONLOGGED_SCN FIRST_NONLOGGED_TIM STATUS  NAME
----- ------------------ ------------------- --------------------- ------------------- ------------ ------------------- --------------- -------------- ------------------- ------------------- ------- --------------------------------------------------
    1        13279958095 2019-03-12 16:40:08                     0                                                                    0              0                   0                     SYSTEM  /mnt/ramdisk/book/system01.dbf
    6        13279960343 2019-03-12 16:58:40                     0                                                                    0              0                   0                     ONLINE  /mnt/ramdisk/book/tea01.dbf

--//可以发现数据文件 online后,OFFLINE_CHANGE#,ONLINE_CHANGE#字段并没有任何记录.也就是这个字段并不是记录数据文件offline的scn号.

3.继续测试:
--//既然不是数据文件offline时记录scn号,自然想到表空间的offline,online有关.

SYS@book> alter tablespace tea offline ;
Tablespace altered.

SYS@book>  SELECT file#, CHECKPOINT_CHANGE#,CHECKPOINT_TIME,UNRECOVERABLE_CHANGE#,UNRECOVERABLE_TIME,LAST_CHANGE#,LAST_TIME, OFFLINE_CHANGE#, ONLINE_CHANGE#,FIRST_NONLOGGED_SCN,FIRST_NONLOGGED_TIME,status,name FROM v$datafile where file# in (1,6);
FILE# CHECKPOINT_CHANGE# CHECKPOINT_TIME     UNRECOVERABLE_CHANGE# UNRECOVERABLE_TIME  LAST_CHANGE# LAST_TIME           OFFLINE_CHANGE# ONLINE_CHANGE# FIRST_NONLOGGED_SCN FIRST_NONLOGGED_TIM STATUS  NAME
----- ------------------ ------------------- --------------------- ------------------- ------------ ------------------- --------------- -------------- ------------------- ------------------- ------- --------------------------------------------------
    1        13279958095 2019-03-12 16:40:08                     0                                                                    0              0                   0                     SYSTEM  /mnt/ramdisk/book/system01.dbf
    6        13279960510 2019-03-12 17:01:12                     0                      13279960510 2019-03-12 17:01:12               0              0                   0                     OFFLINE /mnt/ramdisk/book/tea01.dbf

--//表空间offline,缺省要更新文件头的scn,你可以发现STATUS=OFFLINE(而不是recover).CHECKPOINT_CHANGE#=LAST_CHANGE#.
--//注 : alter tablespace tea offline immediate ;.这样不更新文件头,与offline 数据文件类似,你可以理解一组文件(表空间)offline.大家可以自行测试.

SYS@book> alter tablespace tea online ;
Tablespace altered.

SYS@book>  SELECT file#, CHECKPOINT_CHANGE#,CHECKPOINT_TIME,UNRECOVERABLE_CHANGE#,UNRECOVERABLE_TIME,LAST_CHANGE#,LAST_TIME, OFFLINE_CHANGE#, ONLINE_CHANGE#,FIRST_NONLOGGED_SCN,FIRST_NONLOGGED_TIME,status,name FROM v$datafile where file# in (1,6);
FILE# CHECKPOINT_CHANGE# CHECKPOINT_TIME     UNRECOVERABLE_CHANGE# UNRECOVERABLE_TIME  LAST_CHANGE# LAST_TIME           OFFLINE_CHANGE# ONLINE_CHANGE# FIRST_NONLOGGED_SCN FIRST_NONLOGGED_TIM STATUS  NAME
----- ------------------ ------------------- --------------------- ------------------- ------------ ------------------- --------------- -------------- ------------------- ------------------- ------- --------------------------------------------------
    1        13279958095 2019-03-12 16:40:08                     0                                                                    0              0                   0                     SYSTEM  /mnt/ramdisk/book/system01.dbf
    6        13279960695 2019-03-12 17:03:55                     0                                                          13279960510    13279960695                   0                     ONLINE  /mnt/ramdisk/book/tea01.dbf

--//可以发现表空间tea online后,OFFLINE_CHANGE#,ONLINE_CHANGE#有信息,OFFLINE_CHANGE#记录就是表空间offline时的scn,而ONLINE_CHANGE#记录就是表空间online时的scn.
--//oracle为什么这样设计,假设你需要恢复1个数据文件,取出来的数据文件scn小于OFFLINE_CHANGE#,这样恢复时,从OFFLINE_CHANGE# 到 ONLINE_CHANGE#的日志或者归档可以跳过,
--//节约日志应用与恢复时间.

--//自然要问,为什么数据文件offline时为什么没有类似的记录呢?而数据文件offline时,处于"不稳定状态",不能直接online的,这样控制文件仅仅记录LAST_CHANGE#(offline时).
--//这样恢复时日志仅仅应用到LAST_CHANGE#就ok了,但是为什么数据文件online时LAST_CHANGE#的信息不写入OFFLINE_CHANGE#,online时的scn写入ONLINE_CHANGE#呢?
--//感觉这个存在一点点歧义行,个人理解.

--//也许oracle在视图字段命名上不科学,应该将OFFLINE_CHANGE#, ONLINE_CHANGE#命名为TABLESPACE_OFFLINE_CHANGE#,TABLESPACE_ONLINE_CHANGE#更加科学一些.