Code: [Copy to clipboard] alter session set events '10046 trace name context forever, level 12' /* code to be traced goes here */ alter session set events '10046 trace name context off'
更好的方法是使用DBMS_SUPPORT包来激活扩展SQL跟踪:
Code: [Copy to clipboard] dbms_support.start_trace(waits=>true, binds=>true) /* code to be traced goes here */ dbms_support.stop_trace()
Code: [Copy to clipboard] dbms_support.start_trace_in_session( sid => 42, serial# => 1215, waits => true, binds => true) /* code to be traced executes during this time window */ dbms_support.stop_trace_in_session( sid => 42, serial => 1215)
Code: [Copy to clipboard] alter session set timed_statistics=true; alter session set max_dump_file_size=unlimited; alter session set tracefile_identifier='Hello'; /* only in Oracle Database 8.1.7and later */ alter session set events '10046 trace name context forever, level 12'; select 'Howdy, it is 'sysdate from dual; exit;
然后在由USER_DUMP_DEST实例参数的值命名的目录中寻找文件名中包含字符串"Hello"的最新写入的.trc文件。用你最喜欢的文本编辑器打开它。 阅读Oracle MetaLink注释39817.1或(Optimizing Oracle Performance,《优化Oracle性能》)一书,以便大概了解原始跟踪文件中有些什么。一定要运行跟踪文件上的tkPRof,并研究其输出,但也不要由于有了tkprof就不再看原始的跟踪文件。跟踪文件中还有许多tkprof没有向你展示的内容。 假如你不仅需要一个由简单的SELECT from DUAL 生成的跟踪文件,还需要一个更感爱好的跟踪文件,那么需要跟踪下面这条SQL语句:
Code: [Copy to clipboard] select object_type, owner, object_name from dba_objects;