-- 총 10,000회의 조인이 발생
SELECT y.dept_name
, SUM(x.sale_qty) s_qty
, SUM(x.sale_amt) s_amt
FROM sale x
, dept y
WHERE x.dept_cd = y.dept_cd
AND x.yymm = :in_date
AND x.saup = :saup
GROUP BY y.dept_name
;
구분 | 수정전 | 수정후 | 개선효과 | 조인 | 10,000회 | 20회 | 9980회 {column} {column:width=10} {code:SQL} -- 총 20회의 조인이 발생 SELECT y.dept_name , x.s_qty , x.s_amt FROM (SELECT dept_cd , SUM(sale_qty) s_qty , SUM(sale_amt) s_amt FROM sale WHERE yymm = :in_date AND saup = :saup GROUP BY dept_cd ) x , dept y WHERE x.dept_cd = y.dept_cd ; {code} {column} {column}{column:width=80}{column} {section} |
---|
SELECT dept_cd
, SUM(DECODE(SUBSTR(sale_date,7,2),'01',sale_qty)) s_01
, SUM(DECODE(SUBSTR(sale_date,7,2),'02',sale_qty)) s_02
, .....................................................
, SUM(DECODE(SUBSTR(sale_date,7,2),'30',sale_qty)) s_30
, SUM(DECODE(SUBSTR(sale_date,7,2),'31',sale_qty)) s_31
FROM sale
WHERE yymm = :in_date
AND saup = :saup
GROUP BY dept_cd
;
구분 | 수정전 | 수정후 | 개선효과 | Decode | 10,000행 * 31개 | (20 * 31)행 * 31개 | 31만 ==> 19,220 | Substr | 10,000행 * 31개 | 10,000행 | 31만 ==> 1만 | Group by | 10,000행 | 10,000행 + (20 * 31)행 | 10,000 ==> 10,620 {column} {column:width=10} {code:SQL} SELECT dept_cd , SUM(DECODE(dd,'01',s_qty)) s_01 , SUM(DECODE(dd,'02',s_qty)) s_02 , ............................... , SUM(DECODE(dd,'30',s_qty)) s_30 , SUM(DECODE(dd,'31',s_qty)) s_31 FROM (SELECT dept_cd , SUBSTR(sale_date,7,2) dd , SUM(sale_qty) s_qty FROM sale WHERE yymm = :in_date AND saup = :saup GROUP BY dept_cd, SUBSTR(sale_date,7,2) ) GROUP BY dept_cd ; {code} {column} {column}{column:width=80}{column} {section} |
---|