你好,游客 登录 注册 搜索
背景:
阅读新闻

启用 Oracle 10046 调试事件

[日期:2012-02-27] 来源:Linux社区  作者:robinson_0612 [字体:大 中 小]

四、演示10046调试事件 

  1. 1.创建演示环境                                                                                                                
  2.     CREATE TABLE sql_trace_test                                                                                                 
  3.     (                                                                                                                           
  4.        id            NUMBER                                                                                                     
  5.      , description   VARCHAR2( 50 )                                                                                             
  6.     );                                                                                                                          
  7.                                                                                                                                 
  8.     EXEC DBMS_STATS.gather_table_stats('SCOTT', 'SQL_TRACE_TEST');                                                              
  9.                                                                                                                                 
  10.     CREATE OR REPLACE PROCEDURE populate_sql_trace_test( p_loops IN NUMBER ) AS                                                 
  11.        l_number   NUMBER;                                                                                                       
  12.     BEGIN                                                                                                                       
  13.        FOR i IN 1 .. p_loops                                                                                                    
  14.        LOOP                                                                                                                     
  15.           INSERT INTO sql_trace_test( id, description )                                                                         
  16.           VALUES ( i                                                                                                            
  17.                  , 'Description for '                                                                                           
  18.                    || i );                                                                                                      
  19.        END LOOP;                                                                                                                
  20.                                                                                                                                 
  21.        SELECT COUNT( * ) INTO l_number FROM sql_trace_test;                                                                     
  22.                                                                                                                                 
  23.        COMMIT;                                                                                                                  
  24.        DBMS_OUTPUT.put_line( l_number                                                                                           
  25.                             || ' rows inserted.' );                                                                             
  26.     END;                                                                                                                        
  27.     /                                                                                                                           
  28.                                                                                                                                 
  29.     SHOW ERRORS                                                                                                                 
  30.                                                                                                                                 
  31. 2.使用10046事件开启8级跟踪                                                                                                    
  32.     scott@CNMMBO> ALTER SESSION SET EVENTS '10046 trace name context forever, level 8';                                         
  33.                                                                                                                                 
  34.     Session altered.                                                                                                            
  35.                                                                                                                                 
  36.     scott@CNMMBO> EXEC populate_sql_trace_test(p_loops => 10);                                                                  
  37.     10 rows inserted.                                                                                                           
  38.                                                                                                                                 
  39.     PL/SQL procedure successfully completed.                                                                                    
  40.                                                                                                                                 
  41.     scott@CNMMBO> ALTER SESSION SET EVENTS '10046 trace name context off';                                                      
  42.                                                                                                                                 
  43.     Session altered.                                                                                                            
  44.                                                                                                                                 
  45.     scott@CNMMBO> ho ls -hltr /u02/database/CNMMBO/udump/*example*                                                              
  46.     -rw-r----- 1 Oracle oinstall 9.1K 2012-02-24 10:24 /u02/database/CNMMBO/udump/cnmmbo_ora_6667_trace_sql_example.trc          
  47.                                                                                                                               
  48. 3.使用tkprof工具格式化trace file                                                                                              
  49.     scott@CNMMBO> host                                                                                                          
  50.     oracle@SZDB:~/robinson/scripts/dba_scripts/custom/sql> cd /u02/database/CNMMBO/udump/                                       
  51.     oracle@SZDB:/u02/database/CNMMBO/udump> tkprof cnmmbo_ora_6667_trace_sql_example.trc sql_example.txt \                      
  52.     > explain=scott/tiger sys=no sort=prsela,exeela,fchela                                                                      
  53.                                                                                                                                 
  54.     TKPROF: Release 10.2.0.3.0 - Production on Fri Feb 24 10:33:16 2012                                                         
  55.                                                                                                                                 
  56.     Copyright (c) 1982, 2005, Oracle.  All rights reserved.                                                                     
  57.                                                                                                                                 
  58.     oracle@SZDB:/u02/database/CNMMBO/udump> more sql_example.txt                                                                
  59.                                                                                                                                 
  60.     TKPROF: Release 10.2.0.3.0 - Production on Fri Feb 24 10:33:16 2012                                                         
  61.                                                                                                                                 
  62.     Copyright (c) 1982, 2005, Oracle.  All rights reserved.                                                                     
  63.                                                                                                                                 
  64.     Trace file: cnmmbo_ora_6667_trace_sql_example.trc                                                                           
  65.     Sort options: prsela  exeela  fchela                                                                                        
  66.     ********************************************************************************                                            
  67.     count    = number of times OCI procedure was executed                                                                       
  68.     cpu      = cpu time in seconds executing                                                                                    
  69.     elapsed  = elapsed time in seconds executing                                                                                
  70.     disk     = number of physical reads of buffers from disk                                                                    
  71.     query    = number of buffers gotten for consistent read                                                                     
  72.     current  = number of buffers gotten in current mode (usually for update)                                                    
  73.     rows     = number of rows processed by the fetch or execute call                                                            
  74.     ********************************************************************************                                            
  75.                                                                                                                                 
  76.     BEGIN populate_sql_trace_test(p_loops => 10); END;                                                                          
  77.                                                                                                                                 
  78.     call     count       cpu    elapsed       disk      query    current        rows                                            
  79.     ------- ------  -------- ---------- ---------- ---------- ----------  ----------                                             
  80.     Parse        1      0.00       0.00          0          0          0           0                                            
  81.     Execute      1      0.00       0.00          0          0          0           1                                            
  82.     Fetch        0      0.00       0.00          0          0          0           0                                            
  83.     ------- ------  -------- ---------- ---------- ---------- ----------  ----------                                             
  84.     total        2      0.01       0.00          0          0          0           1                                            
  85.                                                                                                                                 
  86.     Misses in library cache during parse: 1                                                                                     
  87.     Optimizer mode: ALL_ROWS                                                                                                    
  88.     Parsing user id: 66  (SCOTT)                                                                                                
  89.                                                                                                                                 
  90.     Elapsed times include waiting on following events:                                                                          
  91.       Event waited on                             Times   Max. Wait  Total Waited                                               
  92.       ----------------------------------------   Waited  ----------  ------------                                                
  93.       log file sync                                   1        0.09          0.09                                               
  94.       SQL*Net message to client                       1        0.00          0.00                                               
  95.       SQL*Net message from client                     1        0.00          0.00                                               
  96.     ********************************************************************************                                            
  97.                                                                                                                                 
  98.     INSERT INTO SQL_TRACE_TEST( ID, DESCRIPTION )                                                                               
  99.     VALUES                                                                                                                      
  100.      ( :B1 , 'Description for ' || :B1 )                                                                                        
  101.                                                                                                                                 
  102.     call     count       cpu    elapsed       disk      query    current        rows                                            
  103.     ------- ------  -------- ---------- ---------- ---------- ----------  ----------                                             
  104.     Parse        1      0.00       0.00          0          0          0           0                                            
  105.     Execute     10      0.00       0.00          0          1         31          10                                            
  106.     Fetch        0      0.00       0.00          0          0          0           0                                            
  107.     ------- ------  -------- ---------- ---------- ---------- ----------  ----------                                             
  108.     total       11      0.00       0.00          0          1         31          10                                            
  109.                                                                                                                                 
  110.     Misses in library cache during parse: 1                                                                                     
  111.     Misses in library cache during execute: 1                                                                                   
  112.     Optimizer mode: ALL_ROWS                                                                                                    
  113.     Parsing user id: 66  (SCOTT)   (recursive depth: 1)                                                                         
  114.                                                                                                                               
  115.   /**************************************************/                                                                        
  116.   /* Author: Robinson Cheng                         */                                                                        
  117.   /* Blog:   http://blog.csdn.net/robinson_0612     */                                                                        
  118.   /* MSN:    robinson_0612@hotmail.com              */                                                                        
  119.   /* QQ:     645746311                              */                                                                        
  120.   /**************************************************/                                                                        
  121.                                                                                                                                 
  122. 4.演示应用程序上的跟踪(使用client identifier)                                                                                 
  123.     -->根据前面的描述创建角色并授予需要跟踪的用户以及添加client identifier的触发器,并启动应用程序                                 
  124.     oracle@2go-devDB01uv:/u02/database/SYBO2/udump> ls -hltr    -->移出不必要的trace文件以清除判断其产生的跟踪文件               
  125.     total 8.5K                                                                                                                  
  126.     drwxr-xr-x 2 oracle oinstall 8.5K 2012-02-24 11:52 old                                                                      
  127.                                                                                                                                       
  128.     oex_admin@SYBO2> create role sql_trace;                    -->创建角色                                                       
  129.                                                                                                                                 
  130.     Role created.                                                                                                               
  131.                                                                                                                                 
  132.     goex_admin@SYBO2> grant sql_trace to goex_webuser;         -->将角色授予用户。注:触发器logon_set_identifier已经创建         
  133.                                                                                                                                 
  134.     Grant succeeded.                                                                                                            
  135.                                                                                                                                 
  136.     -->启动应用程序后,session的信息如下                                                                                         
  137.     goex_admin@SYBO2> SELECT sid , serial#, username , client_identifier, client_info , module AS module_name                   
  138.       2  FROM   v$session WHERE username is not null;                                                                           
  139.                                                                                                                                 
  140.            SID    SERIAL# USERNAME             CLIENT_IDENTIFIER              CLIENT_INFO   MODULE_NAME                         
  141.     ---------- ---------- -------------------- ------------------------------ ------------- -----------------                    
  142.           1068       6250 GOEX_WEBUSER         GOEX_WEBUSER:Robinson.Cheng                  mBO.exe                             
  143.           1073       7201 GOEX_ADMIN                                                        SQL*Plus                            
  144.           1075       9115 GOEX_ADMIN                                                        SQL*Plus                            
  145.           1086      60120 GOEX_ADMIN                                                        TOAD 10.5.0.41                      
  146.                                                                                                                                 
  147.     -->开启基于client identifier的trace                                                                                          
  148.     goex_admin@SYBO2> exec DBMS_MONITOR.CLIENT_ID_TRACE_ENABLE('GOEX_WEBUSER:Robinson.Cheng',waits=>TRUE, binds=>FALSE );       
  149.                                                                                                                                 
  150.     PL/SQL procedure successfully completed.                                                                                    
  151.                                                                                                                                 
  152.     -->调用应用程序的report 模块并生成report,此时可以看到应用程序多产生了一个session,其sid,serial分别为1078,34788               
  153.     goex_admin@SYBO2> SELECT sid , serial#, username , client_identifier, client_info , module AS module_name                   
  154.       2  FROM   v$session WHERE username is not null;                                                                           
  155.                                                                                                                                 
  156.            SID    SERIAL# USERNAME             CLIENT_IDENTIFIER              CLIENT_INFO   MODULE_NAME                         
  157.     ---------- ---------- -------------------- ------------------------------ ------------- -----------------                    
  158.           1068       6250 GOEX_WEBUSER         GOEX_WEBUSER:Robinson.Cheng                  mBO.exe                             
  159.           1073       7201 GOEX_ADMIN                                                        SQL*Plus                            
  160.           1075       9115 GOEX_ADMIN                                                        SQL*Plus                            
  161.           1078      34788 GOEX_WEBUSER         GOEX_WEBUSER:Robinson.Cheng                  mBO.exe                             
  162.           1086      60120 GOEX_ADMIN                                                        TOAD 10.5.0.41                      
  163.                                                                                                                                 
  164.     -->report产生后关闭基于client identifier的trace                                                                              
  165.     goex_admin@SYBO2> exec DBMS_MONITOR.CLIENT_ID_TRACE_DISABLE('GOEX_WEBUSER:Robinson.Cheng');                                 
  166.                                                                                                                                 
  167.     PL/SQL procedure successfully completed.                                                                                    
  168.                                                                                                                                 
  169.     oracle@2go-devDB01uv:/u02/database/SYBO2/udump> ls -hltr     -->此时的udump目录下产生了两个trace文件                         
  170.     total 409K                                                                                                                  
  171.     drwxr-xr-x 2 oracle oinstall 8.5K 2012-02-24 11:52 old                                                                      
  172.     -rw-r----- 1 oracle oinstall 4.9K 2012-02-24 11:56 sybo2_ora_6419.trc                                                        
  173.     -rw-r----- 1 oracle oinstall 391K 2012-02-24 11:56 sybo2_ora_6422.trc                                                        
  174.                                                                                                                               
  175.     oracle@2go-devDB01uv:/u02/database/SYBO2/udump> cat sybo2_ora_6419.trc | grep SESSION                                       
  176.     *** SESSION ID:(1068.6250) 2012-02-24 11:56:19.617                                                                          
  177.     oracle@2go-devDB01uv:/u02/database/SYBO2/udump> cat sybo2_ora_6422.trc | grep SESSION                                       
  178.     *** SESSION ID:(1078.34788) 2012-02-24 11:56:20.650                                                                         
  179.                                IF ( DBMS_SESSION.is_role_enabled( 'SQL_TRACE' ) ) THEN                                          
  180.                                   DBMS_SESSION.set_identifier( UID );                                                           
  181.     ALTER SESSION  SET NLS_CALENDAR = 'GREGORIAN'  NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS' NLS_NUMERIC_CHARACTERS = '.,'      
  182.              
linux
相关资讯       oracle数据库教程 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款