0
点赞
收藏
分享

微信扫一扫

Oracle授权普通用户查看执行计划


利用不同的方法查看执行计划有对应不同的授权方法。

一、 explain plan与dbms_xplan.display

最常用的方法,plsqldev的F5对应也是这个。这个命令会产生执行计划,并把执行计划保存到"PLAN_TABLE"中,查看执行计划使用 select * from table(dbms_xplan.display);

启用方法(9i需手动启用):

--创建PLAN_TABLE,存储执行计划
SQL> @$ORACLE_HOME/rdbms/admin/utlxplan.sql

--创建同义词使多个用户可以共享一个plan_table,并授权给public
create public synonym plan_table for plan_table;
grant all on plan_table to public;

Prerequisites

To issue an EXPLAIN PLAN statement, you must have the privileges necessary to insert rows into an existing output table that you specify to hold the execution plan.

You must also have the privileges necessary to execute the SQL statement for which you are determining the execution plan. If the SQL statement accesses a view, then you must have privileges to access any tables and views on which the view is based. If the view is based on another view that is based on a table, then you must have privileges to access both the other view and its underlying table.

To examine the execution plan produced by an EXPLAIN PLAN statement, you must have the privileges necessary to query the output table.

The EXPLAIN PLAN statement is a data manipulation language (DML) statement, rather than a data definition language (DDL) statement. Therefore, Oracle Database does not implicitly commit the changes made by an EXPLAIN PLAN statement. If you want to keep the rows generated by an EXPLAIN PLAN statement in the output table, then you must commit the transaction containing the statement.

参考 EXPLAIN PLAN

二、 autotrace

实际上它需要两个东西:PLAN_TABLE 和 plustrace角色,另外plsqldev是不支持用autotrace的。

执行以下脚本创建plustrace角色,并执行grant plustrace to public授权,这样所有用户都可以使用autotrace跟踪工具。

@?/sqlplus/admin/plustrce.sql
grant plustrace to public;

脚本内容如下

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

三、 dbms_xplan.display_cursor

基本信息:select * from table(dbms_xplan.display_cursor);

grant select on v_$session to tempuser;
grant select on v_$sql_plan to tempuser;
grant select on v_$sql to tempuser;

高级统计:select * from table(dbms_xplan.display_cursor(null,null,'ADVANCED  ALLSTATS LAST PEEKED_BINDS'));

grant select on v_$sql_plan_statistics_all to tempuser;
--这里statistics_level =all让统计信息最为详尽
alter session set statistics_level =all;

查看真实执行计划

set linesize 200 pagesize 999
alter session set statistics_level=all;
-- 执行sql
select * from table(dbms_xplan.display_cursor(null,null,'advanced -PROJECTION iostats,last'));

参考

oracle 9i启用Execution Plan的方法 | 学步园

utlxplan_ITPUB博客

https://www.iteye.com/blog/falchion-616234

授权普通非DBA用户可以有权限查看执行计划的方法


举报

相关推荐

0 条评论