ORACLE SQL效能優化系列(九)

2021-04-01 20:02:04 字數 2959 閱讀 6490

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效能優化系列(一)

oracle sql效能優化系列(二)

oracle sql效能優化系列(三)

oracle sql效能優化系列(四)

oracle sql效能優化系列(五)

oracle sql效能優化系列(六)

oracle sql效能優化系列(七)

oracle sql效能優化系列(八)

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