oracle裡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 |
------------------------------------------------------------------------------
此處優化器選擇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作為了驅動表