如何使用SQLPLUS分析SQL語句(查詢執行計划跟蹤)


方法一: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  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> grant PLUSTRACE to 用戶名;

2,然后在當前用戶SQL>下運行$ORACLE_HOME/rdbms/admin/utl xplan.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> select * from table(dbms_xplan.display);

例子:

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

Explained.

SQL> select * from table(dbms_xplan.display);
 
注:除了上面的,還有一個很有用的函數dbms_xplan.display_awr(sql_id ,null,null,'ALL');這個可以結合AWR報告中的SQL語句的SQL_ID來獲得歷史語句的執行計划,然后通過視圖 dba_hist_sqlbind 找到語句對應的綁定變量值。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM