ORDERED是针对多个目标表的Hint,它的含义是让优化器对多个目标表执行表连接操作时,执照它们在目标SQL的where条件中出现的顺序从左到右依次进行连接。
格式如下:
/*+ ORDERED */
使用范例:
select /*+ ordered */ e.ename,j.job,e.sal,d.deptno
from emp e,jobs j,dept d
where e.empno=j.empno
and e.deptno=d.deptno
and d.loc='CHICAGO'
order by e.ename;
实例:
scott@TEST>select e.ename,j.job,e.sal,d.deptno
2 from emp e,jobs j,dept d
3 where e.empno=j.empno
4 and e.deptno=d.deptno
5 and d.loc='CHICAGO'
6 order by e.ename;
6 rows selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 4113290228
-----------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-----------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 5 | 235 | 9 (23)| 00:00:01 |
| 1 | SORT ORDER BY | | 5 | 235 | 9 (23)| 00:00:01 |
|* 2 | HASH JOIN | | 5 | 235 | 8 (13)| 00:00:01 |
| 3 | NESTED LOOPS | | | | | |
| 4 | NESTED LOOPS | | 5 | 140 | 4 (0)| 00:00:01 |
|* 5 | TABLE ACCESS FULL | DEPT | 1 | 11 | 3 (0)| 00:00:01 |
|* 6 | INDEX RANGE SCAN | IDX_EMP_DEPT | 5 | | 0 (0)| 00:00:01 |
| 7 | TABLE ACCESS BY INDEX ROWID| EMP | 5 | 85 | 1 (0)| 00:00:01 |
| 8 | TABLE ACCESS FULL | JOBS | 14 | 266 | 3 (0)| 00:00:01 |
-----------------------------------------------------------------------------------------------
......
scott@TEST>select /*+ ordered */ e.ename,j.job,e.sal,d.deptno
2 from emp e,jobs j,dept d
3 where e.empno=j.empno
4 and e.deptno=d.deptno
5 and d.loc='CHICAGO'
6 order by e.ename;
6 rows selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 3031293267
-----------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-----------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 5 | 235 | 11 (28)| 00:00:01 |
| 1 | SORT ORDER BY | | 5 | 235 | 11 (28)| 00:00:01 |
|* 2 | HASH JOIN | | 5 | 235 | 10 (20)| 00:00:01 |
| 3 | MERGE JOIN | | 14 | 504 | 6 (17)| 00:00:01 |
| 4 | TABLE ACCESS BY INDEX ROWID| EMP | 14 | 238 | 2 (0)| 00:00:01 |
| 5 | INDEX FULL SCAN | PK_EMP | 14 | | 1 (0)| 00:00:01 |
|* 6 | SORT JOIN | | 14 | 266 | 4 (25)| 00:00:01 |
| 7 | TABLE ACCESS FULL | JOBS | 14 | 266 | 3 (0)| 00:00:01 |
|* 8 | TABLE ACCESS FULL | DEPT | 1 | 11 | 3 (0)| 00:00:01 |
-----------------------------------------------------------------------------------------
......
scott@TEST>select /*+ ordered */ e.ename,j.job,e.sal,d.deptno
2 from emp e,dept d,jobs j
3 where e.empno=j.empno
4 and e.deptno=d.deptno
5 and d.loc='CHICAGO'
6 order by e.ename;
6 rows selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 1175157407
-----------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-----------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 5 | 235 | 11 (28)| 00:00:01 |
| 1 | SORT ORDER BY | | 5 | 235 | 11 (28)| 00:00:01 |
|* 2 | HASH JOIN | | 5 | 235 | 10 (20)| 00:00:01 |
| 3 | MERGE JOIN | | 5 | 140 | 6 (17)| 00:00:01 |
| 4 | TABLE ACCESS BY INDEX ROWID| EMP | 14 | 238 | 2 (0)| 00:00:01 |
| 5 | INDEX FULL SCAN | IDX_EMP_DEPT | 14 | | 1 (0)| 00:00:01 |
|* 6 | SORT JOIN | | 1 | 11 | 4 (25)| 00:00:01 |
|* 7 | TABLE ACCESS FULL | DEPT | 1 | 11 | 3 (0)| 00:00:01 |
| 8 | TABLE ACCESS FULL | JOBS | 14 | 266 | 3 (0)| 00:00:01 |
-----------------------------------------------------------------------------------------------
......