새로쓴 대용량 데이터베이스솔루션 1 (2011년)
Hints for Join Operations 0 0 83,533

by 구루비스터디 Hint 힌트 USE_NL USE_NL_WITH_INDEX USE_MERGE USE_HASH [2023.10.13]


Hints for Join Operations


USE_NL

SELECT /*+ USE_NL(T1 T2) */ *
  FROM T1, T2
 WHERE T1.N4 = T2.N4
   AND T1.N2 = 10
   AND T2.N2 = 10;

--------------------------------------------------------------------------------------
| Id  | Operation                     | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT              |      |    10 |   280 |     4   (0)| 00:00:01 |
|   1 |  NESTED LOOPS                 |      |       |       |            |          |
|   2 |   NESTED LOOPS                |      |    10 |   280 |     4   (0)| 00:00:01 |
|   3 |    TABLE ACCESS BY INDEX ROWID| T1   |    10 |   140 |     1   (0)| 00:00:01 |
|*  4 |     INDEX RANGE SCAN          | T1N2 |    10 |       |     1   (0)| 00:00:01 |
|*  5 |    INDEX RANGE SCAN           | T2N2 |    10 |       |     1   (0)| 00:00:01 |
|*  6 |   TABLE ACCESS BY INDEX ROWID | T2   |     1 |    14 |     1   (0)| 00:00:01 |
--------------------------------------------------------------------------------------


NO_USE_NL

SELECT /*+ NO_USE_NL(T1 T2) */ *
  FROM T1, T2
 WHERE T1.N4 = T2.N4
   AND T1.N2 = 10
   AND T2.N2 = 10;

-------------------------------------------------------------------------------------
| Id  | Operation                    | Name | Rows  | Bytes | Cost (%CPU)| Time     |
-------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT             |      |    10 |   280 |     3  (34)| 00:00:01 |
|*  1 |  HASH JOIN                   |      |    10 |   280 |     3  (34)| 00:00:01 |
|   2 |   TABLE ACCESS BY INDEX ROWID| T1   |    10 |   140 |     1   (0)| 00:00:01 |
|*  3 |    INDEX RANGE SCAN          | T1N2 |    10 |       |     1   (0)| 00:00:01 |
|   4 |   TABLE ACCESS BY INDEX ROWID| T2   |    10 |   140 |     1   (0)| 00:00:01 |
|*  5 |    INDEX RANGE SCAN          | T2N2 |    10 |       |     1   (0)| 00:00:01 |
-------------------------------------------------------------------------------------

-- 조인 순서 별도 지정 필요 (LEADING)


USE_NL_WITH_INDEX

SELECT /*+ USE_NL_WITH_INDEX(T1 T1N2) */ *
  FROM T1, T2
 WHERE T1.N4 = T2.N4
   AND T1.N2 = 0;

--------------------------------------------------------------------------------------
| Id  | Operation                     | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT              |      |    17 |   476 |     2   (0)| 00:00:01 |
|   1 |  NESTED LOOPS                 |      |       |       |            |          |
|   2 |   NESTED LOOPS                |      |    17 |   476 |     2   (0)| 00:00:01 |
|   3 |    TABLE ACCESS BY INDEX ROWID| T1   |    10 |   140 |     1   (0)| 00:00:01 |
|*  4 |     INDEX RANGE SCAN          | T1N2 |    10 |       |     1   (0)| 00:00:01 |
|*  5 |    INDEX RANGE SCAN           | T2N4 |    11 |       |     1   (0)| 00:00:01 |
|   6 |   TABLE ACCESS BY INDEX ROWID | T2   |    11 |   154 |     1   (0)| 00:00:01 |
--------------------------------------------------------------------------------------

SELECT /*+ USE_NL_WITH_INDEX(T1 T1N3) */ *
  FROM T1, T2
 WHERE T1.N4 = T2.N4
   AND T1.N3 = 0;

--------------------------------------------------------------------------------------
| Id  | Operation                     | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT              |      |    14 |   392 |     2   (0)| 00:00:01 |
|   1 |  NESTED LOOPS                 |      |       |       |            |          |
|   2 |   NESTED LOOPS                |      |    14 |   392 |     2   (0)| 00:00:01 |
|   3 |    TABLE ACCESS BY INDEX ROWID| T1   |    55 |   770 |     1   (0)| 00:00:01 |
|*  4 |     INDEX RANGE SCAN          | T1N3 |   100 |       |     1   (0)| 00:00:01 |
|*  5 |    INDEX RANGE SCAN           | T2N4 |    11 |       |     1   (0)| 00:00:01 |
|   6 |   TABLE ACCESS BY INDEX ROWID | T2   |    11 |   154 |     1   (0)| 00:00:01 |
--------------------------------------------------------------------------------------

-- 드라이빙 테이블의 처리주관 인덱스 지정할때 사용


USE_MERGE

SELECT /*+ USE_MERGE(T1 T2) */ *
  FROM T1, T2
 WHERE T1.N4 = T2.N4
   AND T1.N2 = 10
   AND T2.N2 = 10;

--------------------------------------------------------------------------------------
| Id  | Operation                     | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT              |      |    10 |   280 |     4  (50)| 00:00:01 |
|   1 |  MERGE JOIN                   |      |    10 |   280 |     4  (50)| 00:00:01 |
|   2 |   SORT JOIN                   |      |    10 |   140 |     2  (50)| 00:00:01 |
|   3 |    TABLE ACCESS BY INDEX ROWID| T1   |    10 |   140 |     1   (0)| 00:00:01 |
|*  4 |     INDEX RANGE SCAN          | T1N2 |    10 |       |     1   (0)| 00:00:01 |
|*  5 |   SORT JOIN                   |      |    10 |   140 |     2  (50)| 00:00:01 |
|   6 |    TABLE ACCESS BY INDEX ROWID| T2   |    10 |   140 |     1   (0)| 00:00:01 |
|*  7 |     INDEX RANGE SCAN          | T2N2 |    10 |       |     1   (0)| 00:00:01 |
--------------------------------------------------------------------------------------

-- 조인 순서 별도 지정 필요


NO_USE_MERGE

SELECT /*+ NO_USE_MERGE(T1 T2) */ *
  FROM T1, T2
 WHERE T1.N4 = T2.N4
   AND T1.N2 = 10
   AND T2.N2 = 10;

-------------------------------------------------------------------------------------
| Id  | Operation                    | Name | Rows  | Bytes | Cost (%CPU)| Time     |
-------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT             |      |    10 |   280 |     3  (34)| 00:00:01 |
|*  1 |  HASH JOIN                   |      |    10 |   280 |     3  (34)| 00:00:01 |
|   2 |   TABLE ACCESS BY INDEX ROWID| T1   |    10 |   140 |     1   (0)| 00:00:01 |
|*  3 |    INDEX RANGE SCAN          | T1N2 |    10 |       |     1   (0)| 00:00:01 |
|   4 |   TABLE ACCESS BY INDEX ROWID| T2   |    10 |   140 |     1   (0)| 00:00:01 |
|*  5 |    INDEX RANGE SCAN          | T2N2 |    10 |       |     1   (0)| 00:00:01 |
-------------------------------------------------------------------------------------


USE_HASH

SELECT /*+ USE_HASH(T1 T2) */ *
  FROM T1, T2
 WHERE T1.N4 = T2.N4
   AND T1.N2 = 10
   AND T2.N2 = 10;

-------------------------------------------------------------------------------------
| Id  | Operation                    | Name | Rows  | Bytes | Cost (%CPU)| Time     |
-------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT             |      |    10 |   280 |     3  (34)| 00:00:01 |
|*  1 |  HASH JOIN                   |      |    10 |   280 |     3  (34)| 00:00:01 |
|   2 |   TABLE ACCESS BY INDEX ROWID| T1   |    10 |   140 |     1   (0)| 00:00:01 |
|*  3 |    INDEX RANGE SCAN          | T1N2 |    10 |       |     1   (0)| 00:00:01 |
|   4 |   TABLE ACCESS BY INDEX ROWID| T2   |    10 |   140 |     1   (0)| 00:00:01 |
|*  5 |    INDEX RANGE SCAN          | T2N2 |    10 |       |     1   (0)| 00:00:01 |
-------------------------------------------------------------------------------------

-- 통계가 없는 경우 (인라인뷰)에 한해서 조인 순서 별도 지정 (LEADING)


NO_USE_HASH

SELECT /*+ NO_USE_HASH(T1 T2) */ *
  FROM T1, T2
 WHERE T1.N4 = T2.N4
   AND T1.N2 = 10
   AND T2.N2 = 10;

--------------------------------------------------------------------------------------
| Id  | Operation                     | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT              |      |    10 |   280 |     4   (0)| 00:00:01 |
|   1 |  NESTED LOOPS                 |      |       |       |            |          |
|   2 |   NESTED LOOPS                |      |    10 |   280 |     4   (0)| 00:00:01 |
|   3 |    TABLE ACCESS BY INDEX ROWID| T1   |    10 |   140 |     1   (0)| 00:00:01 |
|*  4 |     INDEX RANGE SCAN          | T1N2 |    10 |       |     1   (0)| 00:00:01 |
|*  5 |    INDEX RANGE SCAN           | T2N2 |    10 |       |     1   (0)| 00:00:01 |
|*  6 |   TABLE ACCESS BY INDEX ROWID | T2   |     1 |    14 |     1   (0)| 00:00:01 |
--------------------------------------------------------------------------------------


"구루비 데이터베이스 스터디모임" 에서 2011년에 "새로쓴 대용량 데이터베이스 솔루션1" 도서를 스터디하면서 정리한 내용 입니다.

- 강좌 URL : http://www.gurubee.net/lecture/4443

- 구루비 강좌는 개인의 학습용으로만 사용 할 수 있으며, 다른 웹 페이지에 게재할 경우에는 출처를 꼭 밝혀 주시면 고맙겠습니다.~^^

- 구루비 강좌는 서비스 제공을 위한 목적이나, 학원 홍보, 수익을 얻기 위한 용도로 사용 할 수 없습니다.

댓글등록
SQL문을 포맷에 맞게(깔끔하게) 등록하려면 code() 버튼을 클릭하여 작성 하시면 됩니다.
로그인 사용자만 댓글을 작성 할 수 있습니다. 로그인, 회원가입