
존재하는 테이블 검색

select deptno
from dept;

select deptno
from emp;
ㄴ emp테이블에는 deptno가 40번인 사원이 없음
-- 튜닝 전 SQL
select deptno
from dept
where deptno in (select deptno
from emp);
-- 튜닝 후 SQL
select deptno
from dept d
where exists ( select deptno
from emp e
where e.deptno = d.deptno);
💡 위와 같이 쿼리문이 있으면 메인쿼리부터 작동이 되어서
dept 테이블의 부서번호 10번부터 emp 테이블에 deptno에 존재하는지
를 찾아봅니다. 위에서 찾다가 존재하면 더이상 아래로 내려가면서
찾지 않습니다. 존재하니까 찾지 않습니다. 그래서 exists 문이
검색속도가 빠릅니다.
drop table telecom_table;
create table telecom_table
( telecom varchar2(30),
t_price number(10),
t_cnt number(10) );
insert into telecom_table values( 'KT', 13000, 6 );
insert into telecom_table values( 'LG', 14000, 5 );
insert into telecom_table values( 'SKT', 12000, 7 );
insert into telecom_table values( 'KT알뜰', 9000, 1 );
insert into telecom_table values( 'LG알뜰', 8000, 1 );
insert into telecom_table values( 'SKT알뜰', 7000, 1 );
commit;
-- 튜닝 전:
select telecom
from telecom_table
where telecom in ( select telecom
from emp21 );
-- 실행계획 보기
SELECT * FROM table(dbms_xplan.display_cursor(null, null, 'ALLSTATS LAST'));
답
-- 튜닝 후:
select telecom
from telecom_table t
where exists ( select telecom
from emp21 e
where e.telecom = t.telecom );

select deptno
from dept d
where not exists (select deptno
from emp e
where e.deptno = d.deptno);

select telecom
from telecom_table t
where not exists (select telecom
from emp21 e
where e.telecom = t.telecom);
-- 잘 만들어졌는지 확인코드
select *
from hr.employees;
select *
from hr.departments;