Oracle数据库中的4种SCN号

一个SCN号就是一个oracle数据库中使用的逻辑的、内部的时间戳。这个SCN号可以表明一个事件在oracle数据库中发生的精确时间。

Oracle数据库中的SCN号分为四种,分别为:系统检查点SCN数据文件scn结束scn:数据文件头scn


系统检查点SCN:

系统检查点SCN位于控制文件中,当检查点进程启动时(ckpt),Oracle就把系统检查点的SCN存储到控制文件中。

查询系统检查点SCN的命令如下 

SQL> select CHECKPOINT_CHANGE# from v$database;

CHECKPOINT_CHANGE#
------------------
	   2017133

数据文件SCN:

当ckpt进程启动时,包括全局范围的(比如日志切换)以及文件级别的检查点(将表空间置为只读、begin backup或将某个数据文件设置为offline等),这时会在控制文件中记录的scn。
查询数据文件SCN的命令如下

SQL> select a.TS#,a.name,b.name,b.checkpoint_change# from v$tablespace a, v$datafile b where a.ts#=b.ts#(+) order by ts#;

       TS# NAME 					      NAME						 CHECKPOINT_CHANGE#
---------- -------------------------------------------------- -------------------------------------------------- ------------------
	 0 SYSTEM					      /u01/app/oracle/oradata/PROD1/system01.dbf		    2028292
	 1 SYSAUX					      /u01/app/oracle/oradata/PROD1/sysaux01.dbf		    2028292
	 2 UNDOTBS1					      /u01/app/oracle/oradata/PROD1/undotbs01.dbf		    2028292
	 3 TEMP
	 4 USERS					      /u01/app/oracle/oradata/PROD1/users01.dbf 		    2028292
	 6 EXAMPLE					      /u01/app/oracle/oradata/PROD1/example01.dbf		    2028292
	 7 TEST 					      /u01/app/oracle/oradata/PROD1/test.tbs			    2028292
	 8 DEV_MDS					      /u01/app/oracle/oradata/PROD1/DEV_mds.dbf 		    2028292
	 9 DEV_IAS_TEMP
	10 DEV_BIPLATFORM				      /u01/app/oracle/oradata/PROD1/DEV_biplatform.dbf		    2028292
	11 DEV1_ODI_TEMP
	13 DEV2_ODI_TEMP
	14 DEV2_ODI_USER				      /u01/app/oracle/oradata/PROD1/DEV2_odi_user.dbf		    2028292

13 rows selected.

SQL> alter tablespace test read only;

Tablespace altered.

SQL> select a.TS#,a.name,b.name,b.checkpoint_change# from v$tablespace a, v$datafile b where a.ts#=b.ts#(+) order by ts#;

       TS# NAME 					      NAME						 CHECKPOINT_CHANGE#
---------- -------------------------------------------------- -------------------------------------------------- ------------------
	 0 SYSTEM					      /u01/app/oracle/oradata/PROD1/system01.dbf		    2028292
	 1 SYSAUX					      /u01/app/oracle/oradata/PROD1/sysaux01.dbf		    2028292
	 2 UNDOTBS1					      /u01/app/oracle/oradata/PROD1/undotbs01.dbf		    2028292
	 3 TEMP
	 4 USERS					      /u01/app/oracle/oradata/PROD1/users01.dbf 		    2028292
	 6 EXAMPLE					      /u01/app/oracle/oradata/PROD1/example01.dbf		    2028292
	 7 TEST 					      /u01/app/oracle/oradata/PROD1/test.tbs			    2028314
	 8 DEV_MDS					      /u01/app/oracle/oradata/PROD1/DEV_mds.dbf 		    2028292
	 9 DEV_IAS_TEMP
	10 DEV_BIPLATFORM				      /u01/app/oracle/oradata/PROD1/DEV_biplatform.dbf		    2028292
	11 DEV1_ODI_TEMP
	13 DEV2_ODI_TEMP
	14 DEV2_ODI_USER				      /u01/app/oracle/oradata/PROD1/DEV2_odi_user.dbf		    2028292

13 rows selected.

SQL> alter tablespace test read write;

Tablespace altered.

SQL> select a.TS#,a.name,b.name,b.checkpoint_change# from v$tablespace a, v$datafile b where a.ts#=b.ts#(+) order by ts#;

       TS# NAME 					      NAME						 CHECKPOINT_CHANGE#
---------- -------------------------------------------------- -------------------------------------------------- ------------------
	 0 SYSTEM					      /u01/app/oracle/oradata/PROD1/system01.dbf		    2028292
	 1 SYSAUX					      /u01/app/oracle/oradata/PROD1/sysaux01.dbf		    2028292
	 2 UNDOTBS1					      /u01/app/oracle/oradata/PROD1/undotbs01.dbf		    2028292
	 3 TEMP
	 4 USERS					      /u01/app/oracle/oradata/PROD1/users01.dbf 		    2028292
	 6 EXAMPLE					      /u01/app/oracle/oradata/PROD1/example01.dbf		    2028292
	 7 TEST 					      /u01/app/oracle/oradata/PROD1/test.tbs			    2028338
	 8 DEV_MDS					      /u01/app/oracle/oradata/PROD1/DEV_mds.dbf 		    2028292
	 9 DEV_IAS_TEMP
	10 DEV_BIPLATFORM				      /u01/app/oracle/oradata/PROD1/DEV_biplatform.dbf		    2028292
	11 DEV1_ODI_TEMP
	13 DEV2_ODI_TEMP
	14 DEV2_ODI_USER				      /u01/app/oracle/oradata/PROD1/DEV2_odi_user.dbf		    2028292

13 rows selected.

SQL> alter system checkpoint;

System altered.

SQL> select a.TS#,a.name,b.name,b.checkpoint_change# from v$tablespace a, v$datafile b where a.ts#=b.ts#(+) order by ts#;

       TS# NAME 					      NAME						 CHECKPOINT_CHANGE#
---------- -------------------------------------------------- -------------------------------------------------- ------------------
	 0 SYSTEM					      /u01/app/oracle/oradata/PROD1/system01.dbf		    2028370
	 1 SYSAUX					      /u01/app/oracle/oradata/PROD1/sysaux01.dbf		    2028370
	 2 UNDOTBS1					      /u01/app/oracle/oradata/PROD1/undotbs01.dbf		    2028370
	 3 TEMP
	 4 USERS					      /u01/app/oracle/oradata/PROD1/users01.dbf 		    2028370
	 6 EXAMPLE					      /u01/app/oracle/oradata/PROD1/example01.dbf		    2028370
	 7 TEST 					      /u01/app/oracle/oradata/PROD1/test.tbs			    2028370
	 8 DEV_MDS					      /u01/app/oracle/oradata/PROD1/DEV_mds.dbf 		    2028370
	 9 DEV_IAS_TEMP
	10 DEV_BIPLATFORM				      /u01/app/oracle/oradata/PROD1/DEV_biplatform.dbf		    2028370
	11 DEV1_ODI_TEMP
	13 DEV2_ODI_TEMP
	14 DEV2_ODI_USER				      /u01/app/oracle/oradata/PROD1/DEV2_odi_user.dbf		    2028370

13 rows selected.
从上述结果中可以看到当我们将test表空间转换为read only状态后,他的scn号比其他数据未见SCN号要大。


结束SCN:

每个数据文件都有一个结束scn,在数据库的正常运行中,只要数据文件在线且是可读写的,结束scn为null。否则则存在具体的scn值。结束scn也记录在控制文件中。

SQL> select a.TS#,a.name,b.name,b.checkpoint_change#,b.LAST_CHANGE# from v$tablespace a, v$datafile b where a.ts#=b.ts#(+) order by ts#;

       TS# NAME 					      NAME						 CHECKPOINT_CHANGE# LAST_CHANGE#
---------- -------------------------------------------------- -------------------------------------------------- ------------------ ------------
	 0 SYSTEM					      /u01/app/oracle/oradata/PROD1/system01.dbf		    2028370
	 1 SYSAUX					      /u01/app/oracle/oradata/PROD1/sysaux01.dbf		    2028370
	 2 UNDOTBS1					      /u01/app/oracle/oradata/PROD1/undotbs01.dbf		    2028370
	 3 TEMP
	 4 USERS					      /u01/app/oracle/oradata/PROD1/users01.dbf 		    2028370
	 6 EXAMPLE					      /u01/app/oracle/oradata/PROD1/example01.dbf		    2028370
	 7 TEST 					      /u01/app/oracle/oradata/PROD1/test.tbs			    2028370
	 8 DEV_MDS					      /u01/app/oracle/oradata/PROD1/DEV_mds.dbf 		    2028370
	 9 DEV_IAS_TEMP
	10 DEV_BIPLATFORM				      /u01/app/oracle/oradata/PROD1/DEV_biplatform.dbf		    2028370
	11 DEV1_ODI_TEMP

       TS# NAME 					      NAME						 CHECKPOINT_CHANGE# LAST_CHANGE#
---------- -------------------------------------------------- -------------------------------------------------- ------------------ ------------
	13 DEV2_ODI_TEMP
	14 DEV2_ODI_USER				      /u01/app/oracle/oradata/PROD1/DEV2_odi_user.dbf		    2028370

13 rows selected.

SQL> alter tablespace test read only;

Tablespace altered.

SQL> select a.TS#,a.name,b.name,b.checkpoint_change#,b.LAST_CHANGE# from v$tablespace a, v$datafile b where a.ts#=b.ts#(+) order by ts#;

       TS# NAME 					      NAME						 CHECKPOINT_CHANGE# LAST_CHANGE#
---------- -------------------------------------------------- -------------------------------------------------- ------------------ ------------
	 0 SYSTEM					      /u01/app/oracle/oradata/PROD1/system01.dbf		    2028370
	 1 SYSAUX					      /u01/app/oracle/oradata/PROD1/sysaux01.dbf		    2028370
	 2 UNDOTBS1					      /u01/app/oracle/oradata/PROD1/undotbs01.dbf		    2028370
	 3 TEMP
	 4 USERS					      /u01/app/oracle/oradata/PROD1/users01.dbf 		    2028370
	 6 EXAMPLE					      /u01/app/oracle/oradata/PROD1/example01.dbf		    2028370
	 7 TEST 					      /u01/app/oracle/oradata/PROD1/test.tbs			    2029996	 2029996
	 8 DEV_MDS					      /u01/app/oracle/oradata/PROD1/DEV_mds.dbf 		    2028370
	 9 DEV_IAS_TEMP
	10 DEV_BIPLATFORM				      /u01/app/oracle/oradata/PROD1/DEV_biplatform.dbf		    2028370
	11 DEV1_ODI_TEMP

       TS# NAME 					      NAME						 CHECKPOINT_CHANGE# LAST_CHANGE#
---------- -------------------------------------------------- -------------------------------------------------- ------------------ ------------
	13 DEV2_ODI_TEMP
	14 DEV2_ODI_USER				      /u01/app/oracle/oradata/PROD1/DEV2_odi_user.dbf		    2028370

13 rows selected.

可见test表空间的数据文件结束SCN不为空,而其他数据文件结束SCN均为空。

如果将数据库处于mount状态下,所有数据文件均不可写,所以都具有结束SCN。

SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup mount;
ORACLE instance started.

Total System Global Area  722366464 bytes
Fixed Size		    2216864 bytes
Variable Size		  251661408 bytes
Database Buffers	  461373440 bytes
Redo Buffers		    7114752 bytes
Database mounted.
SQL> select a.TS#,a.name,b.name,b.checkpoint_change#,b.LAST_CHANGE# from v$tablespace a, v$datafile b where a.ts#=b.ts#(+) order by ts#;

       TS# NAME 					      NAME						 CHECKPOINT_CHANGE# LAST_CHANGE#
---------- -------------------------------------------------- -------------------------------------------------- ------------------ ------------
	 0 SYSTEM					      /u01/app/oracle/oradata/PROD1/system01.dbf		    2030818	 2030818
	 1 SYSAUX					      /u01/app/oracle/oradata/PROD1/sysaux01.dbf		    2030818	 2030818
	 2 UNDOTBS1					      /u01/app/oracle/oradata/PROD1/undotbs01.dbf		    2030818	 2030818
	 3 TEMP
	 4 USERS					      /u01/app/oracle/oradata/PROD1/users01.dbf 		    2030818	 2030818
	 6 EXAMPLE					      /u01/app/oracle/oradata/PROD1/example01.dbf		    2030818	 2030818
	 7 TEST 					      /u01/app/oracle/oradata/PROD1/test.tbs			    2029996	 2029996
	 8 DEV_MDS					      /u01/app/oracle/oradata/PROD1/DEV_mds.dbf 		    2030818	 2030818
	 9 DEV_IAS_TEMP
	10 DEV_BIPLATFORM				      /u01/app/oracle/oradata/PROD1/DEV_biplatform.dbf		    2030818	 2030818
	11 DEV1_ODI_TEMP

       TS# NAME 					      NAME						 CHECKPOINT_CHANGE# LAST_CHANGE#
---------- -------------------------------------------------- -------------------------------------------------- ------------------ ------------
	13 DEV2_ODI_TEMP
	14 DEV2_ODI_USER				      /u01/app/oracle/oradata/PROD1/DEV2_odi_user.dbf		    2030818	 2030818

13 rows selected.


数据文件头SCN:

不同于上述的SCN数据文件开始scn记录在每个数据文件中。当发生系统及文件级别的检查点后,不仅将这时的SCN号记录在控制文件中,同样也记录在数据文件中。

查询数据文件头SCN的命令如下

SQL> select tablespace_name,file#,CHECKPOINT_CHANGE# from v$datafile_header;

TABLESPACE_NAME 		    FILE# CHECKPOINT_CHANGE#
------------------------------ ---------- ------------------
SYSTEM					1	     2030818
SYSAUX					2	     2030818
UNDOTBS1				3	     2030818
USERS					4	     2030818
EXAMPLE 				5	     2030818
TEST					6	     2029996
DEV_MDS 				7	     2030818
DEV_BIPLATFORM				8	     2030818
DEV2_ODI_USER				9	     2030818

9 rows selected.


你可能感兴趣的:(oracle)