select /*+ gather_plan_statistics
leading(d,e)
use_nlj(e)
*/
e.*, d.*
from hr.departments d, hr.employees e
where e.department_id = d.department_id(+);
select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));
Plan hash value: 2296652067
-----------------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows | A-Rows | A-Time | Buffers | OMem | 1Mem | Used-Mem |
-----------------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | | 107 |00:00:00.01 | 11 | | | |
|* 1 | HASH JOIN OUTER | | 1 | 107 | 107 |00:00:00.01 | 11 | 908K| 908K| 1226K (0)|
| 2 | TABLE ACCESS FULL| EMPLOYEES | 1 | 107 | 107 |00:00:00.01 | 4 | | | |
| 3 | TABLE ACCESS FULL| DEPARTMENTS | 1 | 27 | 27 |00:00:00.01 | 7 | | | |
-----------------------------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("E"."DEPARTMENT_ID"="D"."DEPARTMENT_ID")
Note
-----
- dynamic statistics used: dynamic sampling (level=2)
- this is an adaptive plan
select /*+ gather_plan_statistics
use_merge(d)*/
e.*, d.*
from hr.departments d, hr.employees e
where e.department_id = d.department_id(+);
select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));
Plan hash value: 2462675903
------------------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows | A-Rows | A-Time | Buffers | OMem | 1Mem | Used-Mem |
------------------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | | 107 |00:00:00.01 | 6 | | | |
| 1 | MERGE JOIN OUTER | | 1 | 107 | 107 |00:00:00.01 | 6 | | | |
| 2 | SORT JOIN | | 1 | 107 | 107 |00:00:00.01 | 4 | 15360 | 15360 |14336 (0)|
| 3 | TABLE ACCESS FULL| EMPLOYEES | 1 | 107 | 107 |00:00:00.01 | 4 | | | |
|* 4 | SORT JOIN | | 107 | 27 | 106 |00:00:00.01 | 2 | 2048 | 2048 | 2048 (0)|
| 5 | TABLE ACCESS FULL| DEPARTMENTS | 1 | 27 | 27 |00:00:00.01 | 2 | | | |
------------------------------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
4 - access("E"."DEPARTMENT_ID"="D"."DEPARTMENT_ID")
filter("E"."DEPARTMENT_ID"="D"."DEPARTMENT_ID")
select /*+ gather_plan_statistics
*/
e.*, d.*
from hr.departments d, hr.employees e
where e.department_id = d.department_id(+);
select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));
Plan hash value: 4017029581
--------------------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows | A-Rows | A-Time | Buffers | OMem | 1Mem | Used-Mem |
--------------------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | | 107 |00:00:00.01 | 14 | | | |
|* 1 | HASH JOIN RIGHT OUTER| | 1 | 107 | 107 |00:00:00.01 | 14 | 1376K| 1376K| 1511K (0)|
| 2 | TABLE ACCESS FULL | DEPARTMENTS | 1 | 27 | 27 |00:00:00.01 | 2 | | | |
| 3 | TABLE ACCESS FULL | EMPLOYEES | 1 | 107 | 107 |00:00:00.01 | 12 | | | |
--------------------------------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("E"."DEPARTMENT_ID"="D"."DEPARTMENT_ID")
select /*+ gather_plan_statistics
leading(d,e)
use_hash(e)
*/
e.*, d.*
from hr.departments d, hr.employees e
where e.department_id = d.department_id(+);
select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));
Plan hash value: 2296652067
-----------------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows | A-Rows | A-Time | Buffers | OMem | 1Mem | Used-Mem |
-----------------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | | 107 |00:00:00.01 | 11 | | | |
|* 1 | HASH JOIN OUTER | | 1 | 107 | 107 |00:00:00.01 | 11 | 908K| 908K| 1222K (0)|
| 2 | TABLE ACCESS FULL| EMPLOYEES | 1 | 107 | 107 |00:00:00.01 | 4 | | | |
| 3 | TABLE ACCESS FULL| DEPARTMENTS | 1 | 27 | 27 |00:00:00.01 | 7 | | | |
-----------------------------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("E"."DEPARTMENT_ID"="D"."DEPARTMENT_ID")
Note
-----
- dynamic statistics used: dynamic sampling (level=2)
- this is an adaptive plan
select /*+ gather_plan_statistics
leading(d,e)
use_hash(e)
swap_join_inputs(d)*/
e.*, d.*
from hr.departments d, hr.employees e
where e.department_id = d.department_id(+);
select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));
Plan hash value: 4017029581
--------------------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows | A-Rows | A-Time | Buffers | OMem | 1Mem | Used-Mem |
--------------------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | | 107 |00:00:00.01 | 14 | | | |
|* 1 | HASH JOIN RIGHT OUTER| | 1 | 107 | 107 |00:00:00.01 | 14 | 1376K| 1376K| 1569K (0)|
| 2 | TABLE ACCESS FULL | DEPARTMENTS | 1 | 27 | 27 |00:00:00.01 | 2 | | | |
| 3 | TABLE ACCESS FULL | EMPLOYEES | 1 | 107 | 107 |00:00:00.01 | 12 | | | |
--------------------------------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("E"."DEPARTMENT_ID"="D"."DEPARTMENT_ID")
inline view : from 절 괄호안에 select문nested subquerycorrelated subquery(상호관련, 상관관계있는) : 메인쿼리에 있는 컬럼이 서브쿼리에 있는 형태scalar subquery(스칼라 서브쿼리) : select절의 컬럼 위치에 select 문select /*+ gather_plan_statistics
leading(e,d)
use_nl(d)
*/
e.employee_id, e.department_id, d.department_name
from hr.departments d join hr.employees e
on e.department_id = d.department_id;
select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));

select /*+ gather_plan_statistics
leading(e,d)
use_nl(d)
*/
e.employee_id,e.salary, e.department_id, d.department_name
from hr.departments d join hr.employees e
on e.department_id = d.department_id;
select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));

select /*+ gather_plan_statistics
leading(e,d)
use_nl(d)
*/
e.employee_id,e.salary, e.department_id, d.department_name
from hr.departments d join hr.employees e
on e.department_id = d.department_id
order by 3;
select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));

select /*+ gather_plan_statistics*/
e.employee_id,e.salary, e.department_id,
(select department_name
from hr.departments
where department_id = e.department_id) dept_name
from hr.employees e;
select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));

select /*+ gather_plan_statistics */ *
from hr.employees
where department_id in (select department_id from hr.departments);
select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));

select /*+ gather_plan_statistics */ *
from hr.employees
where department_id in (select /*+ no_unnest */ department_id from hr.departments);
select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));

select /*+ gather_plan_statistics */ *
from hr.employees e
where exists (select /*+ no_unnest */ 'x' from hr.departments where department_id = e.department_id);
select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));

unnest : filter 방식 보다는 조인 방식으로 실행계획을 만든다.no_unnest : filter 방식으로 실행계획을 만든다.select /*+ gather_plan_statistics */ *
from hr.employees e
where exists (select 'x' from hr.departments where department_id = e.department_id);
select * from table(dbms_xplan.display_cursor(null,null,'allstats last'))

eliminate_join no_eliminate_joinselect c.column_name ,u.constraint_name, u.constraint_type, u.search_condition,u.r_constraint_name, u.index_name
from dba_constraints u, dba_cons_columns c
where u.constraint_name = c.constraint_name
and u.table_name in ('EMPLOYEES','DEPARTMENTS')
and u.owner = 'HR'
order by 1;

select /*+ gather_plan_statistics */ *
from hr.employees e
where exists (select /*+ no_eliminate_join(d) */ 'x' from hr.departments d where department_id = e.department_id);
select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));

select a.ksppinm as parameter,b.ksppstvl as session_value, c.ksppstvl as instance_value
from x$ksppi a, x$ksppcv b, x$ksppsv c
where a.indx = b.indx
and a.indx = c.indx
and a.ksppinm = '_optimizer_join_elimination_enabled';

select /*+ gather_plan_statistics */ e.*
from hr.departments d join hr.employees e
on e.department_id = d.department_id;
select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));

alter session set "_optimizer_join_elimination_enabled" = false;
select /*+ gather_plan_statistics */ e.*
from hr.departments d join hr.employees e
on e.department_id = d.department_id;
select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));

select /*+ gather_plan_statistics */ *
from hr.employees
where department_id is not null;
select /*+ gather_plan_statistics */ *
from hr.employees
where department_id in (select /*+ no_unnest */ department_id
from hr.departments
where location_id = 1500);
select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));

select /*+ gather_plan_statistics */ *
from hr.employees
where department_id in (select /*+ unnest */ department_id
from hr.departments
where location_id = 1500);
select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));

select /*+ gather_plan_statistics */ e.*
from hr.employees e, hr.departments d
where e.department_id = d.department_id
and d.location_id = 1500;
select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));

select /*+ gather_plan_statistics */ *
from hr.departments d
where exists (select 'x'
from hr.employees
where department_id = d.department_id);
select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));

select /*+ gather_plan_statistics */ d.*
from hr.employees e, hr.departments d
where e.department_id = d.department_id;
select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));

select /*+ gather_plan_statistics */ d.*
from (select distinct department_id from hr.employees) e, hr.departments d
where e.department_id = d.department_id;
select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));

nl_sjmerge_sjhash_sjselect /*+ gather_plan_statistics */ *
from hr.departments d -- departments는 1쪽 집합이다
where exists (select 'x'
from hr.employees -- m쪽 집합
where department_id = d.department_id);
select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));

select /*+ gather_plan_statistics */ *
from hr.departments d
where exists (select /*+ unnest nl_sj */'x'
from hr.employees
where department_id = d.department_id);
select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));

select /*+ gather_plan_statistics */ *
from hr.departments d
where exists (select /*+ unnest merge_sj */'x'
from hr.employees
where department_id = d.department_id);
select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));

select /*+ gather_plan_statistics */ *
from hr.departments d
where exists (select /*+ unnest hash_sj */'x'
from hr.employees
where department_id = d.department_id);
select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));

nl_ajmerge_ajhash_ajselect /*+ gather_plan_statistics */ *
from hr.departments d
where not exists (select 'x'
from hr.employees
where department_id = d.department_id);
select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));

select /*+ gather_plan_statistics */ *
from hr.departments d
where not exists (select /*+ unnest nl_aj */'x'
from hr.employees
where department_id = d.department_id);
select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));

select /*+ gather_plan_statistics */ *
from hr.departments d
where not exists (select /*+ unnest merge_aj */'x'
from hr.employees
where department_id = d.department_id);
select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));

select /*+ gather_plan_statistics */ *
from hr.departments d
where not exists (select /*+ unnest hash_aj */'x'
from hr.employees
where department_id = d.department_id);
select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));

select /*+ gather_plan_statistics */ e.*,d.*
from hr.employees e, hr.departments d, hr.locations l
where e.department_id = d.department_id
and d.location_id = l.location_id
and l.city = 'London';
select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));

select /*+ gather_plan_statistics */ e.*,d.*
from hr.employees e, hr.departments d
where e.department_id = d.department_id
and exists (select *
from hr.locations
where location_id = d.location_id
and city = 'London');
select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));

select /*+ gather_plan_statistics */ e.*,d.*
from hr.employees e, hr.departments d
where e.department_id = d.department_id
and exists (select /*+ no_unnest */ *
from hr.locations
where location_id = d.location_id
and city = 'London');
select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));

push_subqselect /*+ gather_plan_statistics */ e.*,d.*
from hr.employees e, hr.departments d
where e.department_id = d.department_id
and exists (select /*+ no_unnest push_subq */ *
from hr.locations
where location_id = d.location_id
and city = 'London');
select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));

select /*+ gather_plan_statistics */ *
from hr.departments d
where exists (select 'x'
from hr.locations
where location_id = d.location_id
and city = 'London')
and location_id is not null;
merge : view를 해체no_merge : view를 해체하지 않는다.select /*+ gather_plan_statistics */ *
from (select * from hr.employees where manager_id = 145) e,
(select * from hr.departments where location_id = 2500) d
where e.department_id = d.department_id;
select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));

select /*+ gather_plan_statistics */ *
from hr.employees e, hr.departments d
where e.department_id = d.department_id
and e.manager_id = 145
and d.location_id = 2500;
select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));

select /*+ gather_plan_statistics */ *
from (select /*+ no_merge */ * from hr.employees where manager_id = 145) e,
(select /*+ no_merge */ * from hr.departments where location_id = 2500) d
where e.department_id = d.department_id;
select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));

select /*+ gather_plan_statistics */ *
from hr.employees e, (select * from hr.departments where department_id = 20) d
where e.department_id = d.department_id ;
select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));

select /*+ gather_plan_statistics */ *
from hr.employees e, hr.departments d
where e.department_id = 20
d.department_id = 20;
select /*+ gather_plan_statistics */ d.department_id,
d.department_name, e.avg_sal
from hr.departments d,
(select department_id, avg(salary) avg_sal
from hr.employees
group by department_id) e
where e.department_id = d.department_id
and d.location_id = 1800;
select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));

select /*+ gather_plan_statistics */ d.department_id, d.department_name, e.avg_sal
from hr.departments d, (select /*+ no_merge */ department_id, avg(salary) avg_sal
from hr.employees
group by department_id) e
where e.department_id = d.department_id
and d.location_id = 1800;
select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));

2,3 번 수행구문
select *
from hr.departments
where location_id = 1800;
4번 : view 안의 조건부 술어에 department_id 값을 넣는다.
6,7,8번 수행구문, group 함수를 사용하면 sort aggregate operation이 발생(sort가 발생하는것이 아니라 그냥 이름일 뿐이다)
select /*+ gather_plan_statistics */ avg(salary) avg_sal
from hr.employees
where department_id = 20;

select /*+ gather_plan_statistics */ d.department_id, d.department_name, e.avg_sal
from hr.departments d, (select /*+ no_merge no_push_pred */ department_id, avg(salary) avg_sal
from hr.employees
group by department_id) e
where e.department_id = d.department_id
and d.location_id = 1800;
select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));
