ORACLE SQL效能優化系列(5)

2021-04-07 00:54:58 字數 1378 閱讀 6977

17. 使用表的別名(alias)

當在sql語句中連線多個表時, 請使用表的別名並把別名字首於每個column上.這樣一來,就可以減少解析的時間並減少那些由column歧義引起的語法錯誤.

(譯者注: column歧義指的是由於sql中不同的表具有相同的column名,當sql語句中出現這個column時,sql解析器無法判斷這個column的歸屬)

18. 用exists替代in

在許多基於基礎表的查詢中,為了滿足乙個條件,往往需要對另乙個表進行聯接.在這種情況下, 使用exists(或not exists)通常將提高查詢的效率.

低效:

select *

from emp (基礎表)

where empno > 0

and deptno in (select deptno

from dept

where loc = 『melb')

高效:

select *

from emp (基礎表)

where empno > 0

and exists (select 『x'

from dept

where dept.deptno = emp.deptno

and loc = 『melb')

(譯者按: 相對來說,用not exists替換not in 將更顯著地提高效率,下一節中將指出)

19. 用not exists替代not in

在子查詢中,not in子句將執行乙個內部的排序和合併. 無論在哪種情況下,not in都是最低效的 (因為它對子查詢中的表執行了乙個全表遍歷). 為了避免使用not in ,我們可以把它改寫成外連線(outer joins)或not exists.

例如:

select …

from emp

where dept_no not in (select dept_no

from dept

where dept_cat='a');

為了提高效率.改寫為:

(方法一: 高效)

select ….

from emp a,dept b

where a.dept_no = b.dept(+)

and b.dept_no is null

and b.dept_cat(+) = 『a'

(方法二: 最高效)

select ….

from emp e

where not exists (select 『x'

from dept d

where d.dept_no = e.dept_no

and dept_cat = 『a');

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...