for(i=0; i<100; i++){ -- outer loop
for(j=0; j<100; j++){ -- inner loop
.....
}
}
for outer in 1..100 loop
for inner in 1..100 loop
dbms_output.put_line(outer || ':' || inner);
end loop;
end loop;
begin
for outer in (select deptno, empno, rpad(ename, 10) ename from emp)
loop -- outer 루프
for inner in (select dname from dept where deptno = outer.deptno)
loop -- inner 루프
dbms_output.put_line(outer.empno||' : '||outer.ename||' : '||inner.dname);
end loop;
end loop;
end;
select /*+ ordered use_nl(d) */ e.empno, e.ename, d.dname
from emp e, dept d
where d.deptno = e.deptno
select /*+ ordered use_nl(e) */ *
from dept d, emp e
where e.deptno = d.deptno
Outer테이블
SQL> explain plan for
2 select /*+ ordered use_nl(e)*/*
3 from dept d, emp e
4 where d.deptno = e.deptno;
해석되었습니다.
SQL> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
Plan hash value: 4192419542
---------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 14 | 798 | 9 (0)| 00:00:01 |
| 1 | NESTED LOOPS | | 14 | 798 | 9 (0)| 00:00:01 |
| 2 | TABLE ACCESS FULL| DEPT | 4 | 80 | 3 (0)| 00:00:01 | => Outer/Driving
|* 3 | TABLE ACCESS FULL| EMP | 4 | 148 | 2 (0)| 00:00:01 | => Inner/Driven
---------------------------------------------------------------------------
SQL> explain plan for
2 select /*+ ordered full(d) use_merge(e)*/ *
3 from dept d, emp e
4 where d.deptno = e.deptno;
해석되었습니다.
SQL> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
Plan hash value: 1407029907
----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 14 | 798 | 8 (25)| 00:00:01 |
| 1 | MERGE JOIN | | 14 | 798 | 8 (25)| 00:00:01 |
| 2 | SORT JOIN | | 4 | 80 | 4 (25)| 00:00:01 |
| 3 | TABLE ACCESS FULL| DEPT | 4 | 80 | 3 (0)| 00:00:01 | => Outer/First
|* 4 | SORT JOIN | | 14 | 518 | 4 (25)| 00:00:01 |
| 5 | TABLE ACCESS FULL| EMP | 14 | 518 | 3 (0)| 00:00:01 | => Inner/Second
SQL> explain plan for
2 select /*+ ordered use_hash(e)*/*
3 from dept d, emp e
4 where d.deptno = e.deptno;
해석되었습니다.
SQL> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
Plan hash value: 615168685
---------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 14 | 798 | 7 (15)| 00:00:01 |
|* 1 | HASH JOIN | | 14 | 798 | 7 (15)| 00:00:01 |
| 2 | TABLE ACCESS FULL| DEPT | 4 | 80 | 3 (0)| 00:00:01 | => Outer/Build Input
| 3 | TABLE ACCESS FULL| EMP | 14 | 518 | 3 (0)| 00:00:01 | => Inner/Probe Input
---------------------------------------------------------------------------
NL 조인 | 소트머지 조인 | 해시 조인 | |
---|---|---|---|
실행계획상 위쪽 | Outer(Driving)테이블 | Outer(First)테이블 | Build Input |
실행계획상 아래쪽 | Inner(Driven)테이블 | Inner(Second)테이블 | Probe Input |
select /*+ ordered use_nl(B) use_nl(C) use_hash(D) */ *
from A, B, C, D
where ...
select /*+ leading(C, A, D, B) use_nl(A) use_nl(D) use_hash(B) */ *
from A, B, C, D
where ...
select /*+ use_nl(A,B,C,D)*/ *
from A, B, C, D
where ...
select /*+ ordered use_nl(e) */
e.empno, e.ename, d.dname, e.job, e.sal
from dept d, emp e
where e.deptno = d.deptno ...........(1)
and d.loc = 'SEOUL' ...........(2)
and d.gb = '2' ...........(3)
and e.sal >= 1500 ...........(4)
order by sal desc
pk_dept : dept.deptno
dept_loc_idx : dept.loc
pk_emp : emp.empno
emp_deptno_idx : emp.deptno
emp_sal_idx : emp.sal
-------------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 45 | 4 (25)| 00:00:01 |
| 1 | SORT ORDER BY | | 1 | 45 | 4 (25)| 00:00:01 |
|* 2 | TABLE ACCESS BY INDEX ROWID | EMP | 4 | 100 | 1 (0)| 00:00:01 |
| 3 | NESTED LOOPS | | 1 | 45 | 3 (0)| 00:00:01 |
|* 4 | TABLE ACCESS BY INDEX ROWID| DEPT | 1 | 20 | 2 (0)| 00:00:01 |
|* 5 | INDEX RANGE SCAN | DEPT_LOC_IDX | 1 | | 1 (0)| 00:00:01 |
|* 6 | INDEX RANGE SCAN | EMP_DEPTNO_IDX | 5 | | 0 (0)| 00:00:01 |
-------------------------------------------------------------------------------------------------
select /*+ ordered use_nl(d) */ count(e.ename), count(d.dname)
from scott.t_emp e, scott.dept d
where d.deptno = e.deptno
call count cpu elapsed disk query current rows
------- ------ -------- ---------- ---------- ---------- ---------- ----------
Parse 1 0.03 0.04 0 1 0 0
Execute 1 0.00 0.00 0 0 0 0
Fetch 2 38.75 45.39 8730 1409213 0 1
------- ------ -------- ---------- ---------- ---------- ---------- ----------
total 4 38.78 45.43 8730 1409214 0 1
Misses in library cache during parse: 1
Optimizer mode: ALL_ROWS
Parsing user id: SYS
Rows Row Source Operation
------- ---------------------------------------------------
1 SORT AGGREGATE (cr=1409213 pr=8730 pw=0 time=45394912 us)
1400000 NESTED LOOPS (cr=1409213 pr=8730 pw=0 time=53215534 us)
1400000 TABLE ACCESS FULL T_EMP (cr=9211 pr=8730 pw=0 time=9815520 us)
1400000 TABLE ACCESS BY INDEX ROWID DEPT (cr=1400002 pr=0 pw=0 time=32299603 us)
1400000 INDEX UNIQUE SCAN PK_DEPT (cr=2 pr=0 pw=0 time=12912986 us)(object id 51250)
- 강좌 URL : http://www.gurubee.net/lecture/3266
- 구루비 강좌는 개인의 학습용으로만 사용 할 수 있으며, 다른 웹 페이지에 게재할 경우에는 출처를 꼭 밝혀 주시면 고맙겠습니다.~^^
- 구루비 강좌는 서비스 제공을 위한 목적이나, 학원 홍보, 수익을 얻기 위한 용도로 사용 할 수 없습니다.