oracle segment undo_71_UNDO扩展学习

b74fb484687b5462a6e339e171025f4d.png

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>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/568099.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

js判断对象是否为空对象_js对象

七种数据类型 number string bool symbol undefined null object五个Falsy 值 undefined null 0 NaN 对象 object第七种数据类型,唯一一种复杂类型定义无序的数据租户键值对的集合写法let obj {name:frank,age:18}let obj new Object({name:frank})console.log({name:frank,a…

Python字典方法

字典也有方法&#xff0c;很有用&#xff0c;但其使用频率可能没有列表和字符串方法那样高。1、clear 删除所有的字典项d {key: value} d.clear() print(d){}2、copy 方法copy返回一个新字典&#xff0c;其包含的键值对与原来的字典相同&#xff08;这各方法是浅复制&#xff…

交流充电桩电路图_直流充电桩和交流充电桩给电动汽车充电过程中是如何工作的?...

近几年来&#xff0c;新能源汽车发展越来越快&#xff0c;而限制新能源电动汽车发展的主要因素是续航里程和充电问题。续航里程要靠提高电池性能来解决&#xff0c;而解决充电问题就要靠充电桩的普及来实现。下面小编带着大家一起来了解一下直流充电桩和交流充电桩给电动汽车充…

Windows上安装Ubuntu子系统练习linux基本命令

经常在我的群里看到自学测试的小伙伴花费了大量的时间在环境搭建和各种软件的安装上面&#xff0c;有很多就卡在第一步&#xff0c;虚拟机的安装。 有很多安装之后比如启动蓝屏之类的等等&#xff0c;其实&#xff0c;我想说的是&#xff0c;这些都是在走弯路&#xff0c;在这个…

如何巧妙的申请换部门_如何设置户外广告?市城管局局长体验户外广告审批流程...

为巩固拓展“不忘初心、牢记使命”主题教育成果&#xff0c;落实“对标找差、再攀新高”工作要求&#xff0c;优化营商环境&#xff0c;提升服务品质&#xff0c;近日&#xff0c;张家港市城管局局长殷沪飞以个体工商户的身份&#xff0c;到行政审批局城管窗口体验户外广告设置…

安卓APP版本发布流程(一)

一、加固安卓包&#xff08;新版安卓Release包&#xff09;1、下载安装加固软件&#xff0c;注册登录账号https://jiagu.360.cn/#/global/index2、添加签名设置&#xff0c;对应签名路径、密码、别名、别名密码向安卓开发要3、添加签名后&#xff0c;APK加固-添加应用&#xff…

小程序弹出层禁止列表滑动_是时候展现真正的技术了!小程序教程来了——百战Web前端课程更新05.07...

百战程序员十大精品课程&#xff0c;实时更新&#xff0c;保持行业领先。本次更新课程Web前端第二十九阶段安心食疗-微信小程序全部7个章节及课程资料。小程序是依托微信而生的&#xff0c;是一种不用下载就能使用的应用&#xff0c;也是一项创新&#xff0c;经过近几年的发展&…

Jmeter实战(二)|同用户重复并发多次抢红包

之前我们有写过Jmeter多人并发抢1个红包&#xff0c;今天我们来写另外一种场景&#xff0c;同用户重复并发多次抢红包&#xff0c;两种场景还是不太一样&#xff0c;都是比较切合用户实际使用场景&#xff0c;如果1个用户在某些条件下&#xff0c;能重复领取到2次红包&#xff…

【北京上海】七猫内推,高薪海量岗位等你来~

微信长按识别、扫码&#xff0c;查看海量待招岗位&#xff01;&#xff01;&#xff01;UI、产品、测试、开发、运营、数据分析等等

Fiddler无所不能——之测试开发攻城狮必备神器

fiddler官网 https://www.progress.com/ 官网下载fiddler https://www.telerik.com/download/fiddler/fiddler4 fiddler插件下载 https://www.telerik.com/fiddler/add-ons Fiddler无所不能——之测试开发攻城狮必备神器 1、模拟真实网络环境4g网、3g网络、2g网络、弱网等​…

TensorFlow7-多元线性回归模型

运行结果&#xff1a;错误 # 不确定多少行为None。列为12列。即12个数据特征 xtf.placeholder(tf.float32,[None,12],name"X") # 12个特征数据ytf.placeholder(tf.float32,[None,1],name"Y") # 1个标签数据### 创建变量 定义模型结构# 定义了一个命令空…

史上最全jmeter逻辑控制器案例详解

在jmeter中逻辑控制器可以控制采样器(samplers)的执行顺序。由此可知&#xff0c;控制器需要和采样器一起使用&#xff0c;否则控制器就没有什么意义了。放在控制器下面的所有的采样器都会当做一个整体&#xff0c;执行时也会一起被执行。 &#xff08;1&#xff09;控制测试计…