UNDO扩展学习
UNDO是Oracle在UNDO SEGMENT(回滚段)中记录的信息。下面使用UNDO这名字可能会包含两种意思,一是前面说的回滚段中的记录、二是UNDO整个机制。
在这部分内容里,没有涉及闪回技术,要知道不少闪回技术是基于UNDO机制来实现的,比如flashback query、flashback table、flashback version query等。
首先看下我的操作环境:
sys@L10GR204> select * from v$version;
BANNER
----------------------------------------------------------------------------------------------------
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bi
PL/SQL Release 10.2.0.4.0 - Production
CORE 10.2.0.4.0 Production
TNS for Linux: Version 10.2.0.4.0 - Production
NLSRTL Version 10.2.0.4.0 - Production
下面列出本节要讨论的内容:
1) 什么时候联机回滚段?
2) 如何证明SYSTEM回滚段只对系统对象提供服务
3) alter system set undo_tablespace = uuu;
4) 如何创建一个新的UNDO表空间并使用
5) 回滚段增减
6) 了解几个参数
7) 构造一个CR块
8) 快速构造ora-01555 快照过旧
9) enq: US – contention
10) Oracle INSERT、UPDATE、DELETE与Undo
什么时候联机回滚段?
这是一个非常简单的问题,如果大家已经掌握了数据库的启动过程,那就很容易回答上了。
看下面简单实验:
当前实例为MOOUNT状态
sys@L10GR204> select status from v$instance;
STATUS
------------------------
MOUNTED
这时候试图访问和UNDO相关视图,将会报错
sys@L10GR204> select * from v$rollname;
select * from v$rollname
*
ERROR at line 1:
ORA-01219: database not open: queries allowed on fixed tables/views only
当我们OPEN之后,即可读取回滚段信息
sys@L10GR204> alter database open;
Database altered.
sys@L10GR204> col name for a55
sys@L10GR204> select rn.usn, rn.name, rs.status from v$rollname rn, v$rollstat rs where rn.usn = rs.usn;
USN NAME STATUS
---------- ------------------------------------------------------- ------------------------------
0 SYSTEM ONLINE
1 _SYSSMU1$ ONLINE
2 _SYSSMU2$ ONLINE
3 _SYSSMU3$ ONLINE
4 _SYSSMU4$ ONLINE
5 _SYSSMU5$ ONLINE
6 _SYSSMU6$ ONLINE
7 _SYSSMU7$ ONLINE
8 _SYSSMU8$ ONLINE
9 _SYSSMU9$ ONLINE
10 _SYSSMU10$ ONLINE
11 rows selected.
-- 仔细看,USN为0的回滚段为SYSTEM,这是我们创建数据库的时候创建于系统表空间的回滚段,只提供于更改系统对象做服务。
sys@L10GR204> set line 150
sys@L10GR204> col SEGMENT_NAME for a15
sys@L10GR204> col TABLESPACE_NAME for a10
sys@L10GR204> select segment_name, segment_type, tablespace_name from dba_segments where segment_name = 'SYSTEM';
SEGMENT_NAME SEGMENT_TYPE TABLESPACE
--------------- ------------------------------------ ----------
SYSTEM ROLLBACK SYSTEM
如何证明SYSTEM回滚段只对系统对象提供服务
为了证明这句话的正确性,我先将非SYSTEM的回滚段去除,只要将当前UNDO表空间脱机处理即可。
sys@L10GR204> select file_id, tablespace_name from dba_data_files where tablespace_name = 'UNDOTBS1';
FILE_ID TABLESPACE
---------- ----------
2 UNDOTBS1
UNDO表空间脱机需要注意操作:
在非归档模式如下操作
OPEN模式直接操作:
sys@L10GR204> alter database datafile 2 offline drop;
ERROR:
ORA-03114: not connected to ORACLE
alter database datafile 2 offline drop
*
ERROR at line 1:
ORA-00603: ORACLE server session terminated by fatal error
告警日志频繁输出:
Mon Mar 25 13:35:28 2013
Errors in file /u01/app/oracle/admin/L10GR204/bdump/l10gr204_pmon_3756.trc:
ORA-00376: file 2 cannot be read at this time
ORA-01110: data file 2: '/u01/app/oracle/oradata/L10GR204/undotbs01.dbf'
Mon Mar 25 13:35:38 2013
Errors in file /u01/app/oracle/admin/L10GR204/bdump/l10gr204_pmon_3756.trc:
ORA-00376: file 2 cannot be read at this time
ORA-01110: data file 2: '/u01/app/oracle/oradata/L10GR204/undotbs01.dbf'
Mon Mar 25 13:35:38 2013
Errors in file /u01/app/oracle/admin/L10GR204/bdump/l10gr204_pmon_3756.trc:
ORA-00376: file 2 cannot be read at this time
ORA-01110: data file 2: '/u01/app/oracle/oradata/L10GR204/undotbs01.dbf'
Mon Mar 25 13:35:38 2013
Errors in file /u01/app/oracle/admin/L10GR204/bdump/l10gr204_pmon_3756.trc:
ORA-00376: file 2 cannot be read at this time
ORA-01110: data file 2: '/u01/app/oracle/oradata/L10GR204/undotbs01.dbf'
Mon Mar 25 13:35:38 2013
Errors in file /u01/app/oracle/admin/L10GR204/bdump/l10gr204_pmon_3756.trc:
ORA-00376: file 2 cannot be read at this time
ORA-01110: data file 2: '/u01/app/oracle/oradata/L10GR204/undotbs01.dbf'
试图关闭数据库
sys@L10GR204> shutdown immediate
-- 系统HANG住,无法正常关闭
解决方法:
sys@L10GR204> shutdown abort
sys@L10GR204> startup mount
sys@L10GR204> recover database;
sys@L10GR204> alter database open;
经过上面一系列操作,系统能open了,但是,当我们再次关闭数据库的时候,将得到错误提示
sys@L10GR204> shutdown immediate
ORA-00376: file 2 cannot be read at this time
ORA-01110: data file 2: '/u01/app/oracle/oradata/L10GR204/undotbs01.dbf'
这是因为UNDO表空间当前状态是OFFLINE缘故
sys@L10GR204> select FILE#, status from v$datafile;
FILE# STATUS
---------- --------------
1 SYSTEM
2 OFFLINE
3 ONLINE
4 ONLINE
5 ONLINE
在线处理之后恢复正常
sys@L10GR204> alter database datafile 2 online;
Database altered.
sys@L10GR204> select FILE#, status from v$datafile;
FILE# STATUS
---------- --------------
1 SYSTEM
2 ONLINE
3 ONLINE
4 ONLINE
5 ONLINE
sys@L10GR204> select rn.usn, rn.name, rs.status from v$rollname rn, v$rollstat rs where rn.usn = rs.usn;
USN NAME STATUS
---------- ------------------------------------------------------- ------------------------------
0 SYSTEM ONLINE
11 _SYSSMU11$ ONLINE
在数据库open又开启归档模式下,进行如下操作:
sys@L10GR204> alter database datafile 2 offline drop;
ERROR:
ORA-03114: not connected to ORACLE
alter database datafile 2 offline drop
*
ERROR at line 1:
ORA-00603: ORACLE server session terminated by fatal error
可见依然会挂起,但这时候,我们是无法通过非归档模式下解决方法来恢复,当打开数据库时,直接异常终止,如下:
idle> alter database open;
alter database open
*
ERROR at line 1:
ORA-01092: ORACLE instance terminated. Disconnection forced
告警日志输出:
Errors in file /u01/app/oracle/admin/L10GR204/bdump/l10gr204_smon_22399.trc:
ORA-00604: error occurred at recursive SQL level 1
ORA-00376: file 2 cannot be read at this time
ORA-01110: data file 2: '/u01/app/oracle/oradata/L10GR204/undotbs01.dbf'
Mon Mar 25 13:50:42 2013
Errors in file /u01/app/oracle/admin/L10GR204/bdump/l10gr204_smon_22399.trc:
ORA-00604: error occurred at recursive SQL level 1
ORA-00376: file 2 cannot be read at this time
ORA-01110: data file 2: '/u01/app/oracle/oradata/L10GR204/undotbs01.dbf'
Mon Mar 25 13:50:43 2013
Errors in file /u01/app/oracle/admin/L10GR204/udump/l10gr204_ora_23025.trc:
ORA-00604: error occurred at recursive SQL level 1
ORA-00376: file 2 cannot be read at this time
ORA-01110: data file 2: '/u01/app/oracle/oradata/L10GR204/undotbs01.dbf'
Error 604 happened during db open, shutting down database
USER: terminating instance due to error 604
Instance terminated by USER, pid = 23025
ORA-1092 signalled during: alter database open...
这时候,我们需要手工介恢复:
idle> conn / as sysdba
idle> startup mount
[oracle@khm21 ~]$ rman target /
RMAN> recover database;
idle> alter database open;
上面是在误操作(OPEN状态下)之后恢复的过程,OPEN状态下将默认回滚表空间脱机处理是较危险的操作,请务必重启到MOUNT模式下操作
sys@L10GR204> alter database datafile 2 offline drop;
-- 不带drop选项,会报ORA-01145
sys@L10GR204> alter database datafile 2 offline;
alter database datafile 2 offline
*
ERROR at line 1:
ORA-01145: offline immediate disallowed unless media recovery enabled
在系统运行于归档模式下的时候,则如下操作:
sys@L10GR204> shutdown immediate
sys@L10GR204> startup mount
sys@L10GR204> alter database datafile 2 offline;
sys@L10GR204> alter database open;
下面验证system回滚段只为系统对象做服务,使用sys用户操作:
sys@L10GR204> create table test(id int);
Table created.
sys@L10GR204> insert into test values(1);
1 row created.
sys@L10GR204> delete from test;
1 row deleted.
sys@L10GR204> rollback;
Rollback complete.
当前只有一个SYSTEM回滚段,我们操作对象时SYSTEM表空间里的时候,SYSTEM回滚段就为此服务,因此正常。
sys@L10GR204> select rn.usn, rn.name, rs.status from v$rollname rn, v$rollstat rs where rn.usn = rs.usn;
USN NAME STATUS
---------- ------------------------------ ------------------------------
0 SYSTEM ONLINE
通过普通用户操作:
ning@L10GR204> show user
USER is "ning"
ning@L10GR204> create table test(id int);
create table test(id int)
*
ERROR at line 1:
ORA-01552: cannot use system rollback segment for non-system tablespace 'ning'
-提示system回滚段无法为其他表空间提供服务。
将默认回滚表空间在线处理
sys@L10GR204> alter database datafile 2 online;
Database altered.
有了默认回滚段,操作恢复正常
ning@L10GR204> create table test(id int);
Table created.
ning@L10GR204> insert into test values(5);
1 row created.
ning@L10GR204> rollback;
Rollback complete.
这时候发现产生了一个回滚段
sys@L10GR204> select rn.usn, rn.name, rs.status from v$rollname rn, v$rollstat rs where rn.usn = rs.usn;
USN NAME STATUS
---------- ------------------------------ ------------------------------
0 SYSTEM ONLINE
1 _SYSSMU1$ ONLINE
通过如下方法可以查看_SYSSMU1$回滚段的使用情况:
sys@L10GR204> select OWNER, SEGMENT_NAME, TABLESPACE_NAME, EXTENT_ID, FILE_ID, BLOCK_ID, STATUS from dba_undo_extents where segment_name='_SYSSMU1$' and rownum <=3;
OWNER SEGMENT_NA TABLESPACE EXTENT_ID FILE_ID BLOCK_ID STATUS
------ ---------- ---------- ---------- ---------- ---------- ------------------
SYS _SYSSMU1$ UNDOTBS1 0 2 9 EXPIRED
SYS _SYSSMU1$ UNDOTBS1 1 2 2569 EXPIRED
SYS _SYSSMU1$ UNDOTBS1 2 2 24217 EXPIRED
alter system set undo_tablespace = uuu;
为什么要研究修改undo表空间,只是想告诉大家,当我们执行alter system set undo_tablespace = uuu;的时候都会发生什么。
首先我们观察当前都有哪些回滚段:
sys@L10GR204> select rn.usn, rn.name, rs.status from v$rollname rn, v$rollstat rs where rn.usn = rs.usn;
USN NAME STATUS
---------- ------------------------------ ------------------------------
0 SYSTEM ONLINE
1 _SYSSMU1$ ONLINE
2 _SYSSMU2$ ONLINE
3 _SYSSMU3$ ONLINE
4 _SYSSMU4$ ONLINE
5 _SYSSMU5$ ONLINE
6 _SYSSMU6$ ONLINE
7 _SYSSMU7$ ONLINE
8 _SYSSMU8$ ONLINE
9 _SYSSMU9$ ONLINE
10 _SYSSMU10$ ONLINE
11 rows selected.
创建一个新的undo表空间,注意,这里为下面的实验,我故意创建了非常小的回滚表空间。
sys@L10GR204> create undo tablespace undo_ning datafile '/u01/app/oracle/oradata/L10GR204/undo_ning.dbf' size 150K autoextend off;
Tablespace created.
将当前默认undo tablespace设置成新建的
sys@L10GR204> alter system set undo_tablespace=undo_ning;
System altered.
发现原先的回滚段全部消失,表空间过小,一个回滚段都没创建。
sys@L10GR204> select rn.usn, rn.name, rs.status from v$rollname rn, v$rollstat rs where rn.usn = rs.usn;
USN NAME STATUS
---------- ------------------------------ ------------------------------
0 SYSTEM ONLINE
添加数据文件
sys@L10GR204> alter tablespace undo_ning add datafile '/u01/app/oracle/oradata/L10GR204/undo_ning02.dbf' size 200k autoextend on;
Tablespace altered.
生成一个回滚段。
sys@L10GR204> select rn.usn, rn.name, rs.status from v$rollname rn, v$rollstat rs where rn.usn = rs.usn;
USN NAME STATUS
---------- ------------------------------ ------------------------------
0 SYSTEM ONLINE
12 _SYSSMU12$ ONLINE
结论:在我们在线修改undo tablespace的时候,并不是原先的回滚段移动到新的undo tablespace,而是脱机重新创建。但如果原undo tablespace里还有active事务,则需要等到这些活动事务全部结束之后才能切换。
如何创建一个新的UNDO表空间并使用
上面已经演示过创建新UNDO表空间并使用的过程,这里分享一下如果创建过程遇到ORA-30015如何去解决。
sys@L10GR204> create undo tablespace l_u_s datafile '/u01/app/oracle/oradata/L10GR204/l_u_s01.dbf' size 1M autoextend off;
Tablespace created.
当我想使用新创建的undo表空间的时候,得到错误提示
sys@L10GR204> alter system set undo_tablespace=l_u_s;
alter system set undo_tablespace=l_u_s
*
ERROR at line 1:
ORA-02097: parameter cannot be modified because specified value is invalid
ORA-30015: previously offlined undo tablespace 'UNDO_ning' is still pending
查一下当前回滚段状态,发现_SYSSMU19$状态为PENDING OFFLINE,估计还有个活跃事务。
sys@L10GR204> col NAME for a20
sys@L10GR204> select rn.usn, rn.name, rs.status from v$rollname rn, v$rollstat rs where rn.usn = rs.usn;
USN NAME STATUS
---------- -------------------- ------------------------------
0 SYSTEM ONLINE
19 _SYSSMU19$ PENDING OFFLINE
34 _SYSSMU34$ ONLINE
35 _SYSSMU35$ ONLINE
36 _SYSSMU36$ ONLINE
37 _SYSSMU37$ ONLINE
38 _SYSSMU38$ ONLINE
39 _SYSSMU39$ ONLINE
40 _SYSSMU40$ ONLINE
9 rows selected.
我们通过下面脚本区找当前活跃事务SESSION信息
sys@L10GR204> col USERNAME for a10
sys@L10GR204> col ROLLBACK for a15
sys@L10GR204> SELECT S.SID, S.SERIAL#, S.USERNAME, R.NAME "ROLLBACK"
2 FROM V$SESSION S, V$TRANSACTION T, V$ROLLNAME R
3 WHERE S.TADDR = T.ADDR AND T.XIDUSN = R.USN
4 AND R.name IN (SELECT segment_name FROM dba_rollback_segs WHERE TABLESPACE_NAME ='UNDO_ning')
5 /
SID SERIAL# USERNAME ROLLBACK
---------- ---------- ---------- ---------------
148 34 ning _SYSSMU19$
直接将该SESSION KILL掉
sys@L10GR204> alter system kill session '148,34';
System altered.
这下就可以正常设置新UNDO表空间了
sys@L10GR204> alter system set undo_tablespace='L_U_S';
System altered.
sys@L10GR204> alter system set undo_retention=1;
System altered.
回滚段增减
在上面,我们已经看出,当Oracle数据库启动的时候,默认就会创建多个回滚段,但是,某些系统这些回滚段是不够用的,表现在事务量非常大的系统中。
不过不用担心,Oracle也知道有这种现象,因此如果当前回滚段不足,则会自动创建新回滚段并提供使用。一段时间(通过指定算法)后如果事务量减少,Oracle的SMON将部分回滚段脱机。
我们在上面环境的基础下,通过实验观察这个效果。
这里用到了如下脚本
[oracle@khm21 ~]$ cat showsysevent.sql
column EVENT for a65
select *
from (
select event, total_waits, time_waited
from v$system_event
where wait_class <> 'Idle'
order by 3 desc
)
where EVENT = 'enq: US - contention'
/
查看当前回滚段情况:
sys@L10GR204> select rn.usn, rn.name, rs.status from v$rollname rn, v$rollstat rs where rn.usn = rs.usn;
USN NAME STATUS
---------- ------------------------------ ------------------------------
0 SYSTEM ONLINE
12 _SYSSMU12$ ONLINE
13 _SYSSMU13$ ONLINE
创建一张测试对象表并插入数据
ning@L10GR204> create table khm(id number, domain varchar2(20));
Table created.
ning@L10GR204> begin
2 for i in 1 .. 100 loop
3 insert into khm values(i,'Integrated Cloud Applications and Platform Services');
4
5 end loop;
6 commit;
7 end;
8 /
PL/SQL procedure successfully completed.
创建一个过程,对khm表进行delete,然后马上回滚:
create or replace procedure khm_delete
is
begin
for i in 1 .. 100 loop
delete from khm;
rollback;
end loop;
end;
/
打开100个会话并执行该存储过程
var jobno number;
begin
for l in 1 .. 100 loop
dbms_job.submit(:jobno, 'khm_delete;');
commit;
end loop;
end;
/
发现回滚段自动增加了几个
sys@L10GR204> select rn.usn, rn.name, rs.status from v$rollname rn, v$rollstat rs where rn.usn = rs.usn;
USN NAME STATUS
---------- ------------------------------ ------------------------------
0 SYSTEM ONLINE
12 _SYSSMU12$ ONLINE
13 _SYSSMU13$ ONLINE
14 _SYSSMU14$ ONLINE
15 _SYSSMU15$ ONLINE
16 _SYSSMU16$ ONLINE
6 rows selected.
下面是US锁等待统计,具体下面讨论
ning@L10GR204> @showsysevent
EVENT TOTAL_WAITS TIME_WAITED
----------------------------------------------------------------- ----------- -----------
enq: US - contention 26 339
了解几个参数
我不会去解释所有和UNDO相关的参数,UNDO_MANAGEMENT、UNDO_RETENTION、UNDO_TABLESPACE这三个参数我也不解释。
使用下面脚本查看和回滚相关的一些参数,了解一下即可,除非特殊情况不建议修改(尤其是隐含参数)
sys@L10GR204> set pagesize 9999
sys@L10GR204> set line 150
sys@L10GR204> col NAME for a30
sys@L10GR204> col VALUE for a20
sys@L10GR204> col DESCRIB for a80
sys@L10GR204> SELECT x.ksppinm NAME, y.ksppstvl VALUE, x.ksppdesc DESCRIB
2 FROM SYS.x$ksppi x, SYS.x$ksppcv y
3 WHERE x.inst_id = USERENV ('Instance')
4 AND y.inst_id = USERENV ('Instance')
5 AND x.indx = y.indx
6 AND x.ksppinm LIKE '%&par%'
7
sys@L10GR204> /
Enter value for par: undo
old 6: AND x.ksppinm LIKE '%&par%'
new 6: AND x.ksppinm LIKE '%undo%'
NAME VALUE DESCRIB
------------------------------ -------------------- --------------------------------------------------------------------------------
_gc_undo_affinity TRUE if TRUE, enable dynamic undo affinity
_kcl_undo_locks 128 number of locks per undo segment
_kcl_undo_grouping 32 grouping for undo block locks
_gc_undo_affinity_locks TRUE if TRUE, get affinity locks for undo
_gc_dissolve_undo_affinity FALSE if TRUE, dissolve undo affinity after an offline
_gc_initiate_undo_affinity TRUE if TRUE, initiate undo affinity after an online
undo_management AUTO instance runs in SMU mode if TRUE, else in RBU mode
undo_tablespace UNDO_ning use/switch undo tablespace
_collect_undo_stats TRUE Collect Statistics v$undostat
_undo_debug_mode 0 debug flag for undo related operations
_verify_undo_quota FALSE TRUE - verify consistency of undo quota statistics
_in_memory_undo TRUE Make in memory undo for top level transactions
_undo_autotune TRUE enable auto tuning of undo_retention
undo_retention 900 undo retention in seconds
_smon_undo_seg_rescan_limit 10 limit of SMON continous undo segments re-scan
_undo_debug_usage 0 invoke undo usage functions for testing
_optimizer_undo_cost_change 10.2.0.4 optimizer undo cost change
_optimizer_undo_changes FALSE undo changes to query optimizer
18 rows selected.
sys@L10GR204> SELECT x.ksppinm NAME, y.ksppstvl VALUE, x.ksppdesc DESCRIB
2 FROM SYS.x$ksppi x, SYS.x$ksppcv y
3 WHERE x.inst_id = USERENV ('Instance')
4 AND y.inst_id = USERENV ('Instance')
5 AND x.indx = y.indx
6 AND x.ksppinm LIKE '%&par%'
7
sys@L10GR204> /
Enter value for par: rollback
old 6: AND x.ksppinm LIKE '%&par%'
new 6: AND x.ksppinm LIKE '%rollback%'
NAME VALUE DESCRIB
------------------------------ -------------------- --------------------------------------------------------------------------------
transactions_per_rollback_segm 5 number of active transactions per rollback segment
ent
rollback_segments undo segment list
_rollback_segment_initial 1 starting undo segment number
_rollback_segment_count 0 number of undo segments
_offline_rollback_segments offline undo segment list
_corrupted_rollback_segments corrupted undo segment list
_cleanup_rollback_entries 100 no. of undo entries to apply per transaction cleanup
_rollback_stopat 0 stop at -position to step rollback
fast_start_parallel_rollback LOW max number of parallel recovery slaves that may be used
9 rows selected.
从中介绍几个隐含参数:
_undo_autotune :从Oracle 10g R2开始默认为TRUE,意思是根据undo表空间大小或者系统负载情况,自动调整undo_retention。这个功能可以在线关闭,方法:alter system set "_undo_autotune"=false;
_smon_undo_seg_rescan_limit:limit of SMON continous undo segments re-scan,上面介绍了Oracle脱机回滚段是根据算法的,算法里面的其中一个条件(如果前面条件都不满足则采用此条件)就是参考_smon_undo_seg_rescan_limit的值。
_rollback_segment_count:指定在线状态的回滚段个数。
构造一个CR块
下面是简单构造一个CR块的示例。
SESSION A:
ning@L10GR204> select distinct sid from v$mystat;
SID
----------
149
当前KHM表id=95的记录如下:
ning@L10GR204> select * from khm where id = 95;
ID DOMAIN
---------- ----------------------------------------
95 Integrated Cloud Applications and Platform Services
打开一个游标,但不执行
ning@L10GR204> variable l refcursor
ning@L10GR204> exec open :l for select * from khm where id = 95;
PL/SQL procedure successfully completed.
SESSION B:
ning@L10GR204> select distinct sid from v$mystat;
SID
----------
143
在另一个会话将id=95的记录值修改并提交
ning@L10GR204> update khm set domain = 'Create your own social network with the best community website builder - NING' where id = 95;
1 row updated.
ning@L10GR204> commit;
Commit complete.
回到打开游标的会话,执行并读取,这时我们发现读取到的是被SESSION B修改之前的记录,这就从CR块里读出来的记录
SESSION A:
ning@L10GR204> print l
ID DOMAIN
---------- ----------------------------------------
95 Integrated Cloud Applications and Platform Services
再次查询,因为事务已经提交,所以读取到的是UPDATE之后的内容
ning@L10GR204> select * from khm where id = 95;
ID DOMAIN
---------- ----------------------------------------
95 Create your own social network with the best community website builder - NING
这其中SCN的作用是非常重要的,其实SESSION A打开游标的时候就有对应的SCN,这个SCN肯定小于更新并提交时SCN,当我们print的时候还是拿当前SCN去查询,经过跟块上的SCN比较之后它去回滚段读取镜像数据。
下面我们深入了解一下构造CR块并读取过程。
拿下面数据作为参考进行实验:
ning@L10GR204> select rowid, t.* from khm t where t.id >95;
ROWID ID DOMAIN
------------------ ---------- ----------------------------------------
AAANIuAAFAAAAAWABf 96 Create your own social network with the best community website builder - NING
AAANIuAAFAAAAAWABg 97 Create your own social network with the best community website builder - NING
AAANIuAAFAAAAAWABh 98 Create your own social network with the best community website builder - NING
AAANIuAAFAAAAAWABi 99 Integrated Cloud Applications and Platform Services
AAANIuAAFAAAAAWABj 100 Integrated Cloud Applications and Platform Services
SESSION A更新两条记录
ning@L10GR204> update khm set domain='bbs.ning.com' where id=96;
1 row updated.
ning@L10GR204> update khm set domain='ffdg' where id=97;
1 row updated.
查看该事务存在信息
ning@L10GR204> @showtra
Enter value for sid: 149
old 13: WHERE SID=&SID
new 13: WHERE SID=149
XIDUSN XIDSLOT XIDSQN UBAFIL UBABLK UBASQN UBAREC STATUS
---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------------
50 12 3 13 44 3 45 ACTIVE
通过USN号去查看当前使用的回滚段
sys@L10GR204> select USN, NAME from v$rollname where USN=50;
USN NAME
---------- -------------------------
50 _SYSSMU50$
如果想dump该回滚段头信息,则如下方式操作:
sys@L10GR204> alter system dump undo header '_SYSSMU50$';
sys@L10GR204> oradebug setmypid
sys@L10GR204> oradebug tracefile_name
/u01/app/oracle/admin/L10GR204/udump/l10gr204_ora_4085.trc
如果想dump该回滚段内容看,则如下方式操作:
sys@L10GR204> alter system dump datafile 13 block 44;
sys@L10GR204> oradebug setmypid
sys@L10GR204> oradebug tracefile_name
/u01/app/oracle/admin/L10GR204/udump/l10gr204_ora_4277.trc
至于DUMP出来的内容如何去看,我将在下面分析,这里不做解释。
下面是简单演示如何通过v$bh查看cr状态xcur状态:
SESSION A:
当前是free状态
sys@L10GR204> select file#, block#, status from v$bh where file#=5 and block#=23;
FILE# BLOCK# STATUS
---------- ---------- --------------
5 23 free
插入一条记录并查看
ning@L10GR204> insert into khm values(1,'恩墨学院 - 名师传道,授业以专');
1 row created.
ning@L10GR204> @showrowid
Enter value for table_name: khm
old 7: from &table_name
new 7: from khm
Enter value for id: 1
old 8: where id=&id
new 8: where id=1
ROWID OBJECT_ID FILE_ID BLOCK_ID NUM ROWIDTOCHAR(ROWID)
------------------ ---------- ---------- ---------- ---------- ------------------------------------
AAANIuAAFAAAAAXAAA 53806 5 23 0 AAANIuAAFAAAAAXAAA
sys@L10GR204> select file#, block#, status from v$bh where file#=5 and block#=23;
FILE# BLOCK# STATUS
---------- ---------- --------------
5 23 free
5 23 xcur
5 23 cr
-- INSERT一条记录产生了一个XCUR块儿以及CR块儿。
SESSION B:
查询该表:
ning@L10GR204> select * from khm;
ID DOMAIN
---------- ----------------------------------------
91 Create your own social network with the best community website builder - NING
92 Create your own social network with the best community website builder - NING
93 Create your own social network with the best community website builder - NING
94 Create your own social network with the best community website builder - NING
95 Create your own social network with the best community website builder - NING
96 Create your own social network with the best community website builder - NING
97 Create your own social network with the best community website builder - NING
98 Create your own social network with the best community website builder - NING
99 Integrated Cloud Applications and Platform Services
100 Integrated Cloud Applications and Platform Services
10 rows selected.
结果又产生了一个CR块儿
sys@L10GR204> select file#, block#, status from v$bh where file#=5 and block#=23;
FILE# BLOCK# STATUS
---------- ---------- --------------
5 23 free
5 23 xcur
5 23 cr
5 23 cr
SESSION C:
ning@L10GR204> select * from khm;
ID DOMAIN
---------- ----------------------------------------
91 Create your own social network with the best community website builder - NING
92 Create your own social network with the best community website builder - NING
93 Create your own social network with the best community website builder - NING
94 Create your own social network with the best community website builder - NING
95 Create your own social network with the best community website builder - NING
96 Create your own social network with the best community website builder - NING
97 Create your own social network with the best community website builder - NING
98 Create your own social network with the best community website builder - NING
99 Integrated Cloud Applications and Platform Services
100 Integrated Cloud Applications and Platform Services
10 rows selected.
sys@L10GR204> select file#, block#, status from v$bh where file#=5 and block#=23;
FILE# BLOCK# STATUS
---------- ---------- --------------
5 23 free
5 23 cr
5 23 cr
5 23 xcur
5 23 cr
SESSION D、SESSION E、SESSION F...也同样操作,结果是CR块儿最多构造5个,超过5个则覆盖之前的CR块儿。
sys@L10GR204> select file#, block#, status from v$bh where file#=5 and block#=23;
FILE# BLOCK# STATUS
---------- ---------- --------------
5 23 free
5 23 cr
5 23 cr
5 23 cr
5 23 cr
5 23 xcur
5 23 cr
7 rows selected.
CR最多创建多少个受隐含参数_db_block_max_cr_dba值的影响,默认为6。
sys@L10GR204> @GetHparDes
Enter value for par: block_max_cr
old 6: AND x.ksppinm LIKE '%&par%'
new 6: AND x.ksppinm LIKE '%block_max_cr%'
NAME VALUE DESCRIB
------------------------------ -------------------- --------------------------------------------------------------------------------
_db_block_max_cr_dba 6 Maximum Allowed Number of CR buffers per dba
快速构造ora-01555 快照过旧
ORA-01555是一个ORACLE非常经典的错误之一,研究UNDO的时候不能排除的内容。
ning@L10GR204> host oerr ora 1555
01555, 00000, "snapshot too old: rollback segment number %s with name "%s" too small"
// *Cause: rollback records needed by a reader for consistent read are
// overwritten by other writers
// *Action: If in Automatic Undo Management mode, increase undo_retention
// setting. Otherwise, use larger rollback segments
查看当前UNDO相关参数设置以及undo表空间大小
sys@L10GR204> show parameter undo
NAME TYPE VALUE
------------------------------------ ---------------------- ------------------------------
undo_management string AUTO
undo_retention integer 2
undo_tablespace string L_S_U
sys@L10GR204> select bytes/1048576, AUTOEXTENSIBLE from dba_data_files where tablespace_name='L_S_U';
BYTES/1048576 AUTOEX
------------- ------
1 NO
创建一个过程:
create or replace procedure ltb_update
is
cursor c1 is select object_id from ltb where rownum <= 100;
begin
for r_c1 in c1 loop
update ltb set owner='ning' where object_id=r_c1.object_id;
commit;
end loop;
end;
/
打开30个会话并执行该存储过程
var jobno number;
begin
for l in 1 .. 20 loop
dbms_job.submit(:jobno, 'ltb_update;');
commit;
end loop;
end;
/
告警日志输出大量的ORA-015555:
Tue Mar 26 21:59:21 2013
ORA-01555 caused by SQL statement below (SQL ID: a9s2m307s4dv1, Query Duration=1 sec, SCN: 0x0000.0046a23b):
Tue Mar 26 21:59:21 2013
UPDATE LTB SET OWNER='ning' WHERE OBJECT_ID=:B1
Tue Mar 26 21:59:21 2013
ORA-01555 caused by SQL statement below (SQL ID: a9s2m307s4dv1, Query Duration=1 sec, SCN: 0x0000.0046a1df):
Tue Mar 26 21:59:21 2013
UPDATE LTB SET OWNER='ning' WHERE OBJECT_ID=:B1
Tue Mar 26 21:59:21 2013
Errors in file /u01/app/oracle/admin/L10GR204/bdump/l10gr204_j020_5371.trc:
ORA-12012: error on auto execute of job 2153
ORA-01555: snapshot too old: rollback segment number 51 with name "_SYSSMU51$" too small
ORA-06512: at "ning.LTB_UPDATE", line 6
ORA-06512: at line 1
Tue Mar 26 21:59:21 2013
Errors in file /u01/app/oracle/admin/L10GR204/bdump/l10gr204_j012_5354.trc:
ORA-12012: error on auto execute of job 2126
ORA-01555: snapshot too old: rollback segment number 54 with name "_SYSSMU54$" too small
ORA-06512: at "ning.LTB_UPDATE", line 6
ORA-06512: at line 1
最后讨论下ORA-01555的解决方案:
1) 不要让事务很大;
2) 允许undo表空间自动扩展,undo_retention根据需求设大些;
3) 减少不必要的commit;
4) 优化SQL,减少查询时间。
enq: US – contention
上面经过测试,在事务数增加导致回滚段不足时,Oracle会分配更多回滚段,这时候服务器进程或者后台进程需要获得US锁,每个回滚段都分配一个US锁,如果这个获取过程中发生争用,就会产生enq: US – contention等待事件。一般情况下,US锁争用现象很少发生,但如果发生了,需要具体分析,从应用角度分析,或者查看BUG信息。
在上面我提到过,Oracle的SMON进程将部分回滚段脱机处理,肯定有朋友不清楚SMON居然还干这事。这是Oracle为了减少撤销空间使用的压力而设计的。
但是SMON并不是经常做这件事情,这里有个算法,SMON基于过去7天v$undostat动态视图信息或者AWR中UNDO历史快照使用信息来决定脱机回滚段数量,而且不删除多余的回滚段,而是OFFLINE处理。
如果发现脱机回滚段严重影响性能,DBA也可以禁止此功能,方法:10511事件。
sys@L10GR204> host oerr ora 10511
10511, 00000, "turn off SMON check to cleanup undo dictionary"
// *Cause:
// *Action:
sys@L10GR204> alter system set events '10511 trace name context forever, level 1';
System altered.
SMON还为UNDO段做一件事情,就是定期收缩(Shrink)。当然只有SMON才定期做这件事情,不一定SMON不工作回滚段就不收缩,比如回滚段之间空间紧张时也会发生。
SMON每12个小时进行一次收缩,这个功能我们也可以使用events事件禁用,但一般不建议这么做。
sys@L10GR204> host oerr ora 10512
10512, 00000, "turn off SMON check to shrink rollback segments"
// *Cause:
// *Action:
sys@L10GR204> alter system set events '10512 trace name context forever,level 1';
System altered.
Permalink
Leave a Comment
姓名 (必填)
电子邮件 required)
URI
所见即所得编辑器
工具栏 源码 插入代码
▲
元素路径
NOTICE: You should type some Chinese word (like “你好”) in your comment to pass the spam-check, thanks for your patience!
来自 <http://www.ning.com/archives/755.html>