在SQL*PLUS中,當你成功的執行一個DML語句,比如
SELECT
,
DELETE
,
UPDATE
,
INSERT,你
可以通過SQL優化器和語句的執行統計自動的獲得一份報告。這份報告對于DML語句的性能監控和調優都是很有用處的。這份報告就是本文要講的AUTOTRACE 報告。
?
配置AUTOTRACE報告(Configuring the AUTOTRACE Report)
你可以通過以下的AUTOTRACE系統變量來配置AUTOTRACE報告. 如下表:
Table? AUTOTRACE Settings
AUTOTRACE Setting
|
Result
|
SET AUTOTRACE OFF
|
不能獲得
AUTOTRACE報告
. 這是默認的.
|
SET AUTOTRACE ON EXPLAIN
|
僅僅顯示優化器執行計劃的
AUTOTRACE
報告
|
SET AUTOTRACE ON STATISTICS
|
僅僅顯示SQL語句執行的統計結果的
AUTOTRACE
報告
|
SET AUTOTRACE ON
|
包括上面兩項內容的AUTOTRACE報告
|
SET AUTOTRACE TRACEONLY
|
與
SET
AUTOTRACE
ON類似
,所有的統計和數據都在,但不可以打印
|
?
其實,平時我們應該較多的就是SET AUTOTRACE ON 、SET AUTOTRACE OFF,誰會在乎多看一點AUTOTRACE報告呢!
J
安裝AUTOTRACE報告(Setups Required for the AUTOTRACE Report)
要用這個特性,用戶必須被賦予PLUSTRACE角色,而PLUSTRACE角色需要DBA來賦予。另外,該用戶必須創建PLAN_TABLE表。
I.????????????????
首先創建PLUSTRACE角色并且賦給DBA:
CONNECT sys/sys’s password AS SYSDBA
@$ORACLE_HOME/sqlplus/admin/plustrce.sql
II.?????????????
賦權限給用戶
CONNECT / AS SYSDBA
GRANT PLUSTRACE TO USER(預賦權的用戶名);
?
這樣,就可以在該用戶下設置AUTOTRACE報告的顯示與否了。
?
AUTOTRACE報告中涉及到的兩個方面的內容
I.?????????????
SQL語句的執行計劃
執行計劃就是SQL優化器執行語句的查詢執行順序,每一行的執行計劃都會有個行號,這個行號是連續的
II.?????????
SQL語句的數據庫統計
數據庫統計是服務器記錄的執行當前的這條語句所需要的系統資源等,主要包括下表的內容
Database Statistic Name
|
Description
|
recursive calls
|
Number of recursive calls generated at both the user and system level. Oracle maintains tables used for internal processing. When Oracle needs to make a change to these tables, it internally generates an internal SQL statement, which in turn generates a recursive call.
|
db block gets
|
Number of times a CURRENT block was requested.
|
consistent gets
|
Number of times a consistent read was requested for a block.
|
physical reads
|
Total number of data blocks read from disk. This number equals the value of "physical reads direct" plus all reads into buffer cache.
|
redo size
|
Total amount of redo generated in bytes.
|
bytes sent via SQL*Net to client
|
Total number of bytes sent to the client from the foreground processes.
|
bytes received via SQL*Net from client
|
Total number of bytes received from the client over Oracle Net.
|
SQL*Net roundtrips to/from client
|
Total number of Oracle Net messages sent to and received from the client.
|
sorts (memory)
|
Number of sort operations that were performed completely in memory and did not require any disk writes.
|
sorts (disk)
|
Number of sort operations that required at least one disk write.
|
rows processed
|
Number of rows processed during the operation.
|
?
簡單使用的例子:
SQL> show user
USER is "SYS"
(注:當前是sysdba用戶)
(注:創建PLUSTRACE角色并且賦給DBA)
SQL> start $ORACLE_HOME/sqlplus/admin/plustrce.sql
(注:start 等價于@)
SQL> drop role plustrace;
drop role plustrace
????????? *
ERROR at line 1:
ORA-01919: role 'PLUSTRACE' does not exist
?
?
SQL> create role plustrace;
?
Role created.
?
SQL>
SQL> grant select on v_$sesstat to plustrace;
?
Grant succeeded.
?
SQL> grant select on v_$statname to plustrace;
?
Grant succeeded.
SQL>
?
SQL> grant select on v_$session to plustrace;
?
Grant succeeded.
?
SQL> grant plustrace to dba with admin option;
?
Grant succeeded.
?
SQL>
SQL> set echo off
(注:賦角色plustrace給所需用戶,這里用戶是bill)
SQL> grant plustrace to bill;
?
Grant succeeded.
(注:接下來,用bill用戶連接,測試AUTOTRACE)
SQL> connect bill/bill’s password;
Connected.
SQL> show user
USER is "BILL"
(注:為了看的清楚些,建立一個測試的表test,然后察看AUTOTRACE報告)
SQL>? create table test(id number(1));
?
Table created.
?
SQL> insert into test values(1);
?
1 row created.
?
SQL> /
?
1 row created.
?
SQL> /
?
1 row created.
?
SQL> /
?
1 row created.
?
SQL> commit;
?
Commit complete.
SQL> select * from test;
?
??????? ID
----------
???????? 1
???????? 1
???????? 1
???????
?1
?
SQL> set AUTOTRACE on
SQL> select * from test;
?
??????? ID
----------
???????? 1
???????? 1
???????? 1
???????? 1
?
Execution Plan
----------------------------------------------------------
?? 0????? SELECT STATEMENT Optimizer=CHOOSE
?? 1??? 0?? TABLE ACCESS (FULL) OF 'TEST'
?
?
?
?
Statistics
----------------------------------------------------------
????????? 0? recursive calls
????????? 0? db block gets
????????? 4? consistent gets
????????? 0? physical reads
????????? 0? redo size
??????? 547? bytes sent via SQL*Net to client
??????? 655? bytes received via SQL*Net from client
????????? 2? SQL*Net roundtrips to/from client
????????? 0? sorts (memory)
????????? 0? sorts (disk)
4????????
rows processed
?
上面我們就可以看到,當你設置了set AUTOTRACE on后,執行相應的DML語句,就會有相應的AUTOTRACE報告出現了。當然,我們的這種設置是session級別的。這樣,我們就可以根據AUTOTRACE報告對我們執行的DML語句進行分析和調優了!
轉自:http://dev.csdn.net/develop/article/23/23430.shtm