소트 수행 과정

Sort
To-Disk Sort

소트 오퍼레이션

Sort Aggregate

SQL> select sum(sal), max(sal), min(sal), avg(sal) from emp;

------------------------------------------------------------------------------------------
| Id  | Operation           | Name | Rows  | Bytes | Cost (%CPU)| Time     |
------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT    |      |     1 |     4 |     3   (0)| 00:00:01 |
|   1 |  SORT AGGREGATE     |      |     1 |     4 |            |          |
|   2 |   TABLE ACCESS FULL | EMP  |    14 |    56 |     3   (0)| 00:00:01 |
------------------------------------------------------------------------------------------
img
img

Sort Order By

SQL> select * from emp order by sal desc;

------------------------------------------------------------------------------------------
| Id  | Operation           | Name | Rows  | Bytes | Cost (%CPU)| Time     |
------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT    |      |    14 |   518 |     4  (25)| 00:00:01 |
|   1 |  SORT ORDER BY      |      |    14 |   518 |     4  (25)| 00:00:01 |
|   2 |   TABLE ACCESS FULL | EMP  |    14 |   518 |     3   (0)| 00:00:01 |
------------------------------------------------------------------------------------------

Sort Group By

SQL> select deptno, sum(sal), max(sal), min(sal), avg(sal)
  2  from   emp
  3  group by deptno
  4  order by deptno ;

------------------------------------------------------------------------------------------
| Id  | Operation           | Name | Rows  | Bytes | Cost (%CPU)| Time     |
------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT    |      |    11 |   165 |     4  (25)| 00:00:01 |
|   1 |  SORT GROUP BY      |      |    11 |   165 |     4  (25)| 00:00:01 |
|   2 |   TABLE ACCESS FULL | EMP  |    14 |   210 |     3   (0)| 00:00:01 |
------------------------------------------------------------------------------------------
img
SQL> select deptno, sum(sal), max(sal), min(sal), avg(sal)
  2  from   emp
  3  group by deptno ;

------------------------------------------------------------------------------------------
| Id  | Operation           | Name | Rows  | Bytes | Cost (%CPU)| Time     |
------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT    |      |    11 |   165 |     4  (25)| 00:00:01 |
|   1 |  HASH GROUP BY      |      |    11 |   165 |     4  (25)| 00:00:01 |
|   2 |   TABLE ACCESS FULL | EMP  |    14 |   210 |     3   (0)| 00:00:01 |
------------------------------------------------------------------------------------------
img

그룹핑 결과의 정렬 순서

SQL> select deptno, job, sum(sal), max(sal), min(sal)
  2  from   emp
  3  group by deptno, job ;

DEPTNO JOB                      SUM(SAL)   MAX(SAL)   MIN(SAL)
------ ------------------ ---------- ---------- ----------
    10 CLERK                    1300       1300       1300
    10 MANAGER                  2450       2450       2450
    10 PRESIDENT                5000       5000       5000
    20 CLERK                    1900       1100        800
    20 ANALYST                  6000       3000       3000
    20 MANAGER                  2975       2975       2975
    30 CLERK                     950        950        950
    30 MANAGER                  2850       2850       2850
    30 SALESMAN                 5600       1600       1250

9 개의 행이 선택되었습니다.

Execution Plan
----------------------------------------------------------
0      SELECT STATEMENT Optimizer=CHOOSE (Cost=4 Card=11 Bytes=132)
1    0   SORT (GROUP BY) (Cost=4 Card=11 Bytes=132)
2    1     TABLE ACCESS (FULL) OF 'EMP' (Cost=2 Card=14 Bytes=168)

Sort Unique

SQL> select /*+ ordered use_nl(dept) */ * from dept
  2  where  deptno in (select /*+ unnest */ deptno
  3                    from emp where job = 'CLERK') ;

---------------------------------------------------------------------------------------
| Id  | Operation                     | Name        | Rows  | Bytes | Cost (%CPU)|
---------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT              |             |     3 |    87 |     4  (25)|
|   1 |  NESTED LOOPS                 |             |     3 |    87 |     4  (25)|
|   2 |   SORT UNIQUE                 |             |     3 |    33 |     2   (0)|
|   3 |    TABLE ACCESS BY INDEX ROWID| EMP         |     3 |    33 |     2   (0)|
|   4 |     INDEX RANGE SCAN          | EMP_JOB_IDX |     3 |       |     1   (0)|
|   5 |   TABLE ACCESS BY INDEX ROWID | DEPT        |     1 |    18 |     1   (0)|
|   6 |    INDEX UNIQUE SCAN          | DEPT_PK     |     1 |       |     0   (0)|
---------------------------------------------------------------------------------------
-- 1. UNION (중복 제거를 위한 SORT UNIQUE 발생)
SQL> select job, mgr from emp where deptno = 10
  2  union
  3  select job, mgr from emp where deptno = 20;

------------------------------------------------------------------------------------------
| Id  | Operation           | Name | Rows  | Bytes | Cost (%CPU)| Time     |
------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT    |      |    10 |   150 |     8  (63)| 00:00:01 |
|   1 |  SORT UNIQUE        |      |    10 |   150 |     8  (63)| 00:00:01 |
|   2 |   UNION-ALL         |      |       |       |            |          |
|   3 |    TABLE ACCESS FULL| EMP  |     5 |    75 |     3   (0)| 00:00:01 |
|   4 |    TABLE ACCESS FULL| EMP  |     5 |    75 |     3   (0)| 00:00:01 |
------------------------------------------------------------------------------------------

-- 2. MINUS (차집합 연산을 위한 SORT UNIQUE 발생)
SQL> select job, mgr from emp where deptno = 10
  2  minus
  3  select job, mgr from emp where deptno = 20;

------------------------------------------------------------------------------------------
| Id  | Operation           | Name | Rows  | Bytes | Cost (%CPU)| Time     |
------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT    |      |     5 |   150 |     8  (63)| 00:00:01 |
|   1 |  MINUS              |      |       |       |            |          |v
|   2 |   SORT UNIQUE       |      |     5 |    75 |     4  (25)| 00:00:01 |
|   3 |    TABLE ACCESS FULL| EMP  |     5 |    75 |     3   (0)| 00:00:01 |
|   4 |   SORT UNIQUE       |      |     5 |    75 |     4  (25)| 00:00:01 |
|   5 |    TABLE ACCESS FULL| EMP  |     5 |    75 |     3   (0)| 00:00:01 |
------------------------------------------------------------------------------------------
SQL> select distinct deptno from emp order by deptno;

------------------------------------------------------------------------------------------
| Id  | Operation           | Name | Rows  | Bytes | Cost (%CPU)| Time     |
------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT    |      |     3 |     9 |     5  (40)| 00:00:01 |
|   1 |  SORT UNIQUE        |      |     3 |     9 |     4  (25)| 00:00:01 |
|   2 |   TABLE ACCESS FULL | EMP  |    14 |    42 |     3   (0)| 00:00:01 |
------------------------------------------------------------------------------------------
SQL> select distinct deptno from emp;

------------------------------------------------------------------------------------------
| Id  | Operation           | Name | Rows  | Bytes | Cost (%CPU)| Time     |
------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT    |      |     3 |     9 |     4  (25)| 00:00:01 |
|   1 |  HASH UNIQUE        |      |     3 |     9 |     4  (25)| 00:00:01 |
|   2 |   TABLE ACCESS FULL | EMP  |    14 |    42 |     3   (0)| 00:00:01 |
------------------------------------------------------------------------------------------

Sort Join

SQL> select /*+ ordered use_merge(e) */ *
  2  from   dept d, emp e
  3  where  d.deptno = e.deptno ;

------------------------------------------------------------------------------------------
| Id  | Operation           | Name | Rows  | Bytes | Cost (%CPU)| Time     |
------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT    |      |    14 |   770 |     8  (25)| 00:00:01 |
|   1 |  MERGE JOIN         |      |    14 |   770 |     8  (25)| 00:00:01 |
|   2 |   SORT JOIN         |      |     4 |    72 |     4  (25)| 00:00:01 |
|   3 |    TABLE ACCESS FULL | DEPT |     4 |    72 |     3   (0)| 00:00:01 |
|   4 |   SORT JOIN         |      |    14 |   518 |     4  (25)| 00:00:01 |
|   5 |    TABLE ACCESS FULL | EMP  |    14 |   518 |     3   (0)| 00:00:01 |
------------------------------------------------------------------------------------------

Window Sort

SQL> select empno, ename, job, mgr, sal
  2       , avg(sal) over (partition by deptno)
  3  from emp ;

------------------------------------------------------------------------------------------
| Id  | Operation           | Name | Rows  | Bytes | Cost (%CPU)| Time     |
------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT    |      |    14 |   406 |     4  (25)| 00:00:01 |
|   1 |  WINDOW SORT        |      |    14 |   406 |     4  (25)| 00:00:01 |
|   2 |   TABLE ACCESS FULL | EMP  |    14 |   406 |     3   (0)| 00:00:01 |
------------------------------------------------------------------------------------------