O slideshow foi denunciado.
Utilizamos seu perfil e dados de atividades no LinkedIn para personalizar e exibir anúncios mais relevantes. Altere suas preferências de anúncios quando desejar.

SQL Tracing

Presentation at Oracle APAC User Group, Singapore 13-Jun-19

  • Entre para ver os comentários

SQL Tracing

  1. 1. SQL Tracing Methods in Oracle using the command-line Hemant K Chitale https://hemantoracledba.blogspot.com Oracle APAC User Group 13-June-19
  2. 2. Why Trace SQL Statements ? • To identify individual calls in a session or a user interaction – for diagnostics • To identify the actual path being taken – e.g. a trigger being executed, results in another table being updated • To identify performance issues • To evaluate execution plan alternatives
  3. 3. Tracing your own session • Parameter : SQL_TRACE : “ALTER SESSION SET SQL_TRACE=TRUE” ; execute the SQL Statements ; reset SQL_TRACE to FALSE {Note : This parameter has been deprecated in recent versions} • DBMS_SESSION.SET_SQL_TRACE : “exec DBMS_SESSION.SET_SQL_TRACE(sql_trace=>TRUE);”; execute the SQL statements ; reset to FALSE • DBMS_SESSION.SESSION_TRACE_ENABLE : “exec DBMS_SESSION.SESSION_TRACE_ENABLE(waits=>TRUE, binds=>FALSE);” ; execute the SQL statements ; call SESSION_TRACE_DISABLE. This is the preferred method • Note : I don’t demonstrate EVENT 10046 because I don’t use it any more
  4. 4. Using DBMS_APPLICATION_INFO • This package allows individual sessions to setup information that can be used to identify and trace their executions • For example : This has the session sets the identifiers that are used for tracing it exec dbms_application_info.set_client_info('HR App Client'); exec dbms_application_info.set_module('Employee Module','Updating');
  5. 5. Demonstration
  6. 6. Setting Client Identifier • This can be set by the session to allow the DBA to identify it DBMS_SESSION.SET_IDENTIFIER( client_id VARCHAR2);
  7. 7. Tracing another session (1) • DBMS_MONITOR.SESSION_TRACE_ENABLE exec DBMS_MONITOR.SESSION_TRACE_ENABLE(- > session_id=>102,- > serial_num=>34,- > waits=>TRUE,- > binds=>TRUE); ➢(call SESSION_TRACE_DISABLE at the end) • DBMS_MONITOR.SERV_MOD_ACT_TRACE_ENABLE : (Note : Will apply to all sessions that have these identifiers set) exec DBMS_MONITOR.SERV_MOD_ACT_TRACE_ENABLE(- > service_name=>'orcl',- > module_name=>'Employee Module',- > action_name=>'Updating'); ➢(call SERV_MOD_ACT_TRACE_DISABLE at the end)
  8. 8. Demonstration
  9. 9. Tracing another session (2) • This uses the the Client Identifier information DBMS_MONITOR.CLIENT_ID_TRACE_ENABLE( client_id IN VARCHAR2, waits IN BOOLEAN DEFAULT TRUE, binds IN BOOLEAN DEFAULT FALSE); DBMS_MONITOR.CLIENT_ID_STAT_ENABLE( client_id IN VARCHAR2); ➢This collects statistics into V$CLIENT_STATS • (call the respective DISABLE procedures at the end)
  10. 10. Tracing a Particular Process • The Oracle PROCESS identifier is V$PROCESS.PID. So you can use this information : alter system set events 'sql_trace {process: orapid=22}'; alter system set events 'sql_trace {process: orapid=22} off'; • The OS Process identifier is V$PROCESS.SPID. So you can use this information : alter system set events 'sql_trace {process:3207}'; alter system set events 'sql_trace {process:3207} off’; (V$PROCESS can be joined to V$SESSION on V$PROCESS.ADDR=V$SESSION.PADDR)
  11. 11. Tracing for a specific SQL Statement • 11g onwards allows for tracing for a specific SQL statement. This is useful when you don’t know or can’t predict in advance which session will be executing the SQL statement, you only have the SQL_ID for it (The SQL_ID may have been obtained from previous tracing or debugging files) alter system set events 'sql_trace [sql:06d4jjswswagq] wait=true, plan_stat=all_executions’; The options for "plan_stat" are "never", "first_execution", "all_executions"). This allows us to capture execution plan statistics. Disable tracing with alter system set events 'sql_trace [sql:06d4jjswswagq] off';
  12. 12. Identifying the Trace File for a Session • Each session being traced has a PROCESS identifier that can be used • Pre-11g we would use a query like this : select p.value || '/' || instance_name || '_ora_' || p.spid || '.trc' from v$parameter p, v$process p, v$session s , v$instance where p.name = 'user_dump_dest' and s.username = 'HEMANT' and s.sid = 145 and s.serial#=11 and p.addr=s.paddr • 11g has the trace file name in v$PROCESS
  13. 13. Explicitly setting the TraceFileName • A session can explicitly set a portion of the TraceFileName with : alter session set tracefile_identifier='Hemant'; • And then query V$DIAG_INFO (11g onwards) select value from v$diag_info where name = 'Default Trace File' ;
  14. 14. Interpreting the Trace Files • We don’t have time in today’s session, so I suggest that you look at the links at https://hemantoracledba.blogspot.com/2016/03/recent-blog-series- on-sql-tracing.html
  15. 15. Example from TraceFile PARSING IN CURSOR #140508446427592 len=25 dep=0 uid=142 oct=3 lid=142 tim=4902773054 hv=3857453280 ad='78804848' sqlid='04q6ujzkys470' select * from sales_data END OF STMT PARSE #140508446427592:c=411,e=1502,p=0,cr=0,cu=0,mis=1,r=0,dep=0,og=1,plh=3621922585,tim=4902773053 EXEC #140508446427592:c=0,e=33,p=0,cr=0,cu=0,mis=0,r=0,dep=0,og=1,plh=3621922585,tim=4902773193 WAIT #140508446427592: nam='SQL*Net message to client' ela= 3 driver id=1413697536 #bytes=1 p3=0 obj#=0 tim=4902773262 FETCH #140508446427592:c=0,e=87,p=0,cr=10,cu=2,mis=0,r=1,dep=0,og=1,plh=3621922585,tim=4902773382 WAIT #140508446427592: nam='SQL*Net message from client' ela= 318 driver id=1413697536 #bytes=1 p3=0 obj#=0 tim=4902773760 WAIT #140508446427592: nam='SQL*Net message to client' ela= 1 driver id=1413697536 #bytes=1 p3=0 obj#=0 tim=4902773851 FETCH #140508446427592:c=0,e=86,p=0,cr=16,cu=2,mis=0,r=1,dep=0,og=1,plh=3621922585,tim=4902773894 STAT #140508446427592 id=1 cnt=2 pid=0 pos=1 obj=0 op='PARTITION RANGE ALL PARTITION: 1 6 (cr=26 pr=0 pw=0 str=1 time=83 us cost=3 size=50 card=2)' STAT #140508446427592 id=2 cnt=2 pid=1 pos=1 obj=79798 op='TABLE ACCESS FULL SALES_DATA PARTITION: 1 6 (cr=26 pr=0 pw=0 str=6 time=128 us cost=3 size=50 card=2)'
  16. 16. Optimizer Tracing • Pre-11g, you would have to use EVENT=10053 to get an Optimizer Trace. • 11g onwards allows the Optimizer Event to be set OR a new DIAG utility : alter system set events 'trace[rdbms.SQL_Optimizer.*][sql:b086mzzp82x7w]’; alter session set events 'trace[rdbms.SQL_Optimizer.*][sql:b086mzzp82x7w]’; begin dbms_sqldiag.dump_trace(p_sql_id=>'b086mzzp82x7w', p_component=>'Optimizer', p_file_id=>'OPT_TRACE_b086mzzp82x7w'); end;