配置AUTOTRACE的一种方法

配置AUTOTRACE的一种方法

AUTOTRACE是Sql*Plus中的一个工具,可以显示所执行查询的解释计划(explain plan)以及所用的资源。

配置AUTOTRACE的方法不止一种,以下是Tom采用的方法:
(1)作为system登录SQL*Plus,运行[ORACLE_HOME]/rdbms/admin/utlxplan.sql
   以下是该脚本的主要内容:  
Rem
Rem This is the format for the table that is used by the EXPLAIN PLAN
Rem statement.  The explain statement requires the presence of this
Rem table in order to store the descriptions of the row sources.

create table PLAN_TABLE (
 statement_id  varchar2(30),
 timestamp     date,
 remarks       varchar2(80),
 operation     varchar2(30),
 options        varchar2(255),
 object_node   varchar2(128),
 object_owner  varchar2(30),
 object_name   varchar2(30),
 object_instance numeric,
 object_type     varchar2(30),
 optimizer       varchar2(255),
 search_columns  number,
 id  numeric,
 parent_id numeric,
 position numeric,
 cost  numeric,
 cardinality numeric,
 bytes  numeric,
 other_tag       varchar2(255),
 partition_start varchar2(255),
        partition_stop  varchar2(255),
        partition_id    numeric,
 other  long,
 distribution    varchar2(30),
 cpu_cost numeric,
 io_cost  numeric,
 temp_space numeric,
        access_predicates varchar2(4000),
        filter_predicates varchar2(4000));

(2)创建一个表plan_table的同义词
运行CREATE PUBLIC SYNONYM PLAN_TABLE FOR PLAN_TABLE;
(3)授权,使任何人都可以使用SQL*Plus进行跟踪,这样就不需要每个用户都安装自己的计划表。
运行GRANT all on plan_table to public;

当然,也可以只将plan_table授权给某个用户。
还有一种作法,在想要使用AUTOTRACE的每个模式中分别运行@utlxplan.

(4)创建并授予plustrace角色:
作为sys或sysdba登录SQL*Plus,运行[ORACLE_HOME]/sqlplus/admin/plustrce.sql
以下是plustrce.sql的主要内容:
--   sqlplus "/ as sysdba" @plustrce
--
--   Catalog.sql must have been run before this file is run.
--   This file must be run while connected to a DBA schema.

set echo on

drop role plustrace;
create role plustrace;

grant select on v_$sesstat to plustrace;
grant select on v_$statname to plustrace;
grant select on v_$session to plustrace;
grant plustrace to dba with admin option;

set echo off

并将该角色授予public, 即 grant plustrace to public.当然,可以把grant命令中的public替换为某个用户。

AUTOTRACE会自动生成一个报告,其中可能列出SQL优化器所用的执行路径,以及语句的执行统计信息。成功执行SQL DML(即select,delete,update,merge和insert)语句后就会生成这个报告。它对于监视并调优这些语句的性能很有帮助。

通过设置AUTOTRACE系统变量可以控制这个报告:
set autotrace off:         不生成autotrace报告,这是默认设置
set autotrace on explain:  autotrace报告只显示优化器执行路径
set autotrace on statistics: autotrace报告只显示sql语句的执行统计信息
set autotrace on: autotrace报告既包括优化器执行路径,又包括SQL语句的执行统计信息。 
set autotrace traceonly: 这与set autotrace on类似,但是不显示用户的查询输出(如果有的话)

你可能感兴趣的:(auto)