Oracle SQL 执行计划和分析小结

Oracle有多种方式获得一条SQL语句的查询计划,比如使用explain plan命令,在PLSQL Developer里面按F5快捷键等,但是作为SQL调优,比较简便的方式,还是在sqlplus中使用set autotrace on和set timing on的方式来得到SQL的实际运行时间和查询计划;

第一种:SQLPLUS中的autotrace是分析SQL的执行计划,执行效率的一个非常简单方便的工具,使用autotrace不会产生跟踪文件。利用autotrace工具提供的SQL执行计划和执行状态可以为优化SQL提供依据,以及效果的对比。

AutoTrace用法:

SET AUTOT[RACE] {OFF | ON | TRACE[ONLY]} |  [EXP[LAIN]] [STAT[ISTICS]]

例如:

SET AUTOTRACE OFF          停止AutoTrace
SET AUTOTRACE ON            开启AutoTrace,显示AutoTrace信息和SQL执行结果
SET AUTOTRACE TRACEONLY    开启AutoTrace,仅显示AutoTrace信息
SET AUTOTRACE ON EXPLAIN    开启AutoTrace,仅显示Autotrace的EXPLAIN信息
SET AUTOTRACE STATISTICS    开启AutoTrace,仅显示Autotrace的STATISTICS信息

AutoTrace启用:

1、使用dba角色用户sys设置权限,执行脚本plustrce.sql。

Oracle10g存放目录为${ORACLE安装目录}\product\10.2.0\db_1\sqlplus\admin\plustrce.sql

Oracle11g存放目录为${ORACLE安装目录}\product\11.2.0\dbhome_1\sqlplus\admin\plustrce.sql

plustrce.sql脚本用于给SQL*Plus Set AutoTrace命令创建角色plustrace访问动态性能视图。该脚本必须在DBA角色权限下执行,

执行完毕后,给需要使用AutoTrace功能的用户赋予权限。

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

相关推荐