USE_NL
use_nl (table1,table2....) 提示指示指定表作为inner table,如果指定的表已经作为了outer table(驱动表),那么优化器会忽略
use_nl 提示,如果非要强制它作为inner table ,可以与ordered 提示一起使用。
下面以SCOTT测试用户作为试验
SQL> select table_name,last_analyzed from user_tables;
TABLE_NAME LAST_ANALYZED
------------------------------ --------------
DEPT 01-12月-09
EMP 01-12月-09
BONUS 01-12月-09
SALGRADE 01-12月-09
表已经分析过了
SQL> select ename,dept.deptno from dept,emp where dept.deptno=emp.deptno;
已选择14行。
执行计划
----------------------------------------------------------
Plan hash value: 3074306753
------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 14 | 168 | 3 (0)| 00:00:01 |
| 1 | NESTED LOOPS | | 14 | 168 | 3 (0)| 00:00:01 |
| 2 | TABLE ACCESS FULL| EMP | 14 | 126 | 3 (0)| 00:00:01 |
|* 3 | INDEX UNIQUE SCAN| PK_DEPT | 1 | 3 | 0 (0)| 00:00:01 |
------------------------------------------------------------------------------
此处优化器选择emp作为驱动表(outer table),因为dept上有索引,而且索引正好建在连接列上,可见优化器选择是正确的
SQL> select /*+ use_nl(emp) */ ename,dept.deptno from dept,emp where dept.deptno=emp.deptno;
已选择14行。
执行计划
----------------------------------------------------------
Plan hash value: 3074306753
------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 14 | 168 | 3 (0)| 00:00:01 |
| 1 | NESTED LOOPS | | 14 | 168 | 3 (0)| 00:00:01 |
| 2 | TABLE ACCESS FULL| EMP | 14 | 126 | 3 (0)| 00:00:01 |
|* 3 | INDEX UNIQUE SCAN| PK_DEPT | 1 | 3 | 0 (0)| 00:00:01 |
------------------------------------------------------------------------------
由于emp作为outer table,优化器忽略use_nl提示
SQL> select /*+ ordered use_nl(emp) */ ename,dept.deptno from dept,emp where dept.deptno=emp.deptno;
已选择14行。
执行计划
----------------------------------------------------------
Plan hash value: 3566768842
------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 14 | 168 | 7 (0)| 00:00:01 |
| 1 | NESTED LOOPS | | 14 | 168 | 7 (0)| 00:00:01 |
| 2 | INDEX FULL SCAN | PK_DEPT | 4 | 12 | 1 (0)| 00:00:01 |
|* 3 | TABLE ACCESS FULL| EMP | 4 | 36 | 2 (0)| 00:00:01 |
------------------------------------------------------------------------------
可见,现在dept作为了驱动表