在復雜的查詢中
在主要/明細關系表中寫一個SQL的時候
可是
當你用IN子句來寫一個查詢語句的時候
select ename from emp e
where mgr in (select empno from emp where ename =
以下是關於這個查詢的說明計劃:
OBJECT OPERATION
SELECT STATEMENT()
NESTED LOOPS()
EMP TABLE ACCESS(FULL)
EMP TABLE ACCESS(BY INDEX ROWID)
PK_EMP INDEX(UNIQUE SCAN)
這個查詢實際上等同於以下這個:
select e
你可以用EXISTS寫同樣的查詢
select ename from emp e
where exists (select
當你在一個WHERE子句中寫EXISTS時
關於這個查詢的說明計劃如下
OBJECT OPERATION
SELECT STATEMENT()
FILTER()
EMP TABLE ACCESS(FULL)
EMP TABLE ACCESS(BY INDEX ROWID)
PK_EMP INDEX(UNIQUE SCAN)
這實際上與PL/SQL編碼類似
set serveroutput on;
declare
l_count integer;
begin
for e in (select mgr
select count(*) into l_count from emp
where e
if l_count !=
dbms_output
end if;
end loop;
end;
為了確定在基於規則的最優化中
有些人盡量避免使用EXISTS子句
如果你想運行一下你自己的測試
REM
set feedback off
set verify off
set pages
column operation format a
column object format a
TTITLE * STATEMENT_ID =
select object_name object
lpad(
from plan_table
start with id =
connect by prior id = parent_id and statement_id =
REM
REM
REM
REM
alter session set optimizer_goal = rule;
truncate table plan_table;
REM
explain plan set statement_id =
select ename from emp e
where mgr in (select empno from emp where ename =
explain plan set statement_id =
select e
where e
explain plan set statement_id =
select ename from emp e
where exists (select
explain plan set statement_id =
select ename from emp e
where mgr = (select empno from emp where ename =
explain plan set statement_id =
select e
where e
and e
REM
explain plan set statement_id =
select ename from emp e where e
explain plan set statement_id =
select e
where e
@@explain IN
@@explain JOIN
@@explain EXISTS
@@explain =
@@explain JOIN
@@explain >
@@explain JOIN
From:http://tw.wingwit.com/Article/os/youhua/201404/30389.html