---
title: "使用SQLPLUS分析SQL语句（查询执行计划跟踪）"
author: "longflang"
date: "2010-11-10T19:16:59Z"
tags: []
categories: ["数据库"]
slug: "sqlplus-sql"
draft: false
lastmod: "2010-11-10T19:16:59Z"
id: 807945
url: "https://longflang.iteye.com/blog/807945"
---

方法一：autotrace

1，  connect sys/密码 as sysdba，在sys用户下运行$ORACLE\_HOME/sqlplus/admin/plustrce.sql  
这段[**sql**]()的实际内容如下：  
set echo on  
drop role plustrace;  
create role plustrace;  
grant [select](http://www.dnbcw.com/biancheng/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

以上产生plustrace角色,然后在sys用户下把此角色赋予一般用户  
SQL&gt; grant PLUSTRACE to 用户名;

2，然后在当前用户SQL&gt;下运行$ORACLE\_HOME/rdbms/admin/utl[xp](http://www.dnbcw.com/biancheng/xp)lan.sql，它会创建一个plan\_table，用来存储分析SQL语句的结果。

实际执行的是如下语句：

create table PLAN\_TABLE (  
                          statement\_id    varchar2(30),  
                          timestamp       date,  
                          remarks         varchar2(80),  
                          operation       varchar2(30),  
                          options         varchar2(30),  
                          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));

3，SQL/PLUS的窗口运行以下命令

set time on;                 (说明：打开时间显示）（可选）  
set autotrace on;            (说明：打开自动分析统计，并显示SQL语句的运行结果）  
set autotrace traceonly;     (说明：打开自动分析统计，不显示SQL语句的运行结果）

4，接下来你就运行需要查看执行计划的SQL语句，看到其分析统计结果了。一般来讲，我们的SQL语句应该避免对大表的全表扫描。

5，关闭以上功能，在SQL/PLUS的窗口运行以下命令

set time off;                      (说明：关闭时间显示）  
set autotrace off;       

附：相关的set autotrace命令：

SET AUTOTRACE OFF  
No AUTOTRACE report is generated.

SET AUTOTRACE ON EXPLAIN  
shows only the optimizer

execution path.

SET AUTOTRACE ON

STATISTICS  
shows only the SQL

statement execution statistics.

SET AUTOTRACE ON  
includes both the optimizer

execution path and the SQL statement execution statistics.

SET AUTOTRACE TRACEONLY  
Like SET AUTOTRACE ON, but suppresses the printing of the user’s query output, if any.

说明：如果执行了set autotrace on 语句，接下来的查询、插入、更新、删除语句就会显示执行计划和有用的统计信息，直到执行set autotrace off语句。

方法二： EXPLAIN PLAN FOR sql

1，在sqlplus下执行：EXPLAIN PLAN FOR sql语句，执行后会提示已经解释。

2，然后执行如下查询，查询出执行计划

SQL&gt; select * from table(dbms\_xplan.display);

例子：

SQL&gt; explain plan for select * from emp where deptno='20';

Explained.

SQL&gt; select * from table(dbms\_xplan.display);

转自：[http://space.itpub.net/24695024/viewspace-677182](http://space.itpub.net/24695024/viewspace-677182)