시소당
대부분의 마스터-디테일관계의 조인은 디테일 테이블이 마스터의 내용을 확인하는 성격인테 조인대신 인라인뷰나 exists를 이용하여 수행속도를 높일 수 있다.
Customer(고객), Sale(판매정보)에서 고객정보의 영업점(br_no)이 b10001에서 b10100사이에 있는 고객의 판매내역을 조사하여 1995년 1/4/분기의 매출액(sale_amount)가 1,000원 이상되는 거래가 몇건인가 알아보려한다.
Select count(*) as 거래건수
From sale a, customer b
Where a.saledate beween ‘1990101’ and ‘19950331’
And a.sale_amt >= 1000
And a.cust_id = b.cust_id
And b.br_no btween ‘b10001’ and ‘b10100’;
<개선안1> (인라인뷰를 이용)
우선 Sale 테이블에서 먼저 group by하여 count를 구하여 그 다음 customer table을 연결하여 br_no조건을 체크한다.
select sum(cnt) as 거래건수
from ( select cust_no, count(*) as cnt
from sale
where saledate between ‘19950101’ and ‘19950331’ and a.sale_amt >= 1000
group by cust_no) v, customer b
where v.cust_no = b.cust_no
and b.br_no between ‘b10001’ and ‘b10100’;
<개선안2> (exists를 이용)
select count(*) as 거래건수
from sale a
where a.saledate bewteem ‘19950101’ and ‘19950331’
and a.sale_amt >= 1000
and exist (select ‘x’ from customer b
where a.cust_no = b.cust_no
and b.br_no beween ‘b10001’ and ‘b10100’);