코드 :
--오늘의 자습 시간 문제_2026_01_22
--오늘의 마지막 문제1. (이수자 평가 6번 제출물)
--아래의 SQL을 해쉬조인으로 수행하는데 적절한 조인순서를 지정하시오
--먼저 bonus 테이블을 생성합니다
drop table bonus purge;
create table bonus
as
select empno, sal * 1.2 as comm2
from emp
where job='SALESMAN';
select * from bonus;
select /*+ leading(b e) use_hash(e) swap_join_inputs(b)*/ e.ename, b.comm2
from emp e, bonus b
where e.empno = b.empno(+);
select * from table(dbms_xplan.display_cursor(null, null, 'ALLSTATS LAST'));
--emp 테이블이 기준테이블 이 되야함.
--hash 메모리에는 bonus 테이블이 올라가야함.
select count(*) from emp; --14
select count(*) from bonus; --4
코드 :
--오늘의 마지막 문제2.
--아래의 SQL을 튜닝하는데 가장 버퍼가 작게 나오겠금 조인 순서와 조인방법을 결정하고
--인덱스도 가장 좋은걸로 생성하시오
drop table sales200;
drop table times200;
create table sales200
as
select *
from sh.sales;
create table times200
as
select *
from sh.times;
select count(*) from sales200; -- 918843
select count(*) from times200; -- 1826
--튜닝 전
select t.calendar_year, sum(amount_sold)
from sales200 s, times200 t
where s.time_id = t.time_id
and t.week_ending_day_id = 1582
group by t.calendar_year;
select * from table(dbms_xplan.display_cursor(null, null, 'ALLSTATS LAST'));
--데이터 개수 파악하기
select count(*) from sales200; -- 918843
select count(*) from times200 where week_ending_day_id = 1582; --7
--인덱스 생성
--sales200
create index sales200_time_id_amount_sold on sales200(time_id, amount_sold);
--times200
alter table times200 add constraint times200_pk primary key(week_ending_day_id,time_id,calendar_year);
--튜닝 후
select /*+leading(t s) use_nl(s)*/ t.calendar_year, sum(amount_sold)
from sales200 s, times200 t
where s.time_id = t.time_id
and t.week_ending_day_id = 1582
group by t.calendar_year;
select * from table(dbms_xplan.display_cursor(null, null, 'ALLSTATS LAST'));