方法一:autotrace
1,, connect sys/密碼 as sysdba,,在sys用戶下運行$ORACLE_HOME/sqlplus/admin/plustrce.sql 這段sql的實際內(nèi)容如下: 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 以上產(chǎn)生plustrace角色,然后在sys用戶下把此角色賦予一般用戶 SQL> grant PLUSTRACE to 用戶名; 2,,然后在當(dāng)前用戶SQL>下運行$ORACLE_HOME/rdbms/admin/utlxplan.sql,,它會創(chuàng)建一個plan_table,用來存儲分析SQL語句的結(jié)果,。 實際執(zhí)行的是如下語句: 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; (說明:打開自動分析統(tǒng)計,并顯示SQL語句的運行結(jié)果) set autotrace traceonly; (說明:打開自動分析統(tǒng)計,,不顯示SQL語句的運行結(jié)果) 4,,接下來你就運行需要查看執(zhí)行計劃的SQL語句,看到其分析統(tǒng)計結(jié)果了,。一般來講,,我們的SQL語句應(yīng)該避免對大表的全表掃描。 5,,關(guān)閉以上功能,,在SQL/PLUS的窗口運行以下命令 set time off; (說明:關(guān)閉時間顯示) set autotrace off; 附:相關(guān)的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. 說明:如果執(zhí)行了set autotrace on 語句,,接下來的查詢、插入,、更新,、刪除語句就會顯示執(zhí)行計劃和有用的統(tǒng)計信息,直到執(zhí)行set autotrace off語句,。 方法二: EXPLAIN PLAN FOR sql 1,,在sqlplus下執(zhí)行:EXPLAIN PLAN FOR sql語句,執(zhí)行后會提示已經(jīng)解釋,。 2,,然后執(zhí)行如下查詢,查詢出執(zhí)行計劃 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); 注:除了上面的,,還有一個很有用的函數(shù)dbms_xplan.display_awr(sql_id ,null,null,'ALL');這個可以結(jié)合AWR報告中的SQL語句的SQL_ID來獲得歷史語句的執(zhí)行計劃,,然后通過視圖 dba_hist_sqlbind 找到語句對應(yīng)的綁定變量值。
|
|