신문지한장

[SQL튜닝] 실행계획과 비용 본문

Oracle/sql&script

[SQL튜닝] 실행계획과 비용

신문지한장 2024. 8. 6. 16:55

1. 테스트용 테이블 생성

create table t
as
select d.no, e.*
from scott.emp e, (select rownum no from dual connect by level <= 1000 )d;

 

2. 테스트용 인덱스 생성

create index t_x01 on t(deptno,no);
create index t_x02 on t(deptno, job, no);

 

3. T테이블에 통계정보 수집

exec dbms_stats.gather_table_stats(user, 't');

 

4. AutoTrace 활성화 후 SQL 실행시 실행계획 확인가능

select * from t
where deptno=10
and no=1;

옵티마이저가 선택한 인덱스 T_X01, T_X02를 선택할 수있고 테이블을 선택할 수 있는데 T_X01을 선택한 이유는 Cost가 2로 표시됨

 

5. T_X02로 강제 힌트 줘서 사용함

set autotrace traceonly exp;
select /*+ index( t t_x02) */ * from t
where deptno=10
and no=1;

Cost가 7로 올라감

 

6. full scan 할 경우 Cost가 29로 올라감

select /*+ full(t) */* from t
where deptno=10
and no=1;