1、使用 sqlplus 的 set autotrace 参数可以SQL语句的执行计划和统计信息;
2、但普通用户开启autotrace的时候可能报错 SP2-0618 SP2-0611
故障现象
.
SQL> conn wang/oracle
Connected.
SQL> set autotrace on
SP2-0618: Cannot find the Session Identifier. Check PLUSTRACE role is enabled
SP2-0611: Error enabling STATISTICS report
SQL> !oerr sp2 0618
00618, 0, "Cannot find the Session Identifier. Check PLUSTRACE role is enabled\n"
// *Cause: Unable to find the session identifier.
// *Action: Check that the PLUSTRACE role has been granted.
SQL> !oerr sp2 0611
00611, 0, "Error enabling %s report\n"
// *Document: NO
// *Cause: Continuation of 613 or 618.
// *Action:
解决办法
SQL> conn sys/oracle@earth as sysdba
SQL> @?/sqlplus/admin/plustrce--使用sys执行plustrce脚本创建plustrace角色
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> 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> set echo off
SQL> grant plustrace to wang;--将plustrace角色授予普通用户
Grant succeeded.
SQL> conn wang/oracle@earth
Connected.
SQL> set autotrace traceonly exp
SQL> select * from dual;
Execution Plan
----------------------------------------------------------
Plan hash value: 272002086
--------------------------------------------------------------------------
Id Operation Name Rows Bytes Cost (%CPU) Time
--------------------------------------------------------------------------
0 SELECT STATEMENT 1 2 2 (0) 00:00:01
1 TABLE ACCESS FULL DUAL 1 2 2 (0) 00:00:01
--------------------------------------------------------------------------