问题
I'm developing an app using Oracle database and I would surely like to have an SQL statements tracer that could trace sessions and processes statements - like Toad's SQL Tracker/Monitor. But since the good one cost alot of money I'm thinking about building a small one myself. Any ideeas about wich would be the best solution for tracing oracle sql statements?
回答1:
Sql Plus + tkprof.
alter session set timed_statistics = true;
alter session set sql_trace = true;
show parameter user_dump_dest
tkprof <trc-файл> <txt-файл>
If you need to trace any session (not only your own):
select sid,serial# from v$session
to look at sid of session and
begin
sys.dbms_system.set_ev(sid, serial#, 10046, 12, '');
end;
Otherwise you can use logon trigger:
CREATE OR REPLACE TRIGGER SYS.TRACE_A_USER
AFTER
LOGON ON <some_db_user>.SCHEMA
DECLARE
user_sid NUMBER;
user_serial# NUMBER;
user_program VARCHAR2(48);
BEGIN
-- Collect the current user session details.
SELECT sid, serial#, UPPER(program)
INTO user_sid, user_serial#, user_program
FROM v$session
WHERE audsid = USERENV('SESSIONID');
-- Start tracing if the user is running the identified application.
IF user_program = 'SOMECODE.EXE' THEN
-- Enable tracing. Note level 12 tracing includes bind variable
-- and wait statistics.
sys.dbms_system.set_ev(user_sid, user_serial#, 10046, 12, '');
END IF;
END;
来源:https://stackoverflow.com/questions/4244017/trace-oracle-statements