having 절은 group 함수로 검색조건을 줄때만 사용해야 합니다. 일반 검색조건을 주게 되면 인덱스 엑세스 하지 못하게 됩니다.
▣관련 예제
명령 프롤프트 창에 demo를 수행합니다.
튜닝전
create index emp_job on emp(job)
select job, sum(sal)
from emp
group by job
having sum(sal)> 5000 and job='SALESMAN';
select * from table(dbms_xplan.display_cursor(null, null, 'ALLSTATS LAST'))
;
튜닝 후
select job, sum(sal)
from emp
where job='SALESMAN'
group by job
having sum(sal)> 5000;
select * from table(dbms_xplan.display_cursor(null, null, 'ALLSTATS LAST'))
;
문제 11. 아래의 sql을 튜닝하시오!
@demo
create index emp_deptno on emp(deptno);
select deptno, avg(sal)
from emp
group by deptno
having avg(sal)>2000 and deptno = 20;
select * from table(dbms_xplan.display_cursor(null, null, 'ALLSTATS LAST'))
;
select deptno, avg(sal)
from emp
where deptno = 20
group by deptno
having avg(sal)>2000;
select * from table(dbms_xplan.display_cursor(null, null, 'ALLSTATS LAST'))
;