您的位置:首页 > 其它

普通用户使用set autotrace

2016-11-21 17:27 316 查看

关于set autotrace

SET AUTOTRACE OFF ---------------- 不生成AUTOTRACE 报告,这是缺省模式
SET AUTOTRACE ON EXPLAIN ------ AUTOTRACE只显示优化器执行路径报告
SET AUTOTRACE ON STATISTICS -- 只显示执行统计信息
SET AUTOTRACE ON ----------------- 包含执行计划和统计信息
SET AUTOTRACE TRACEONLY ------ 同set autotrace on,但是不显示查询输出

默认情况下,普通用户是没有set auotrace的权限的

SQL> conn test/oracle
Connected.
SQL> set autotrace on;
SQL> select * from user_tables;
Error ORA-942 while gathering statistics
SP2-0612: Error generating AUTOTRACE report

no rows selected

SP2-0612: Error generating AUTOTRACE report

Execution Plan
----------------------------------------------------------
ERROR:
ORA-01039: insufficient privileges on underlying objects of the view

SP2-0612: Error generating AUTOTRACE EXPLAIN report
SP2-0612: Error generating AUTOTRACE STATISTICS report

1. 在sys用户下运行plustrce.sql创建plustrace角色

[oracle@centos6 admin]$ cd $ORACLE_HOME/sqlplus/admin
[oracle@centos6 admin]$ ls
glogin.sql help libsqlplus.def plustrce.sql pupbld.sql
[oracle@centos6 admin]$ pwd
/u01/app/oracle/product/11.2.0/db_1/sqlplus/admin
[oracle@centos6 admin]$ cat plustrce.sql
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_$mystat to plustrace;
grant plustrace to dba with admin option;

set echo off
SQL> @plustrce.sql
SQL>
SQL> drop role plustrace;
drop role plustrace
*
ERROR at line 1:
ORA-01919: role 'PLUSTRACE' does not exist

SQL> create role plustrace;

Role created.

SQL>
SQL> grant select on v_$sesstat to plustrace;

Grant succeeded.

SQL> grant select on v_$statname to plustrace;

Grant succeeded.

SQL> grant select on v_$mystat to plustrace;

Grant succeeded.

SQL> grant plustrace to dba with admin option;

Grant succeeded.

SQL>
SQL> set echo off

2, 将plustrace角色赋给需要的用户

SQL> grant plustrace to test;

Grant succeeded.也可以直接赋给public角色

3. 创建plan_table,用来存储sql语句分析的结果

[oracle@centos6 admin]$ cd $ORACLE_HOME/rdbms/admin
[oracle@centos6 admin]$ ls utlxplan*
utlxplan.sql
[oracle@centos6 admin]$ cat utlxplan.sql
create table PLAN_TABLE (
statement_id varchar2(30),
plan_id number,
timestamp date,
remarks varchar2(4000),
operation varchar2(30),
options varchar2(255),
object_node varchar2(128),
object_owner varchar2(30),
object_name varchar2(30),
object_alias varchar2(65),
object_instance numeric,
object_type varchar2(30),
optimizer varchar2(255),
search_columns number,
id numeric,
parent_id numeric,
depth 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),
projection varchar2(4000),
time numeric,
qblock_name varchar2(30),
other_xml clob
);
SQL> conn test/oracle
Connected.
SQL> show user
USER is "TEST"
SQL> @utlxplan.sql

Table created.

SQL> set autotrace on
SQL> select * from user_tables;


即可正常使用set autotrace
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: