Oracle SQL Trace
I came across a good deep dive white paper from an Oracle Development Expert, Cary Millsap. It was about SQL Trace. Well, as yet as a DBA, I have been avoiding SQL Trace without a second thought ever, but it indeed is one of most insightful utilities – not just for Developers but for DBAs (and more so because there are many Dev-DBAs that I know personally). There has to be some reasons why SQLTrace persisted to be a constant offering from Oracle since Oracle 6. From performance standpoint, enabling SQL Trace never hits >5%, which is quite acceptable for amount of time it can save.
Before speaking of anything else, always use following when doing a trace:
- binds => true : so it can tell what values the system is running with
- waits => true : so it can capture extended waits as (i.e. waits caused by OS operations)
Here’s how you can be using Traces:
1. Session Tracing
- dbms_monitor.session_trace_enable (or session_trace_disable). You can mention session_id, serial_num alongside which doest the magic.
- Inside a Trigger, specifically for the fast moving sessions which lasts for a couple of seconds. You can code them with a small trigger definition and with (session_id=>NULL, serial_num=>NULL)
- With, sys_context( ‘<USERENV>’,’<IP>’). It tells the environment details and the IP details
- Or, you can capture the suspicious SQL and can run them inside your own current session.
2. End to End Tracing
- dbms_monitor.client_id_trace_enable (client_id= ‘<IP>’, waits=> true, binds=> true, plan_stat=> ‘ALL_EXECUTIONS’);
- dbms_monitor.serv_mod_act_trace_enable (service_name=> ‘<SERVICE>’, module_name=> ‘<MODULE’, action_name=> ‘<ACTION>’, waits=> true, binds=> true, plan_stat=> ‘ALL_EXECUTIONS’);
3. Database or Instance Level Tracing: For end to end tracing at instance or database level, its important that your code passes back the client, service, module and action details. You can take this as a Development Best Practice, which by the way, is seldom mentioned in any Best Practices document. It can be done by making use of:
1. PL/SQL packages, i.e.
– dbms_session,
– dbms_application_info
- ILO or Instrumentation Library for Oracle, i.e.
– OCI_ATTR_CLIENT_IDENTIFIER,
– OCI_ATTR_SERVICE,
– OCI_ATTR_MODULE,
– OCI_ATTR_ACTION