Oracle中用Events事件诊断与解决数据库问题

网络整理 - 07-27

  Events事件是Oracle的重要诊断工具及问题解决办法,很多时候需要通过Events设置来屏蔽或者更改Oracle的行为;

  有四种类型的Events:

  Immediate dumps

  Conditional dumps

  Trace dumps

  Events that change database behaviour

  每一个事件都有一个号跟Oracle的错误信息是一样的.如10046和ORA-10046

  每一个事件都有一个Level,可以是以下:

  范围1到10

  位标 0x01 0x02 0x04 0x08 0x10

  标识 0=off,1=on

  ID号 对象ID(object id),内存地址(memory address)

  要注意的是,Events在每一个版本之间都有所改变.有一些存在的事件可能存在争议性或者已经不可用了,往往这些事件号会由新的事件所替代掉.也要注意在当前的版本中message file不一定可以反映出Events.

  很多Events都会影响数据库的行为,一些测试Events极有可能导致数据库DOWN掉.所以,在没有Oracle Support的前提下,最好不要在PRO系统上做Events操作.DEV系统上如果要做Events最好先做个数据库的全备份.

  Enabling Events(Enable事件)

  Events可以在Instance一级Enabled,主要是在INIT.ORA文件中做操作:

  event='event trace name context forever, level level';

  (红色部分:event指事件号.level指定事件的级别)

  一次可以Enable多个事件,可以用以下两种方式:

  1. 用一个冒号隔开

  event = "10248 trace name context forever, level 10:10249 trace name context forever, level 10"

  2. 两个Events分开写

  event="10248 trace name context forever, level 10"

  event="10249 trace name context forever, level 10"#一些版本的Oracle,event要一样的大小写

  Events也可以在Instance一级用ALTER SYSTEM命令来Enable:

  ALTER SYSTEM SET EVENTS 'event trace name context forever, level level';

  在Instance一级用以下Disable

  ALTER SYSTEM SET EVENTS 'event trace name context off';

  Events也可以在Session一级用ALTER SESSION命令来Enable:

  ALTER SESSION SET EVENTS 'event trace name context forever, level level';

  在Session一级用以下命令Disable:

  ALTER SESSION SET EVENTS 'event trace name context off';

  Events在其他的Session用ORADEBUG来Enable:

  在一个Process中实现Enable:

  ORADEBUG EVENT event TRACE NAME CONTEXT FOREVER, LEVEL level

  在某个进程中Enable:

  ORADEBUG SETORAPID 8(PID进程号)

  ORADEBUG EVENT event TRACE NAME CONTEXT FOREVER, LEVEL level

  以下命令Disable:

  ORADEBUG EVENT event TRACE NAME CONTEXT OFF

  在Session中实现Enable:

  ORADEBUG SESSION_EVENT event TRACE NAME CONTEXT FOREVER, LEVEL level

  在Session中实现Disable:

  ORADEBUG SESSION_EVENT event TRACE NAME CONTEXT OFF

  Events也可以用DBMS_SYSTEM.SETEV包来实现Enable和Disable

  (在做之前要先从V$session视图中获得SID和Serial#)

  用以下方式:EXECUTE DBMS_SYSTEM.SET_EV(SID,Serial#,event,level, '')

  如EXECUTE dbms_system.set_ev (9,29,10046,8,'');

  要Disable则将level改为0,如: EXECUTE dbms_system.set_ev (9,29,10046,0,'');

  Listing All Events:(列出所有可用的Events)

  大部分的Events number的范围都在10000到10999.可以用以下命令Dump出所有的信息

  SET SERVEROUTPUT ON

  DECLARE

  err_msg VARCHAR2(120);

  BEGIN

  dbms_output.enable (1000000);

  FOR err_num IN 10000..10999

  LOOP

  err_msg := SQLERRM (-err_num);

  IF err_msg NOT LIKE '%Message '||err_num||' not found%' THEN

  dbms_output.put_line (err_msg);

  END IF;

  END LOOP;

  END;

  /

  在UNIX系统下message文件在底下目录$ORACLE_HOME/rdbms/mesg/oraus.msg

  在NT系统下message文件在底下目录$ORACLE_HOME/rdbms/mesg/oraus.msg

  Listing Enabled Events(列出Enabled Events)

  用以下命令列出在当前Session已经Enabled Events:

  SET linesize 120

  SET feedback OFF

  SET SERVEROUTPUT ON

  DECLARE err_msg VARCHAR2(120);

  BEGIN

  dbms_output.enable (1000000);

  FOR err_num IN 10000..10999 LOOP

  err_msg := SQLERRM (-err_num);

  IF err_msg NOT LIKE '%Message '||err_num||' not found%' THEN

  dbms_output.put_line (err_msg);

  END IF;

  END LOOP;

  END;

  /

  常用Events的参考:(红色的为最常用的而且对DBA比较有用的Events)

  Event 10013 - Monitor Transaction Recovery------在Startup时跟踪事务恢复

  ALTER SESSION SET EVENTS '10013 trace name context forever, level 1';

  Event 10015 - Dump Undo Segment Headers----在事务恢复后做Dump回退段头信息

  ALTER SESSION SET EVENTS '10015 trace name context forever, level 1';

  Event 10032 - Dump Sort Statistics----Dump排序的统计信息,level 10是最详细的

  ALTER SESSION SET EVENTS '10032 trace name context forever, level 10';

  Event 10033 - Dump Sort Intermediate Run Statistics—level 10(不明白)

  ALTER SESSION SET EVENTS '10033 trace name context forever, level 10';

  Event 10045 - Trace Free List Management Operations—跟踪Freelist

  ALTER SESSION SET EVENTS '10045 trace name context forever, level 1';

  Event 10046 - Enable SQL Statement Trace---跟踪SQL,有执行计划,邦定变量和等待的统计信息,level 12最详细

  ALTER SESSION SET EVENTS '10046 trace name context forever, level 12';

  级别level参考如下图:

  Level Action

  1 Print SQL statements, execution plans and execution statistics

  4 As level 1 plus bind variables

  8 As level 1 plus wait statistics

  12 As level 1 plus bind variables and wait statistics

  Event 10053 - Dump Optimizer Decisions---在分析SQL语句时,Dump出优化器所做的选择,级别level 1最详细

  ALTER SESSION SET EVENTS '10053 trace name context forever, level 1';

  参考level:

  Level Action

  1 Print statistics and computations

  2 Print computations only

  Event 10060 - Dump Predicates---(参考)

  Event 10065 - Restrict Library Cache Dump Output for State Object Dumps-(参考)

  Event 10079 - Dump SQL*Net Statistics---Dump SQL*NeT的统计信息

  ALTER SESSION SET EVENTS '10079 trace name context forever, level 2';

  Event 10081 - Trace High Water Mark Changes—跟踪HWM的改变

  ALTER SESSION SET EVENTS '10081 trace name context forever, level 1';

  Event 10104 - Dump Hash Join Statistics—Dump HASH JOIN的统计信息,用level 10

  ALTER SESSION SET EVENTS '10104 trace name context forever, level 10';

  Event 10128 - Dump Partition Pruning Information—Dump分区表信息

  ALTER SESSION SET EVENTS '10128 trace name context forever, level level';

  Level参考

  Level Action

  0x0001 Dump pruning descriptor for each partitioned object

  0x0002 Dump partition iterators

  0x0004 Dump optimizer decisions about partition-wise joins

  0x0008 Dump ROWID range scan pruning information