filter下挂两个节点:
挂两个节点时,emp扫描了一次有14条记录,类似于nested loops, 而扫描如果是nested loop的话,则应该是14次,但是此处dept是3次。这是因为filter维护了一个hash table,如果有重复的记录可以减少扫描次数。此处由于emp中的deptno只有三个值,所以只扫描3次。
SQL> select /*+rule*/count(*) from emp
where not exists (select 1
from dept
where dept.dname = 'SALE'
and dept.deptno = emp.deptno);
COUNT(*)
----------
14
SQL> select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));
PLAN_TABLE_OUTPUT
---------------------------------------------------------------------------------------------------
SQL_ID 0wjfqsm43j4z5, child number 0
-------------------------------------
select /*+rule*/count(*) from emp where not exists (select 1 from dept
where dept.dname = 'SALE' and dept.deptno = emp.deptno)
Plan hash value: 3536434453
------------------------------------------------------------------------------------------
| Id | Operation | Name | Starts | A-Rows | A-Time | Buffers |
------------------------------------------------------------------------------------------
| 1 | SORT AGGREGATE | | 1 | 1 |00:00:00.01 | 13 |
|* 2 | FILTER | | 1 | 14 |00:00:00.01 | 13 |
| 3 | TABLE ACCESS FULL | EMP | 1| 14 |00:00:00.01 | 7 |
|* 4 | TABLE ACCESS BY INDEX ROWID| DEPT | 3 | 0 |00:00:00.01 | 6 |
|* 5 | INDEX UNIQUE SCAN | PK_DEPT | 3 | 3 |00:00:00.01 | 3 |
------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - filter( IS NULL)
4 - filter("DEPT"."DNAME"='SALE')
5 - access("DEPT"."DEPTNO"=:B1)
filter下挂三个节点:
挂三个节点时,每当emp返回一条数据,都会驱动dept和bouns分别执行一次(注意的是在执行连接时每次只能两个表进行连接),由于filter使用了缓存技术,所以看到的dept只是扫描了3次,bonus扫描了14次。
SQL> select /*+rule*/count(*) from emp
where not exists (select 1
from dept
where dept.dname = 'SALE'
and dept.deptno = emp.deptno)
and not exists (select 1 from bonus where bonus.ename = emp.ename);
COUNT(*)
----------
14
SQL> select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));
PLAN_TABLE_OUTPUT
-------------------------------------
SQL_ID 2wss1r100vqd9, child number 0
-------------------------------------
select /*+rule*/* from emp where not exists (select 1 from dept
where dept.dname = 'SALE' and dept.deptno = emp.deptno) and not
exists (select 1 from bonus where bonus.ename = emp.ename)
Plan hash value: 1445856646
-----------------------------------------------------------------------------------------
| Id | Operation | Name | Starts | A-Rows | A-Time | Buffers |
-----------------------------------------------------------------------------------------
|* 1 | FILTER | | 1 | 14 |00:00:00.01 | 56 |
| 2 | TABLE ACCESS FULL | EMP | 1 | 14 |00:00:00.01 | 8 |
|* 3 | TABLE ACCESS BY INDEX ROWID| DEPT | 3 | 0 |00:00:00.01 | 6 |
|* 4 | INDEX UNIQUE SCAN | PK_DEPT | 3 | 3 |00:00:00.01 | 3 |
|* 5 | TABLE ACCESS FULL | BONUS | 14 | 0 |00:00:00.01 | 42 |
-----------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter(( IS NULL AND IS NULL))
3 - filter("DEPT"."DNAME"='SALE')
4 - access("DEPT"."DEPTNO"=:B1)
5 - filter("BONUS"."ENAME"=:B1)
SQL> select count(*) from emp
where not exists (select 1
from dept
where dept.dname = 'SALE'
and dept.deptno = emp.deptno);
COUNT(*)
----------
14
更多Oracle相关信息见Oracle 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=12
