์ฟผ๋ฆฌ ๋ณํ์ ํตํด Top-N ์ฟผ๋ฆฌ์ ์ฑ๋ฅ์ ๊ฐ์ ํ ์ฌ๋ก๋ฅผ ์ดํด๋ณด์.
-- 1 DROP TABLE t1 PURGE; DROP TABLE t2 PURGE; CREATE TABLE t1 AS SELECT ROWNUM AS c1, ROWNUM AS c2 FROM XMLTABLE ('1 to 100000'); CREATE TABLE t2 AS SELECT * FROM t1; ALTER TABLE t2 ADD CONSTRAINT t2_pk PRIMARY KEY (c1);
-- 2 SELECT * FROM (SELECT * FROM t1 a LEFT OUTER JOIN t2 b ON b.c1 = a.c2 WHERE a.c1 > 0 ORDER BY a.c1) a WHERE ROWNUM <= 10; -------------------------------------------------------------------- | Id | Operation | Name | Starts | A-Rows | Buffers | -------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1 | 10 | 418 | |* 1 | COUNT STOPKEY | | 1 | 10 | 418 | | 2 | VIEW | | 1 | 10 | 418 | |* 3 | SORT ORDER BY STOPKEY| | 1 | 10 | 418 | |* 4 | HASH JOIN OUTER | | 1 | 100K| 418 | |* 5 | TABLE ACCESS FULL | T1 | 1 | 100K| 209 | | 6 | TABLE ACCESS FULL | T2 | 1 | 100K| 209 | --------------------------------------------------------------------
-- 3 SELECT * FROM (SELECT * FROM t1 a WHERE a.c1 > 0 ORDER BY a.c1) a LEFT OUTER JOIN t2 b ON b.c1 = a.c2 WHERE ROWNUM <= 10; --------------------------------------------------------------------------- | Id | Operation | Name | Starts | A-Rows | Buffers | --------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1 | 10 | 229 | |* 1 | COUNT STOPKEY | | 1 | 10 | 229 | | 2 | NESTED LOOPS OUTER | | 1 | 10 | 229 | | 3 | VIEW | | 1 | 10 | 209 | | 4 | SORT ORDER BY | | 1 | 10 | 209 | |* 5 | TABLE ACCESS FULL | T1 | 1 | 100K| 209 | | 6 | TABLE ACCESS BY INDEX ROWID| T2 | 10 | 10 | 20 | |* 7 | INDEX UNIQUE SCAN | T2_PK | 10 | 10 | 10 | ---------------------------------------------------------------------------
์๋์ ๊ฐ์ด t2 ํ ์ด๋ธ์ ๋ํ ๋์ ์กฐ๊ฑด์ด ์กด์ฌํ๋ค๋ฉด ์ฟผ๋ฆฌ๋ฅผ 3๋ฒ ์ฟผ๋ฆฌ์ฒ๋ผ ๋ณ๊ฒฝํ ์ ์๋ค. ๋์ ์กฐ๊ฑด๊ณผ ํจ๊ป ์์ฐํฐ ์กฐ์ธ๊น์ง ๋์ ์ผ๋ก ์ฒ๋ฆฌํ๋ ๋ฐฉ์์ด ์ฑ๋ฅ ์ธก๋ฉด์์ ๊ฐ์ฅ ํจ์จ์ ์ด์ง๋ง ๊ฐ๋ฐ ๋์ด๋๊ฐ ๋์์ง๋ ๋จ์ ์ด ์๋ค.
-- 4 SELECT * FROM (SELECT * FROM t1 a LEFT OUTER JOIN t2 b ON b.c1 = a.c2 WHERE a.c1 > 0 AND b.c2 > 0 -- ๋์ ์กฐ๊ฑด ORDER BY a.c1) a WHERE ROWNUM <= 10; -------------------------------------------------------------------- | Id | Operation | Name | Starts | A-Rows | Buffers | -------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1 | 10 | 418 | |* 1 | COUNT STOPKEY | | 1 | 10 | 418 | | 2 | VIEW | | 1 | 10 | 418 | |* 3 | SORT ORDER BY STOPKEY| | 1 | 10 | 418 | |* 4 | HASH JOIN | | 1 | 100K| 418 | |* 5 | TABLE ACCESS FULL | T1 | 1 | 100K| 209 | |* 6 | TABLE ACCESS FULL | T2 | 1 | 100K| 209 | --------------------------------------------------------------------
-- 5 SELECT * FROM (SELECT a.* -- ๋ณ๊ฒฝ FROM t1 a LEFT OUTER JOIN t2 b ON b.c1 = a.c2 WHERE a.c1 > 0 AND b.c2 > 0 -- ๋์ ์กฐ๊ฑด ORDER BY a.c1) a LEFT OUTER JOIN t2 b ON b.c1 = a.c2 WHERE ROWNUM <= 10; --------------------------------------------------------------------------- | Id | Operation | Name | Starts | A-Rows | Buffers | --------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1 | 10 | 438 | |* 1 | COUNT STOPKEY | | 1 | 10 | 438 | | 2 | NESTED LOOPS OUTER | | 1 | 10 | 438 | | 3 | VIEW | | 1 | 10 | 418 | | 4 | SORT ORDER BY | | 1 | 10 | 418 | |* 5 | HASH JOIN | | 1 | 100K| 418 | |* 6 | TABLE ACCESS FULL | T1 | 1 | 100K| 209 | |* 7 | TABLE ACCESS FULL | T2 | 1 | 100K| 209 | | 8 | TABLE ACCESS BY INDEX ROWID| T2 | 10 | 10 | 20 | |* 9 | INDEX UNIQUE SCAN | T2_PK | 10 | 10 | 10 | ---------------------------------------------------------------------------
์๋ ์ฟผ๋ฆฌ๋ ๋์ ์กฐ๊ฑด์ด ๋นํ์ฑํ๋ ๊ฒฝ์ฐ๋ฅผ ๊ฐ์ ํ์ฌ ๋์ ์กฐ๊ฑด์ ์ฃผ์ ์ฒ๋ฆฌํ๋ค. ์ฟผ๋ฆฌ ๋ณํ์ ์ํด ์ธ๋ผ์ธ ๋ทฐ ๋ด์์ t2 ํ ์ด๋ธ์ ์กฐ์ธ์ด ์ ๊ฑฐ๋์๋ค. ๋ธ๋ก I/O๊ฐ 3๋ฒ ์ฟผ๋ฆฌ์ ๋์ผํ๊ฒ 229๊ฐ๋ง ๋ฐ์ํ๋ค. ์ฟผ๋ฆฌ ๋ณํ์ ํตํด ๋์ ์กฐ๊ฑด์ด ๋นํ์ฑํ๋ ๊ฒฝ์ฐ ๋ธ๋ก I/O๋ฅผ ๊ฐ์์ํค๋ ๊ฐ์ ๋ฐฉ์์ด๋ค.
-- 6 SELECT * FROM (SELECT a.* -- ๋ณ๊ฒฝ FROM t1 a LEFT OUTER JOIN t2 b ON b.c1 = a.c2 WHERE a.c1 > 0 -- AND b.c2 > 0 -- ๋์ ์กฐ๊ฑด ORDER BY a.c1) a LEFT OUTER JOIN t2 b ON b.c1 = a.c2 WHERE ROWNUM <= 10; --------------------------------------------------------------------------- | Id | Operation | Name | Starts | A-Rows | Buffers | --------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1 | 10 | 229 | |* 1 | COUNT STOPKEY | | 1 | 10 | 229 | | 2 | NESTED LOOPS OUTER | | 1 | 10 | 229 | | 3 | VIEW | | 1 | 10 | 209 | | 4 | SORT ORDER BY | | 1 | 10 | 209 | |* 5 | TABLE ACCESS FULL | T1 | 1 | 100K| 209 | | 6 | TABLE ACCESS BY INDEX ROWID| T2 | 10 | 10 | 20 | |* 7 | INDEX UNIQUE SCAN | T2_PK | 10 | 10 | 10 | ---------------------------------------------------------------------------
-- 7 SELECT a.* FROM (SELECT a.* , (SELECT MAX (x.c2) FROM t2 x WHERE x.c1 = a.c2) AS xc2 FROM t1 a ORDER BY a.c1) a WHERE ROWNUM <= 10 AND a.xc2 > 0 -- ๋์ ์กฐ๊ฑด ; -------------------------------------------------------------------------- | Id | Operation | Name | Starts | A-Rows | Buffers | -------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1 | 10 | 101K| | 1 | SORT AGGREGATE | | 100K| 100K| 101K| | 2 | TABLE ACCESS BY INDEX ROWID| T2 | 100K| 100K| 101K| |* 3 | INDEX UNIQUE SCAN | T2_PK | 100K| 100K| 1462 | |* 4 | COUNT STOPKEY | | 1 | 10 | 101K| |* 5 | VIEW | | 1 | 10 | 101K| | 6 | SORT ORDER BY | | 1 | 10 | 101K| | 7 | TABLE ACCESS FULL | T1 | 1 | 100K| 209 | --------------------------------------------------------------------------
์๋์ ๊ฐ์ด ์ฟผ๋ฆฌ๋ฅผ ๋ณ๊ฒฝํด๋ณด์. ๋ฉ์ธ ์ฟผ๋ฆฌ์ SELECT ๋ชฉ๋ก์์ t1 ํ ์ด๋ธ๋ง ์กฐํํ๊ณ , ์ค์นผ๋ผ ์๋ธ ์ฟผ๋ฆฌ๋ฅผ ์ค๋ณต ๊ธฐ์ ํ๋ค. ์ค๋ณต ์ค์นผ๋ผ ์๋ธ ์ฟผ๋ฆฌ๋ก ์ธํด ๋ธ๋ก I/O๊ฐ 7๋ฒ ์ฟผ๋ฆฌ๋ณด๋ค 20๊ฐ ์ฆ๊ฐํ๋ค.
-- 8 SELECT a.c1 -- ๋ณ๊ฒฝ , a.c2 -- ๋ณ๊ฒฝ , (SELECT MAX (x.c2) FROM t2 x WHERE x.c1 = a.c2) AS xc2 FROM (SELECT a.* , (SELECT MAX (x.c2) FROM t2 x WHERE x.c1 = a.c2) AS xc2 FROM t1 a ORDER BY a.c1) a WHERE ROWNUM <= 10 AND a.xc2 > 0 -- ๋์ ์กฐ๊ฑด ; ---------------------------------------------------------------------------- | Id | Operation | Name | Starts | A-Rows | Buffers | ---------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1 | 10 | 101K| | 1 | SORT AGGREGATE | | 10 | 10 | 20 | | 2 | TABLE ACCESS BY INDEX ROWID | T2 | 10 | 10 | 20 | |* 3 | INDEX UNIQUE SCAN | T2_PK | 10 | 10 | 10 | |* 4 | COUNT STOPKEY | | 1 | 10 | 101K| |* 5 | VIEW | | 1 | 10 | 101K| | 6 | SORT AGGREGATE | | 100K| 100K| 101K| | 7 | TABLE ACCESS BY INDEX ROWID| T2 | 100K| 100K| 101K| |* 8 | INDEX UNIQUE SCAN | T2_PK | 100K| 100K| 1462 | | 9 | SORT ORDER BY | | 1 | 10 | 101K| | 10 | TABLE ACCESS FULL | T1 | 1 | 100K| 209 | ----------------------------------------------------------------------------
์๋ ์ฟผ๋ฆฌ๋ ๋์ ์กฐ๊ฑด์ ์ฃผ์ ์ฒ๋ฆฌํ๋ค. ์์ฐํฐ ์กฐ์ธ๊ณผ ๋ง์ฐฌ๊ฐ์ง๋ก ์ฟผ๋ฆฌ ๋ณํ์ ์ํด ์ค์นผ๋ผ ์๋ธ ์ฟผ๋ฆฌ๊ฐ ์ ๊ฑฐ๋์ด ๋ธ๋ก I/O๊ฐ 209๊ฐ๋ง ๋ฐ์ํ๋ค.
-- 9 SELECT a.c1 -- ๋ณ๊ฒฝ , a.c2 -- ๋ณ๊ฒฝ , (SELECT MAX (x.c2) FROM t2 x WHERE x.c1 = a.c2) AS c2x FROM (SELECT a.* , (SELECT MAX (x.c2) FROM t2 x WHERE x.c1 = a.c2) AS xc2 FROM t1 a ORDER BY a.c1) a WHERE ROWNUM <= 10 -- AND a.xc2 > 0 -- ๋์ ์กฐ๊ฑด ; -------------------------------------------------------------------------- | Id | Operation | Name | Starts | A-Rows | Buffers | -------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1 | 10 | 209 | | 1 | SORT AGGREGATE | | 10 | 10 | 20 | | 2 | TABLE ACCESS BY INDEX ROWID| T2 | 10 | 10 | 20 | |* 3 | INDEX UNIQUE SCAN | T2_PK | 10 | 10 | 10 | |* 4 | COUNT STOPKEY | | 1 | 10 | 209 | | 5 | VIEW | | 1 | 10 | 209 | |* 6 | SORT ORDER BY STOPKEY | | 1 | 10 | 209 | | 7 | TABLE ACCESS FULL | T1 | 1 | 100K| 209 | --------------------------------------------------------------------------
'Oracle > Tuning' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
์ฌ๋ถ ์์ฑ์ผ๋ก ์ธํ ์ฑ๋ฅ ์ ํ ์ฌ๋ก #2 (0) | 2020.07.09 |
---|---|
์ฌ๋ถ ์์ฑ์ผ๋ก ์ธํ ์ฑ๋ฅ ์ ํ ์ฌ๋ก #1 (0) | 2020.07.08 |
๋ทฐ ๋ณํฉ์ด ๋์ํ์ง ์๋ ์ฌ๋ก (0) | 2020.04.07 |
์กฐ์ธ์ ๋ฐ๋ฅธ ์ฌ์ฉ์ ํจ์์ ๋์ (0) | 2020.03.21 |
์ ๊ธฐ์ค ์ด๋ ฅ ์กฐํ (0) | 2020.03.16 |