Oracle SQL 執行計畫和分析小結

來源:互聯網
上載者:User

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提供依據,以及效果的對比。

在CentOS 6.4下安裝Oracle 11gR2(x64)

Oracle 11gR2 在VMWare虛擬機器中安裝步驟

Debian 下 安裝 Oracle 11g XE R2

Oracle SQL引擎體繫結構圖

Oracle SQL基礎 

Oracle 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

更多詳情見請繼續閱讀下一頁的精彩內容:

  • 1
  • 2
  • 下一頁

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.