DBAtlas · Database operations atlas

Enable/disable 10046 trace for a session and summarize with TKPROF

OraclePerformance

Command

-- 1) Find the target session and its trace file:
SELECT s.sid, s.serial#, s.username, s.module, p.tracefile
FROM   v$session s
  JOIN v$process p ON p.addr = s.paddr
WHERE  s.username = 'CUSTOMER_APP';

-- 2) Enable trace (waits + binds = equivalent of level 12):
BEGIN
  DBMS_MONITOR.SESSION_TRACE_ENABLE(
    session_id => <sid>,
    serial_num => <serial#>,
    waits      => TRUE,
    binds      => TRUE,
    plan_stat  => 'ALL_EXECUTIONS');
END;
/
-- Verify: SELECT sql_trace, sql_trace_waits, sql_trace_binds FROM v$session WHERE sid = <sid>;

-- 3) Disable once the problem operation completes:
EXEC DBMS_MONITOR.SESSION_TRACE_DISABLE(session_id => <sid>, serial_num => <serial#>);

-- Alternatives:
-- Your own session: ALTER SESSION SET EVENTS '10046 trace name context forever, level 12';
-- Single SQL_ID, all sessions: ALTER SYSTEM SET EVENTS 'sql_trace[SQL: <sql_id>] wait=true, bind=true';
--   disable: ALTER SYSTEM SET EVENTS 'sql_trace[SQL: <sql_id>] off';
-- For a connection pool: DBMS_MONITOR.CLIENT_ID_TRACE_ENABLE / SERV_MOD_ACT_TRACE_ENABLE

-- 4) Summarize with TKPROF on the OS (most expensive SQL first):
-- $ tkprof /u01/app/oracle/diag/rdbms/orcl/ORCL/trace/ORCL_ora_12345.trc /tmp/ora_12345.prf \
--     sys=no sort=exeela,fchela,prsela waits=yes aggregate=yes

What it does

Extended SQL trace (10046) writes parse/execute/fetch times, wait events and bind values for every SQL the session runs into a trace file; TKPROF turns it into a readable report aggregated per SQL.

When to use it

When AWR/ASH are not enough and you must prove end to end where a single user operation spends its time; for application-to-database round-trip (SQL*Net) problems.

Caution

No license required. Trace files grow quickly and add overhead to the target session; keep it short and always disable it (check DBA_ENABLED_TRACES for leftovers). binds=TRUE writes sensitive data (national IDs, card numbers, etc.) into the trace file. ALTER SESSION SET EVENTS requires the ALTER SESSION privilege (otherwise ORA-01031). TKPROF does not show bind values by default; keep the raw .trc file.

Open in DBAtlas →

Related