DROP TABLE 사원 PURGE;
CREATE TABLE 사원 AS
SELECT 사번
, 성명
, 직급
, 입사일
, 부서코드
, DECODE( 부서코드, 1, 'DB1팀', 2, 'DB2팀', 3, 'DB3팀'
, 4, '시스템1팀', 5, '시스템2팀', 6, '시스템3팀'
, 7, '경리1과', 8, '경리2과', 9, '경리3과', 10, '경리과', 11, '경리4과'
, 12, '개발1팀', 13, '개발2팀', 14, '개발3팀', 15, '개발4팀', 16, '개발5팀', 17, '개발6팀'
, 18, 'MD1팀', 19, 'MD2팀', 20, 'MD3팀', 21, 'MD4팀'
, 22, '디자인1팀', 23, '디자인2팀', 24, '디자인3팀', 25, '디자인4팀'
, 26, '멀티미디어1팀', 27, '멀티미디어2팀', 28, '멀티미디어3팀'
, 29, '모바일1팀'
, 30, '웹모바일팀'
, 31, '마케팅팀'
, 32, '기획팀' ) 부서
FROM (SELECT LEVEL AS 사번
, '아무개'||LEVEL AS 성명
, TRUNC( dbms_random.value( 1,7 ) ) 직급
, TRUNC( SYSDATE ) - 100 + CEIL( LEVEL/ 10000 ) 입사일
, TRUNC( dbms_random.value( 1,32 ) ) 부서코드
FROM DUAL
CONNECT BY LEVEL <= 1000000
)
CREATE UNIQUE INDEX 사원_PK ON 사원 ( 사번 )
--CREATE INDEX 부서_INDEX_01 ON 사원 ( 부서코드, 직급 );
CREATE TABLE 근태 AS
SELECT A.사번
, B.부서코드
, A.근태유형
, A.일자
FROM (SELECT TRUNC( dbms_random.value( 1,1000000 ) ) 사번
, DECODE( A.JOIN_C, 1,'무단결근',2,'조퇴', 3,'지각', 4,'지각', 5,'지각', 6, '연차', 7, '연차', 8,'휴가', 9,'휴가',10, '지각') 근태유형
, A.일자
FROM (SELECT TRUNC( dbms_random.value( 1,10 ) ) JOIN_C --
, TO_CHAR( TO_DATE( '20050101', 'YYYYMMDD') + LEVEL -1, 'YYYYMMDD' ) 일자
FROM DUAL
CONNECT BY LEVEL <= 365--TO_DATE( '20050630', 'YYYYMMDD') - TO_DATE( '20050501', 'YYYYMMDD')
) A
, (SELECT LEVEL LV FROM DUAL CONNECT BY LEVEL <= 6 ) B
WHERE A.JOIN_C >= B.LV
) A
, 사원 B
WHERE A.사번 = B.사번
CREATE INDEX 유형_일자_INX ON 근태 ( 근태유형, 일자 )
SQL> SELECT COUNT(*) FROM 근태
2 ;
COUNT(*)
----------
1574
SQL> SELECT /*+ gather_plan_statistics QB_NAME( MAIN ) LEADING( B@SUB ) UNNEST( @SUB ) USE_MERGE( B@SUB A@MAIN ) */ COUNT(*)
2 FROM 사원 A
3 WHERE 부서코드 IN ( SELECT /*+ QB_NAME( SUB ) */ 부서코드
4 FROM 근태 B
5 WHERE 일자 BETWEEN '20050601' AND '20050612'
6 AND 근태유형 = '지각' )
7 AND 직급 >= 3;
COUNT(*)
----------
365514
SQL> @XPLAN
----------------------------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows | A-Rows | A-Time | Buffers | OMem | 1Mem | Used-Mem |
----------------------------------------------------------------------------------------------------------------------------------
| 1 | SORT AGGREGATE | | 1 | 1 | 1 |00:00:00.43 | 6336 | | | |
| 2 | MERGE JOIN | | 1 | 351K| 365K|00:00:02.44 | 6336 | | | |
| 3 | SORT JOIN | | 1 | 10 | 17 |00:00:00.01 | 3 | 2048 | 2048 | 2048 (0)|
| 4 | SORT UNIQUE | | 1 | 19 | 17 |00:00:00.01 | 3 | 9216 | 9216 | 8192 (0)|
| 5 | TABLE ACCESS BY INDEX ROWID| 근태 | 1 | 19 | 19 |00:00:00.01 | 3 | | | |
|* 6 | INDEX RANGE SCAN | 유형_일자_| 1 | 19 | 19 |00:00:00.01 | 2 | | | |
|* 7 | SORT JOIN | | 17 | 573K| 365K|00:00:01.03 | 6333 | 13M| 1381K| 11M (0)|
|* 8 | TABLE ACCESS FULL | 사원 | 1 | 573K| 666K|00:00:01.33 | 6333 | | | |
----------------------------------------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
6 - access("근태유형"='지각' AND "일자">='20050601' AND "일자"<='20050612')
7 - access("부서코드"="부서코드")
filter("부서코드"="부서코드")
8 - filter("직급">=3)
SQL> SELECT /*+ gather_plan_statistics LEADING( A ) USE_MERGE( B ) */ COUNT(*)
2 FROM 사원 A
3 , (SELECT DISTINCT 부서코드
4 FROM 근태 B
5 WHERE 일자 BETWEEN '20050601' AND '20050612'
6 AND 근태유형 = '지각' ) B
7 WHERE A.부서코드 = B.부서코드
8 AND 직급 >= 3;
COUNT(*)
----------
365514
SQL> @XPLAN
-----------------------------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows | A-Rows | A-Time | Buffers | OMem | 1Mem | Used-Mem |
-----------------------------------------------------------------------------------------------------------------------------------
| 1 | SORT AGGREGATE | | 1 | 1 | 1 |00:00:06.96 | 6336 | | | |
| 2 | MERGE JOIN | | 1 | 351K| 365K|00:00:05.50 | 6336 | | | |
| 3 | SORT JOIN | | 1 | 573K| 666K|00:00:01.72 | 6333 | 13M| 1381K| 11M (0)|
|* 4 | TABLE ACCESS FULL | 사원 | 1 | 573K| 666K|00:00:02.00 | 6333 | | | |
|* 5 | SORT JOIN | | 666K| 19 | 365K|00:00:04.28 | 3 | 73728 | 73728 | |
| 6 | VIEW | | 1 | 19 | 17 |00:00:00.01 | 3 | | | |
| 7 | HASH UNIQUE | | 1 | 19 | 17 |00:00:00.01 | 3 | | | |
| 8 | TABLE ACCESS BY INDEX ROWID| 근태 | 1 | 19 | 19 |00:00:00.01 | 3 | | | |
|* 9 | INDEX RANGE SCAN | 유형_일자_| 1 | 19 | 19 |00:00:00.01 | 2 | | | |
-----------------------------------------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
4 - filter("직급">=3)
5 - access("A"."부서코드"="B"."부서코드")
filter("A"."부서코드"="B"."부서코드")
9 - access("근태유형"='지각' AND "일자">='20050601' AND "일자"<='20050612')
SQL> SELECT /*+ gather_plan_statistics LEADING( A ) USE_MERGE( B ) */ COUNT(*)
2 FROM 사원 A
3 , (SELECT DISTINCT 부서코드
4 FROM 근태 B
5 WHERE 일자 BETWEEN '20050601' AND '20050612'
6 AND 근태유형 = '지각' ) B
7 WHERE A.부서코드 = B.부서코드
8 AND 직급 >= 3;
COUNT(*)
----------
365514
SQL> @XPLAN
-----------------------------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows | A-Rows | A-Time | Buffers | OMem | 1Mem | Used-Mem |
-----------------------------------------------------------------------------------------------------------------------------------
| 1 | SORT AGGREGATE | | 1 | 1 | 1 |00:00:06.92 | 6336 | | | |
| 2 | MERGE JOIN | | 1 | 351K| 365K|00:00:05.42 | 6336 | | | |
| 3 | SORT JOIN | | 1 | 573K| 666K|00:00:01.63 | 6333 | 13M| 1381K| 11M (0)|
|* 4 | TABLE ACCESS FULL | 사원 | 1 | 573K| 666K|00:00:01.33 | 6333 | | | |
|* 5 | SORT JOIN | | 666K| 19 | 365K|00:00:04.33 | 3 | 73728 | 73728 | |
| 6 | VIEW | | 1 | 19 | 17 |00:00:00.01 | 3 | | | |
| 7 | HASH UNIQUE | | 1 | 19 | 17 |00:00:00.01 | 3 | | | |
| 8 | TABLE ACCESS BY INDEX ROWID| 근태 | 1 | 19 | 19 |00:00:00.01 | 3 | | | |
|* 9 | INDEX RANGE SCAN | 유형_일자_| 1 | 19 | 19 |00:00:00.01 | 2 | | | |
-----------------------------------------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
4 - filter("직급">=3)
5 - access("A"."부서코드"="B"."부서코드")
filter("A"."부서코드"="B"."부서코드")
9 - access("근태유형"='지각' AND "일자">='20050601' AND "일자"<='20050612')
SQL> SELECT /*+ gather_plan_statistics LEADING( B ) USE_MERGE( A B ) */ COUNT(*)
2 FROM 사원 A
3 , (SELECT DISTINCT 부서코드
4 FROM 근태 B
5 WHERE 일자 BETWEEN '20050601' AND '20050612'
6 AND 근태유형 = '지각' ) B
7 WHERE A.부서코드 = B.부서코드
8 AND 직급 >= 3;
COUNT(*)
----------
365514
SQL> @XPLAN
-----------------------------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows | A-Rows | A-Time | Buffers | OMem | 1Mem | Used-Mem |
-----------------------------------------------------------------------------------------------------------------------------------
| 1 | SORT AGGREGATE | | 1 | 1 | 1 |00:00:00.41 | 6336 | | | |
| 2 | MERGE JOIN | | 1 | 351K| 365K|00:00:02.44 | 6336 | | | |
| 3 | SORT JOIN | | 1 | 19 | 17 |00:00:00.01 | 3 | 2048 | 2048 | 2048 (0)|
| 4 | VIEW | | 1 | 19 | 17 |00:00:00.01 | 3 | | | |
| 5 | HASH UNIQUE | | 1 | 19 | 17 |00:00:00.01 | 3 | | | |
| 6 | TABLE ACCESS BY INDEX ROWID| 근태 | 1 | 19 | 19 |00:00:00.01 | 3 | | | |
|* 7 | INDEX RANGE SCAN | 유형_일자_| 1 | 19 | 19 |00:00:00.01 | 2 | | | |
|* 8 | SORT JOIN | | 17 | 573K| 365K|00:00:01.02 | 6333 | 13M| 1381K| 11M (0)|
|* 9 | TABLE ACCESS FULL | 사원 | 1 | 573K| 666K|00:00:01.33 | 6333 | | | |
-----------------------------------------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
7 - access("근태유형"='지각' AND "일자">='20050601' AND "일자"<='20050612')
8 - access("A"."부서코드"="B"."부서코드")
filter("A"."부서코드"="B"."부서코드")
9 - filter("직급">=3)
- 강좌 URL : http://www.gurubee.net/lecture/4453
- 구루비 강좌는 개인의 학습용으로만 사용 할 수 있으며, 다른 웹 페이지에 게재할 경우에는 출처를 꼭 밝혀 주시면 고맙겠습니다.~^^
- 구루비 강좌는 서비스 제공을 위한 목적이나, 학원 홍보, 수익을 얻기 위한 용도로 사용 할 수 없습니다.