ORACLE SQL效能優化系列 七

2021-04-13 06:40:21 字數 1384 閱讀 5379

24. 用explain plan 分析sql語句

explain plan 是乙個很好的分析sql語句的工具,它甚至可以在不執行sql的情況下分析語句. 通過分析,我們就可以知道oracle是怎麼樣連線表,使用什麼方式掃瞄表(索引掃瞄或全表掃瞄)以及使用到的索引名稱.

你需要按照從裡到外,從上到下的次序解讀分析的結果. explain plan分析的結果是用縮排的格式排列的, 最內部的操作將被最先解讀, 如果兩個操作處於同一層中,帶有最小操作號的將被首先執行.

nested loop是少數不按照上述規則處理的操作, 正確的執行路徑是檢查對nested loop提供資料的操作,其中操作號最小的將被最先處理.

譯者按:

通過實踐, 感到還是用sqlplus中的set trace 功能比較方便.

舉例:sql> list

1 select *

2 from dept, emp

3* where emp.deptno = dept.deptno

sql> set autotrace traceonly /*traceonly 可以不顯示執行結果*/

sql> /

14 rows selected.

execution plan

0 select statement optimizer=choose

1 0 nested loops

2 1 table access (full) of 'emp'

3 1 table access (by index rowid) of 'dept'

4 3 index (unique scan) of 'pk_dept' (unique)

statistics

0 recursive calls

2 db block gets

30 consistent gets

0 physical reads

0 redo size

2598 bytes sent via sql*net to client

503 bytes received via sql*net from client

2 sql*net roundtrips to/from client

0 sorts (memory)

0 sorts (disk)

14 rows processed

通過以上分析,可以得出實際的執行步驟是:

1. table access (full) of 'emp'

2. index (unique scan) of 'pk_dept' (unique)

3. table access (by index rowid) of 'dept'

4. nested loops (joining 1 and 3)

ORACLE SQL效能優化系列

1.選用適合的 oracle 優化器 oracle 的優化器共有3種 a.rule 基於規則 b.cost 基於成本 c.choose 選擇性 設定預設的優化器 可以通過對 init.ora 檔案中optimizer mode 引數的各種宣告,如 rule,cost,choose,all rows,...

ORACLE SQL效能優化系列

1.選用適合的oracle優化器 oracle的優化器共有3種 a.rule 基於規則 b.cost 基於成本 c.choose 選擇性 設定預設的優化器,可以通過對init.ora檔案中optimizer mode引數的各種宣告,如rule,cost,choose,all rows,first r...

Oracle SQL效能優化系列

1.選用適合的oracle優化器 oracle的優化器共有3種 a.rule 基於規則 b.cost 基於成本 c.choose 選擇性 設定預設的優化器,可以通過對init.ora檔案中optimizer mode引數的各種宣告,如rule,cost,choose,all rows,first r...