
select ename, sal, dense_rank() over(order by sal desc) 순위
from emp;
select *
from (
select ename, sal, dense_rank() over(order by sal desc) 순위
from emp)
where 순위 in (1, 3, 5);
💡 from절의 서브쿼리의 결과가 메모리로 올라가서 구성이 됨
with emp_rank as (
select /*+ materialize */ ename, sal, dense_rank() over (order by sal desc) 순위
from emp
)
select *
from emp_rank;
-- with절 사용 후, 버퍼 갯수 줄어듦
SELECT * FROM table(dbms_xplan.display_cursor(null, null, 'ALLSTATS LAST'));
-- 튜닝 전:
select *
from ( select ename, sal, dense_rank() over (order by sal desc) 순위
from emp )
where sal > ( select avg(sal)
from ( select ename, sal, dense_rank() over (order by sal desc) 순위
from emp ) );
SELECT * FROM table(dbms_xplan.display_cursor(null, null, 'ALLSTATS LAST'));
-- 튜닝 후:
with emp_rank as (
select /*+ materialize */ ename, sal, dense_rank() over (order by sal desc) 순위
from emp
)
select *
from emp_rank
where sal > ( select avg(sal)
from emp_rank );
SELECT * FROM table(dbms_xplan.display_cursor(null, null, 'ALLSTATS LAST'));
select deptno, ename, sal, round(avg(sal) over (partition by deptno)) 부서평균
from emp
where sal > ( select avg(sal)
from ( select deptno, ename, sal, round(avg(sal) over (partition by deptno)) 부서평균
from emp
)
);
with dept_avg as (select deptno, ename, sal, round(avg(sal) over (partition by deptno)) 부서평균
from emp
)
select deptno, ename, sal, 부서평균
from dept_avg
where sal > (select avg(sal)
from dept_avg);
select avg(sum(sal))
from emp
group by job;
select job, sum(sal)
from emp
group by job
having sum(sal) > ( select avg(sum(sal))
from emp
group by job );
with job_sumsal as ( select job, sum(sal) 토탈월급
from emp
group by job )
select job, 토탈월급
from job_sumsal
where 토탈월급 > ( select avg(토탈월급)
from job_sumsal );
여러 with절이 동시에 돌면서 다같이 느려지지 않게 하기 위해서 DBA 가 with절을 동시에 돌지 않게끔 조절해야함. 너무 느리게 도는 with절은 개발자에게 다시 작성하라고 권고해야함

with job_sumsal as (select /*+ materialize */ job, sum(sal) as 토탈월급
from emp
group by job )
select job, 토탈월급
from job_sumsal
where 토탈월급 > ( select avg(토탈월급)
from job_sumsal );
select * from table(dbms_xplan.display_cursor(null, null, 'ALLSTATS LAST'));
with job_sumsal as (select /*+ inline */ job, sum(sal) as 토탈월급
from emp
group by job )
select job, 토탈월급
from job_sumsal
where 토탈월급 > ( select avg(토탈월급)
from job_sumsal );
select * from table(dbms_xplan.display_cursor(null, null, 'ALLSTATS LAST'));
- inline : temp 테이블 구성안하고 그냥 from 절의 서브쿼리로 실행해라
- materialize : temp 테이블 구성해라