PUSH_SUBQ 可以用来控制子查询的执行 这个是PUSH_SUBQ 的本意
我那个例子的意思是说:
PUSH_SUBQ 本质上是个CBO的hints(当然RBO也提不上hints)
由于PUSH_SUBQ 的引入就是为了来解决unnesting的某些不足
所以在不同的版本上,这个hints发挥的作用也有所不同了.
8i上这个提示的作用更接近本原:
$ sqlplus "/ as sysdba"
SQL*Plus: Release 8.1.5.0.0 - ProdUCtion on Sun Sep 12 20:51:21 2004
(c) Copyright 1999 Oracle Corporation.All rights reserved.
Connected to:
Oracle8i Enterprise Edition Release 8.1.5.0.0 - Production
With the Partitioning and Java options
PL/SQL Release 8.1.5.0.0 - Production
SQL connect scott/tiger
Connected.
SQL create table dept1 as select * from dept;
Table created.
SQL set linesize 120
SQL select a.*
2from emp a,dept b where a.deptno = b.deptno and exists (select 1 from dept1 where dept1.dname = b.dname);
EMPNO ENAMEJOBMGR HIREDATE SAL COMM DEPTNO
----- ---------- --------- ---------- --------- ---------- ---------- ----------
7369SMITHCLERK 7902 17-DEC-8080020
7499ALLENSALESMAN7698 20-FEB-81 1600300 30
7521WARD SALESMAN7698 22-FEB-81 1250500 30
7566JONESMANAGER 7839 02-APR-81 297520
7654MARTIN SALESMAN7698 28-SEP-81 1250 1400 30
7698BLAKEMANAGER 7839 01-MAY-81 285030
7782CLARKMANAGER 7839 09-JUN-81 245010
7788SCOTTANALYST 7566 19-APR-87 300020
7839KING PRESIDENT17-NOV-81 500010
7844TURNER SALESMAN7698 08-SEP-81 15000 30
7876ADAMSCLERK 7788 23-MAY-87 110020
EMPNO ENAMEJOBMGR HIREDATE SAL COMM DEPTNO
----- ---------- --------- ---------- --------- ---------- ---------- ----------
7900JAMESCLERK 7698 03-DEC-8195030
7902FORD ANALYST 7566 03-DEC-81 300020
7934MILLER CLERK 7782 23-JAN-82 130010
14 rows selected.
Execution Plan
----------------------------------------------------------
0SELECT STATEMENT Optimizer=CHOOSE
10 FILTER ---------------'最初push_subq的使命是为了消除/提高这个filter的效率的'
21 NESTED LOOPS
32 TABLE Access (FULL) OF 'EMP'
42 TABLE ACCESS (BY INDEX ROWID) OF 'DEPT'
54 INDEX (UNIQUE SCAN) OF 'PK_DEPT' (UNIQUE)
61 TABLE ACCESS (FULL) OF 'DEPT1'
Statistics
----------------------------------------------------------
0recursive calls
52db block gets
21consistent gets
0physical reads
0redo size
2715bytes sent via SQL*Net to client
751bytes received via SQL*Net from client
4SQL*Net roundtrips to/from client
1sorts (memory)
0sorts (disk)
14rows processed
用于push_subq是个CBO hints,这里我们可以看到COST的出现:
SQL select /*+ push_subq */ a.*
2from emp a,dept b where a.deptno = b.deptno and exists (select 1 from dept1 where dept1.dname = b.dname);
EMPNO ENAMEJOBMGR HIREDATE SAL COMM DEPTNO
----- ---------- --------- ---------- --------- ---------- ---------- ----------
7782CLARKMANAGER 7839 09-JUN-81 245010
7839KING PRESIDENT17-NOV-81 500010
7934MILLER CLERK 7782 23-JAN-82 130010
7369SMITHCLERK 7902 17-DEC-8080020
7566JONESMANAGER 7839 02-APR-81 297520
7788SCOTTANALYST 7566 19-APR-87 300020
7876ADAMSCLERK 7788 23-MAY-87 110020
7902FORD ANALYST 7566 03-DEC-81 300020
7499ALLENSALESMAN7698 20-FEB-81 1600300 30
7521WARD SALESMAN7698 22-FEB-81 1250500 30
7654MARTIN SALESMAN7698 28-SEP-81 1250 1400 30
EMPNO ENAMEJOBMGR HIREDATE SAL COMM DEPTNO
------ ---------- --------- ---------- --------- ---------- ---------- ----------
7698 BLAKEMANAGER 7839 01-MAY-81 285030
7844 TURNER SALESMAN7698 08-SEP-81 15000 30
7900 JAMESCLERK 7698 03-DEC-8195030
14 rows selected.
Execution Plan
----------------------------------------------------------
0SELECT STATEMENT Optimizer=CHOOSE (Cost=3 Card=1 Bytes=122)
10 NESTED LOOPS (Cost=3 Card=1 Bytes=122)
21 TABLE ACCESS (FULL) OF 'DEPT' (Cost=1 Card=2 Bytes=44)
32 TABLE ACCESS (FULL) OF 'DEPT1' (Cost=1 Card=1 Bytes=9)--------'这里消除了之前的filter'
41 TABLE ACCESS (FULL) OF 'EMP' (Cost=1 Card=21 Bytes=2100)
Statistics
----------------------------------------------------------
204recursive calls
85db block gets
38consistent gets
0physical reads
0redo size
2706bytes sent via SQL*Net to client
768bytes received via SQL*Net from client
4SQL*Net roundtrips to/from client
5sorts (memory)
0sorts (disk)
14rows processed
再看CBO下:
正常情况下,没什么好说的:
SQL set autotrace traceonly
SQL exec dbms_stats.gather_schema_stats('SCOTT')
PL/SQL procedure successfully completed.
SQL select a.*
2from emp a,dept b where a.deptno = b.deptno and exists (select 1 from dept1 where dept1.dname = b.dname);
14 rows selected.
Execution Plan
----------------------------------------------------------
0SELECT STATEMENT Optimizer=CHOOSE (Cost=2 Card=5 Bytes=275)
10 FILTER
21 NESTED LOOPS (Cost=2 Card=5 Bytes=275)
32 TABLE ACCESS (FULL) OF 'DEPT' (Cost=1 Card=1 Bytes=18)
42 TABLE ACCESS (FULL) OF 'EMP' (Cost=1 Card=14 Bytes=518)
51 TABLE ACCESS (FULL) OF 'DEPT1' (Cost=1 Card=1 Bytes=9)
Statistics
----------------------------------------------------------
39recursive calls
68db block gets
21consistent gets
0physical reads
0redo size
2708bytes sent via SQL*Net to client
751bytes received via SQL*Net from client
4SQL*Net roundtrips to/from client
1sorts (memory)
0sorts (disk)
14rows processed
在CBO下push_subq发挥了同样的作用:
SQL select /*+ push_subq */ a.*
2from emp a,dept b where a.deptno = b.deptno and exists (select 1 from dept1 where dept1.dname = b.dname);
14 rows selected.
Execution Plan
----------------------------------------------------------
0SELECT STATEMENT Optimizer=CHOOSE (Cost=2 Card=5 Bytes=275)
10 NESTED LOOPS (Cost=2 Card=5 Bytes=275)
21 TABLE ACCESS (FULL) OF 'DEPT' (Cost=1 Card=1 Bytes=18)
32 TABLE ACCESS (FULL) OF 'DEPT1' (Cost=1 Card=1 Bytes=9)---'注重这里'
41 TABLE ACCESS (FULL) OF 'EMP' (Cost=1 Card=14 Bytes=518)
Statistics
----------------------------------------------------------
0recursive calls
84db block gets
11consistent gets
0physical reads
0redo size
2709bytes sent via SQL*Net to client
768bytes received via SQL*Net from client
4SQL*Net roundtrips to/from client
1sorts (memory)
0sorts (disk)
14rows processed
而在Oracle9i之中:
[oracle@jumper oracle]$ sqlplus "/ as sysdba"
SQL*Plus: Release 9.2.0.3.0 - Production on Sun Sep 12 21:42:57 2004
Copyright (c) 1982, 2002, Oracle Corporation.All rights reserved.
Connected to:
Oracle9i Enterprise Edition Release 9.2.0.3.0 - Production
With the Partitioning, OLAP and Oracle Data Mining options
JServer Release 9.2.0.3.0 - Production
SQL connect scott/tiger
Connected.
SQL set linesize 120
SQL set autotrace traceonly
SQL select a.*
2from emp a,dept b where a.deptno = b.deptno and exists (select 1 from dept1 where dept1.dname = b.dname);
11 rows selected.
Execution Plan
----------------------------------------------------------
0SELECT STATEMENT Optimizer=CHOOSE
10 FILTER
21 NESTED LOOPS
32 TABLE ACCESS (FULL) OF 'EMP'
42 TABLE ACCESS (BY INDEX ROWID) OF 'DEPT'
54 INDEX (UNIQUE SCAN) OF 'PK_DEPT' (UNIQUE)
61 TABLE ACCESS (FULL) OF 'DEPT1'
Statistics
----------------------------------------------------------
0recursive calls
0db block gets
26consistent gets
0physical reads
0redo size
1164bytes sent via SQL*Net to client
503bytes received via SQL*Net from client
2SQL*Net roundtrips to/from client
0sorts (memory)
0sorts (disk)
11rows processed
SQL select /*+ push_subq */ a.*
2from emp a,dept b where a.deptno = b.deptno and exists (select 1 from dept1 where dept1.dname = b.dname);
11 rows selected.
Execution Plan
----------------------------------------------------------
0SELECT STATEMENT Optimizer=CHOOSE (Cost=8 Card=82 Bytes=9676)
10 HASH JOIN (SEMI) (Cost=8 Card=82 Bytes=9676)
21 HASH JOIN (Cost=5 Card=82 Bytes=8938)
32 TABLE ACCESS (FULL) OF 'EMP' (Cost=2 Card=82 Bytes=7134)
42 TABLE ACCESS (FULL) OF 'DEPT' (Cost=2 Card=82 Bytes=1804)
51 TABLE ACCESS (FULL) OF 'DEPT1' (Cost=2 Card=82 Bytes=738)
Statistics
----------------------------------------------------------
0recursive calls
0db block gets
10consistent gets
0physical reads
0redo size
1195bytes sent via SQL*Net to client
503bytes received via SQL*Net from client
2SQL*Net roundtrips to/from client
0sorts (memory)
0sorts (disk)
11rows processed
SQL exec dbms_stats.gather_schema_stats('scott')
PL/SQL procedure successfully completed.
SQL select a.*
2from emp a,dept b where a.deptno = b.deptno and exists (select 1 from dept1 where dept1.dname = b.dname);
11 rows selected.
Execution Plan
----------------------------------------------------------
0SELECT STATEMENT Optimizer=CHOOSE (Cost=8 Card=11 Bytes=682)
10 HASH JOIN (SEMI) (Cost=8 Card=11 Bytes=682)
21 HASH JOIN (Cost=5 Card=11 Bytes=572)
32 TABLE ACCESS (FULL) OF 'DEPT' (Cost=2 Card=4 Bytes=52)
42 TABLE ACCESS (FULL) OF 'EMP' (Cost=2 Card=11 Bytes=429)
51 TABLE ACCESS (FULL) OF 'DEPT1' (Cost=2 Card=4 Bytes=40)
Statistics
----------------------------------------------------------
172recursive calls
0db block gets
60consistent gets
0physical reads
0redo size
1196bytes sent via SQL*Net to client
503bytes received via SQL*Net from client
2SQL*Net roundtrips to/from client
6sorts (memory)
0sorts (disk)
11rows processed
SQL select /*+ push_subq */ a.*
2from emp a,dept b where a.deptno = b.deptno and exists (select 1 from dept1 where dept1.dname = b.dname);
11 rows selected.
Execution Plan
----------------------------------------------------------
0SELECT STATEMENT Optimizer=CHOOSE (Cost=8 Card=11 Bytes=682)
10 HASH JOIN (SEMI) (Cost=8 Card=11 Bytes=682)
21 HASH JOIN (Cost=5 Card=11 Bytes=572)
32 TABLE ACCESS (FULL) OF 'DEPT' (Cost=2 Card=4 Bytes=52)
42 TABLE ACCESS (FULL) OF 'EMP' (Cost=2 Card=11 Bytes=429)
51 TABLE ACCESS (FULL) OF 'DEPT1' (Cost=2 Card=4 Bytes=40)
Statistics
----------------------------------------------------------
0recursive calls
0db block gets
10consistent gets
0physical reads
0redo size
1196bytes sent via SQL*Net to client
503bytes received via SQL*Net from client
2SQL*Net roundtrips to/from client
0sorts (memory)
0sorts (disk)
11rows processed
SQL
可以肯定的是push_subq从8i到9i的作用发生了变化
这个变化可能来自于CBO的更加优化