1. 使用参数SQL_TRACE

下面是官网对此参数的说明

SQL_TRACE

Property Description
Parameter type Boolean
Default value false
Modifiable ALTER SESSIONALTER SYSTEM
Range of values true | false

SQL_TRACE enables or disables the SQL trace facility. Setting this parameter to true provides information on tuning that you can use to improve performance. You can change the value using the DBMS_SYSTEM package.

Caution:

Using this initialization parameter to enable the SQL trace facility for the entire instance can have a severe performance impact. Enable the facility for specific sessions using the ALTER SESSION statement. If you must enable the facility on an entire production environment, then you can minimize performance impact by:

  • Maintaining at least 25% idle CPU capacity

  • Maintaining adequate disk space for the USER_DUMP_DEST location

  • Striping disk space over sufficient disks

See Also:

Oracle Database Performance Tuning Guide for more information about performance diagnostic tools

Note:

The SQL_TRACE parameter is deprecated. Oracle recommends that you use the DBMS_MONITOR and DBMS_SESSION packages instead. SQL_TRACE is retained for backward compatibility only.

 

可以在initxxx.ora 文件中设置此参数.

alter system set SQL_TRACE = TRACE  scope=both;

alter session set SQL_TRACE = TRUE; ----给当前session开启trace.

11G 之前获取session对应的trace文件的完整路径 可以使用下面的SQL

SELECT    a.VALUE || b.symbol || c.instance_name || '_ora_' || d.spid || '.trc' trace_file
  FROM (SELECT VALUE FROM v$parameter WHERE NAME = 'user_dump_dest') a,
       (SELECT SUBSTR (VALUE, -6, 1) symbol FROM v$parameter
         WHERE NAME = 'user_dump_dest') b,
       (SELECT instance_name FROM v$instance) c,
       (SELECT spid FROM v$session s, v$process p, v$mystat m
         WHERE s.paddr = p.addr AND s.SID = m.SID AND m.statistic# = 0) d
/

11G 还可以使用新增的v$diag_info 视图直接找

SELECT VALUE FROM V$DIAG_INFO WHERE NAME = 'Default Trace File';

生产的trc 文件 可以用oracle自带的tkprof进行处理,原始的trc也能看出很多信息,但是没那么直观,处理后会好很多.

2. 使用10046事件

10046 事件按照收集信息内容,可以分成4个级别:
Level 1: 等同于SQL_TRACE 的功能
Level 4: 在Level 1的基础上增加收集绑定变量的信息
Level 8: 在Level 1 的基础上增加等待事件的信息
Level 12:等同于Level 4+Level 8, 即同时收集绑定变量信息和等待事件信息。
 
对当前session 使用10046事件
SQL>alter session set events ‘10046 trace name context forever, level 12’; --启动10046事件
执行相关事务
SQL>alter session set events ‘10046 trace name context off’; -- 关闭10046事件

3. 使用PL/SQL包

SQL_Trace和10046事件都不能单独给别人的session开启trace,下面的PL/SQL包能做到。

  1. DBMS_MONITOR
  2. DBMS_SESSION
  3. DBMS_SYSTEM(默认只有sys才能执行,其他用户需要执行必须先授权.)
  4. DBMS_SUPPORT(The DBMS_SUPPORT package is not present by default, but can be loaded as the SYS user by executing the "@$ORACLE_HOME/rdbms/admin/dbmssupp.sql" script.)
  • DBMS_MONITOR

With the advent of Oracle 10g the SQL tracing options have been centralized and extended using the DBMS_MONITOR package. The examples below show just a few possible variations for enabling and disabling SQL trace in Oracle 10g.

-- Oracle 10g
SQL> EXEC DBMS_MONITOR.session_trace_enable;
SQL> EXEC DBMS_MONITOR.session_trace_enable(waits=>TRUE, binds=>FALSE);
SQL> EXEC DBMS_MONITOR.session_trace_disable; SQL> EXEC DBMS_MONITOR.session_trace_enable(session_id=>1234, serial_num=>1234);
SQL> EXEC DBMS_MONITOR.session_trace_enable(session_id =>1234, serial_num=>1234, waits=>TRUE, binds=>FALSE);
SQL> EXEC DBMS_MONITOR.session_trace_disable(session_id=>1234, serial_num=>1234); SQL> EXEC DBMS_MONITOR.client_id_trace_enable(client_id=>'tim_hall');
SQL> EXEC DBMS_MONITOR.client_id_trace_enable(client_id=>'tim_hall', waits=>TRUE, binds=>FALSE);
SQL> EXEC DBMS_MONITOR.client_id_trace_disable(client_id=>'tim_hall'); SQL> EXEC DBMS_MONITOR.serv_mod_act_trace_enable(service_name=>'db10g', module_name=>'test_api', action_name=>'running');
SQL> EXEC DBMS_MONITOR.serv_mod_act_trace_enable(service_name=>'db10g', module_name=>'test_api', action_name=>'running', -
> waits=>TRUE, binds=>FALSE);
SQL> EXEC DBMS_MONITOR.serv_mod_act_trace_disable(service_name=>'db10g', module_name=>'test_api', action_name=>'running');

The package provides the conventional session level tracing along with two new variations. First, tracing can be enabled on multiple sessions based on the value of the client_identifier column of the V$SESSIONview, set using the DBMS_SESSION package. Second, trace can be activated for multiple sessions based on various combinations of the service_name, module, action columns in the V$SESSION view, set using the DBMS_APPLICATION_INFO package, along with the instance_name in RAC environments. With all the possible permutations and default values this provides a high degree of flexibility.

给当前session设置 client_identifier

exec dbms_session.set_identifier('myid');

给当前session设置module , action

exec dbms_application_info.set_module('test','youraction');

  • DBMS_SESSION

Trace can be enabled in the current session using the DBMS_SESSION package. This can be useful if you need to enable trace from within a PL/SQL package.

Trace is enabled at session level using

    EXECUTE dbms_session.set_sql_trace (TRUE);

Trace is disabled at session level using

    EXECUTE dbms_session.set_sql_trace (FALSE);

Trace can be enabled in the current session using the DBMS_SUPPORT package. This provides more flexibility than DBMS_SESSION.

Trace is enabled at session level using

    EXECUTE dbms_support.start_trace;

With no parameters, this procedure enables level 1 trace

Event 10046 level 4 trace can be enabled using

    EXECUTE dbms_support.start_trace (binds=>true);

Event 10046 level 8 trace can be enabled using

    EXECUTE dbms_support.start_trace (waits=>true);

Event 10046 level 12 trace can be enabled using

    EXECUTE dbms_support.start_trace (binds=>true,waits=>true);

Trace can be disabled using

    EXECUTE dbms_support.stop_trace;

DBMS_SYSTEM

Trace can be also be enabled in another session using the DBMS_SYSTEM package

The SID and the serial number of the target session must be obtained from V$SESSION. In this case the serial number must be specified

For example to enable trace in a session with SID 9 and serial number 29 use

    EXECUTE dbms_system.set_sql_trace_in_session (9,29,TRUE);

Note this is equivalent to enabling event 10046 level 1

To disable trace in the same session use

    EXECUTE dbms_system.set_sql_trace_in_session (9,29,FALSE);

Event 10046 trace can also be enabled in another session using the DBMS_SYSTEM package

The SID and the serial number of the target session must be obtained from V$SESSION.

For example to enable event 10046 level 8 in a session with SID 9 and serial number 29 use

    EXECUTE dbms_system.set_ev (9,29,10046,8,'');

To disable event 10046 in the same session use

    EXECUTE dbms_system.set_ev (9,29,10046,0,'');

DBMS_SUPPORT

Trace can be enabled in another session using the DBMS_SUPPORT package

The SID and optionally the serial number if the target session must be obtained from V$SESSION. The serial number can optionally be specified as 0.

For example to enable level 1 trace in a session with SID 9 and serial number 29 use

    EXECUTE dbms_support.start_trace_in_session (9,29);

With no parameters, this procedure enables level 1 trace

Event 10046 level 4 trace can be enabled using

    EXECUTE dbms_support.start_trace_in_session (9,29,binds=>true);

Event 10046 level 8 trace can be enabled using

    EXECUTE dbms_support.start_trace_in_session (9,29,waits=>true);

Event 10046 level 12 trace can be enabled using

    EXECUTE dbms_support.start_trace_in_session (9,29,binds=>true,waits=>true);

Trace can be disabled using

    dbms_support.stop_trace_in_session (9,29);

Oracle获取session的trace的更多相关文章

  1. Oracle获取session的IP方法

    方法1 创建触发器:  create orreplace trigger login_on  alfterlogon on database  begin  dbms_application_info ...

  2. 【方法整理】Oracle 获取trace跟踪文件名的几种常用方式

    [方法整理]Oracle 获取trace跟踪文件名的几种常用方式 1  BLOG文档结构图     2  前言部分 2.1  导读和注意事项 各位技术爱好者,看完本文后,你可以掌握如下的技能,也可以学 ...

  3. oracle获取执行计划及优缺点 详解

    一.获取执行计划的6种方法(详细步骤已经在每个例子的开头注释部分说明了):1. explain plan for获取: 2. set autotrace on : 3. statistics_leve ...

  4. [Oracle] 获取运行计划的各方法总结

    总的结论: 一.获取运行计划的6种方法(具体步骤已经在每一个样例的开头凝视部分说明了): 1. explain plan for获取:  2. set autotrace on .  3. stati ...

  5. Oracle获取alter.log的方法

    10g下:可以在 admin\{sid}\pfile文件下的init.ora文件中找到以下内容:audit_file_dest = C:\ORACLE\PRODUCT\10.2.0\ADMIN\ORC ...

  6. Oracle 获取当前日期及日期格式

    http://blog.sina.com.cn/s/blog_6168ee920100l2ye.html Oracle 获取当前日期及日期格式 获取系统日期:  SYSDATE()   格式化日期:  ...

  7. WebAPI中无法获取Session对象的解决办法

    在MVC的WebApi中默认是没有开启Session会话支持的.需要在Global中重写Init方法来指定会话需要支持的类型 public override void Init() { PostAut ...

  8. 在IHttpHandler中获取session

    因为业务要异步通过IHttpHandler获得数据,但还要根据当前登录人员的session过滤,因此要在在IHttpHandler中获取session 方法是HttpHandler容器中如果需要访问S ...

  9. Java通过sessionId获取Session

    Servlet2.1之后不支持SessionContext里面getSession(String id)方法. 但是,我们可以通过HttpSessionListener监听器和全局静态map自己实现一 ...

随机推荐

  1. activity和Task 有关的 Intent启动方式结合intent.setFlags()

      通过设置Intent对象的标记,来改变当前任务堆栈中既存的Activity的顺序: FLAG_ACTIVITY_NEW_TASK----简而言之,跳转到的activity根据情况,可能压在一个新建 ...

  2. Netty核心概念(9)之Future

    1.前言 第7节讲解JAVA的线程模型中就说到了Future,并解释了为什么可以主线程可以获得线程池任务的执行后结果,变成一种同步状态.秘密就在于Java将所有的runnable和callable任务 ...

  3. OpenGL12-shader(GLSL)着色语言3-(属性参数)(代码已上传)

    上一个例程中,使用了uniform 类型的变量,uniform可以理解为全局变量,这一节中使用 的是attribute类型的变量,翻译过来就是属性,他是与顶点绑定的,就意味着一个顶点可以 有很多个属性 ...

  4. [转] Ubuntu 14.04/14.10下安装VMware Workstation 11图文教程

    点击这里查看原文 译者:GuiltyMan 本文由 Linux公社翻译组 原创翻译  Linux公社 诚意奉献 更多请访问此处博客网站 VMware workstation 是一个可以进行桌面操作的虚 ...

  5. Pipenv——最好用的python虚拟环境和包管理工具

    pipenv 是Kenneth Reitz大神的作品,能够有效管理Python多个环境,各种包.过去我们一般用virtualenv搭建虚拟环境,管理python版本,但是跨平台的使用不太一致,且有时候 ...

  6. maven环境搭建Myeclipse配置

    一.Maven的下载安装 准备工作: 1.安装环境:windows 2.需安装JDK,并配置环境变量(略) 3.Maven版本3.0.5 4.下载地址:链接:https://pan.baidu.com ...

  7. 移动端H5适配方法(盒子+图片+文字)

    一.怎么让H5页面适应手机 a.利用meta标签 <meta name="viewport" content="width=device-width,initial ...

  8. Node.js进程管理之进程集群

    一.cluster模块 Node.js是单线程处理,对于高并发的请求怎么样能增加吞吐量呢?为了提高服务器的利用率,能不能多核的来处理呢?于是就有了cluster模块. cluster模块可以轻松实现运 ...

  9. EMC,EMI,EMS,ESD分别是什么?有什么区别和联系?

    一.EMC EMI EMS定义: EMC(ElectromagneticCompatibility) 电磁兼容,是指设备或系统在电磁环境中性能不降级的状态.电磁兼容,一方面要求系统内没有严重的干扰源, ...

  10. RabbitMQ.NET In Window Service

    工作中要求使用RabbitMQ,以Windows Service 模式启动,中间有遇到一些问题,网上大部分博客有误导倾向, 在这里做一个简单的记录,以免后面的人走坑: 1. 自动重新连接,不需要手动处 ...