Goodus ๊ธฐ์ ๋ ธํธ [38 ํ ] Author ์ค๋ณ๊ธธ, ์ด์์ Creation Date 2009-02-27 Last Updated Version 1.0 Copyright(C) 2004 Goodus Inc. All Rights Reserved Version ๋ณ๊ฒฝ์ผ์๋ณ๊ฒฝ์ ( ์์ฑ์ ) ์ฃผ์๋ด์ฉ 1 2009-02-27 ์ค๋ณ๊ธธ, ์ด์์ ๋ฌธ์์ต์ด์์ฑ
Contents 1. SQL Tunning ์ํด์ผํ๋ค, ๊ทธ๋ฌ๋ SQL ์์์ ํ ์์๋ค!...3 2. Stored Outline...4 2.1. Outline ์ฌ์ฉํ User ์๊ฒ Outline ์คํํ ์์๋๊ถํ๋ถ์ฌ...4 2.2. Outline Test ํ๊ฒฝ์กฐ์ฑ...4 2.3. Outline ์์ฑํ Table ์ํต๊ณ์ ๋ณด์์ฑ...5 2.4. ์์ ํ ์์๋ SQL ์ Plan ํ์ธ...5 2.5. ์์ ํ ์์๋ SQL ๋ก Outline ์์ฑ...5 2.6. Tunning ๋ SQL Plan ํ์ธ...5 2.7. Tunning ๋ SQL ๋ก Outline ์์ฑ...6 2.8. ์์ฑ๋ Outline ๋คํ์ธ...6 2.9. Outline ์์ ...6 2.10. Tunning ๋ Plan ์คํ์ฌ๋ถํ์ธ...8 2.11. Outline Tunning ์์ ์์ฌํญ...9 3. SQL Profile ( 10g )...9 3.1. SQL Profile ์ฌ์ฉํ User ์๊ฒํ์๊ถํ๋ถ์ฌ...9 3.2. SQL Profile Test ํ๊ฒฝ์กฐ์ฑ...9 3.3. ์์ ํ ์์๋ SQL Plan ํ์ธ...10 3.4. Tunning ๋ SQL ์ Full Hint Naming ํ์ธ...10 3.5. SQL Profile ์๋์์ฑ...12 3.6. Tunning ๋ Plan ์คํ์ฌ๋ถํ์ธ...12 4. SQL Plan Baseline ( 11g )...13 4.1. SQL Plan Baseline Test ํ๊ฒฝ์กฐ์ฑ...13 4.2. Tunning ๋์ SQL ์ Plan Baseline Capture...14 4.3. Tunning ๋์ SQL ์ Plan Baseline ํ์ธ...15 4.4. Tunning ๋ SQL Plan ์ผ๋ก๋ณ๊ฒฝ...16 4.5. ์์ฑ๋ Baseline ํ์ธ...17 4.6. ๋ฑ๋กํ Plan History ํ์ธ...17 4.7. Tunning ๋ Plan ํ์ฉํ์ธ...19-2
1. SQL Tunning ์ํด์ผํ๋ค, ๊ทธ๋ฌ๋ SQL ์์์ ํ ์์๋ค! SQL Tunning ์ํ์๋กํ์ง๋ง SQL Text ๋ฅผ์์ ํ ์์๋ํ๊ฒฝ๋ค์ด๋ง์ด์๊ธฐ๊ณ ์๋ค. Package ๋ก๋ฉํ๋์๋ฃจ์ ๋ค์ด๋, Java ์ Hibernate Framework ๊ฐ์๊ฒ๋ค์ด๊ทธ์์ด๋ค. ์์๊ฐ์ํ๊ฒฝ์ SQL Tunning ์๋ขฐ๋ฅผ๋ฐ์์๋์ฐ๋ฆฌ๊ฐํ ์์๋๋ฐฉ๋ฒ๋ค์์์๋ณด์. 1) Parameter ์์ Parameter ๋ณ๊ฒฝ์์ ์ญ์ ์ด์ด์์ํ์ฑ์ด๋๋ค. ๊ทธ๋ฆฌ๊ณ ์ค์ ๋ก์ฌ์ฉํ ์์๋ํ๋ผ ๋ฏธํฐ์์๋๋งค์ฐ์ ํ์ ์ด๋ค. 2) Physical Design ์์ Partition ๋๋๊ธฐ, Block Size ๋ฐ๊พธ๊ธฐ, ๊ธฐํ Phsyical Atribute ๋ฅผ์์ ํ๋๋ฐฉ๋ฒ์ด์๋ค. 3) ํต๊ณ์ ๋ณด์กฐ์ Wolfgang Breitling ์์ํด์ฒด๊ณํ๋ TCF(Tuning By Cardinality Feedback) ๊ธฐ๋ฒ์ด์ฌ ๊ธฐ์์ํ๋ค. CBO ๊ฐ์ฌ๋ฐ๋ฅธํ๋จ์ํ ์์๋๋กํต๊ณ์ ๋ณด๋ฅผ๋ณด์ํด์ฃผ๋๊ธฐ๋ฒ์ด๋ค. ์กฐ์๊ฐ๋ฅํํต๊ณ์ ๋ณด์๋์ ํ์ด์์ผ๋ฉฐ Table/Column/Index/Histogram ๋ฑ๋ชจ๋ ํต๊ณ์ ๋ณด๋ฅผ์๋์ผ๋ก์กฐ์ํ ์์๋ค. ํ์ง๋ง, ์๋์ผ๋ก๋ณ๊ฒฝ๋ํต๊ณ์ ๋ณด๋์๋๋ฐฑ์ (10g) ์ด๋์ง์๋๋ค๋์ฌ์ค๊ณผํต๊ณ์ ๋ณด์์ง์๊ธฐ์กด์์กฐ์๋ํต๊ณ์ ๋ณด๋ฅผ๋ฎ์ด์จ๋ฒ๋ฆฐ๋ค๋๊ฒ์์ ์ํ์ฌ์ฌ์ฉํ์ฌ์ผํ๋ค. Index ๋ฅผ์์ฑํ๊ฑฐ๋ Index Key ๋ฅผ๋ณ๊ฒฝํ๋๊ฒ๋๋์๋ฒ์์์๋์ด๋ฒ์ฃผ์์ํ๋ค. 4) Stored Outline Stored Outline ์์๋ Plan Stability ๋ฅผ๋ณด์ฅํ๊ธฐ์ํด์ ์๋๊ฐ๋ ์ด์ง๋ง, ์ค์ธ๊ณ์์์ด๋ชฉ์ ์ผ๋ก์ฌ์ฉ๋๋๊ฒฝ์ฐ๋๊ฑฐ์์๋ค. ์คํ๋ ค Outline ๋ฐ๊ฟ์น๊ธฐ๋ฅผํตํด Query Tuning ์๋๊ตฌ๋กํ์ฉ๋๊ธฐ๋ํ๋ค. 5) SQL Profile 10g ์์์๊ฐ๋ SQL Profile ๋ SQL Tuning Advisor ๊ฐ์ ๊ณตํ๋๊ธฐ๋ฅ์คํ๋์ด๋ค. SQL Tuning Advisor ๋ SQL ๋ถ์ํ Hint ์กฐ์์ํตํด Query ์ฑ๋ฅ๊ฐ์ ์ด๊ฐ๋ฅํ๊ฒฝ์ฐ์๋ํด๋นํ๋ Profile ์์ ๊ณตํ๋ค. ์ด Profile ๋ฅผ์ฌ์ฉํ๋ฉด Query ์์คํ๊ณํ์์กฐ์ํ ์์๋ค. - 3
DBMS_SQLTUNE ํจํค์ง์ UNDOCUMENTED PROCEDURE ๋ค์์ด์ฉํ๋ฉด์๋์ผ๋ก Profile ์์์ฑํ๊ณ ์กฐ์ํ ์๋์๋ค. ์ด๊ด์ ์์๋ณด๋ฉด Stored Outline ์์ฌ์ฉํ๋๊ฒ๊ณผ๊ฑฐ์๋์ผํ๊ธฐ๋ฒ์ด๋ผ๊ณ ํ ์์๋ค. 6) Advanced Rewrite 10g ์์์๊ฐ๋ Advanced Rewrite ๋ํน์ SQL Text ๋ฅผ๊ฐ๋ก์ฑ์๋ค๋ฅธ SQL Text ๋ก ๋ณํํ๋๊ธฐ๋ฅ์์๋ฏธํ๋ค. DBMS_ADVANCED_REWRITE ํจํค์ง๋ฅผ์ด์ฉํ๋ค. ์ธ๋ป๋ณด๋ฉด๊ฐ์ฅ๊ฐ๋ ฅํ๊ณ ํ์คํ๊ธฐ๋ฒ์ผ๋ก๋ณด์ธ๋ค. ํ์ง๋ง, Bind ๋ณ์๊ฐ์๋ Query ๋ฑ์ด๊ธฐ๋ณธ์ ์ผ๋ก์ง์๋์ง์๋๋ค๋์ ์์น๋ช ์ ์ด๋ค. ๋ํ Parse ๊ณผ์ ์์์๋ถํ๋ฅผ์๊ฐํด๋ณด๋ฉด OLTP ์๋๋ง์ง์๋ค๋๊ฒฐ๋ก ์์ป์์์๋ค. DW ์ฑ์์ฟผ๋ฆฌ์์์ฌ์ฉ๋ ๋ชฉ์ ์ผ๋ก๊ณ ์๋๊ฒ์ด๋ค. ํ์ง๋ง Parse ๊ณผ์ ์์์์ค๋ฒํค๋๋ฅผ๊ฐ์ํด์๋ผ๋ Query ์ฑ๋ฅ์๋์ฌ์ผํ ๋ช ๋ถ์ด์๋ค๋ฉด๊ณ ๋ คํด๋ณผ๋งํ๋ฐฉ๋ฒ์ด๋ค ์ด๋ฒ๊ธฐ์ ๋ ธํธ์์๋ Stored Outline, 10g SQL Profile, 11g ์ SQL Baseline ์์๊ฐ ํ๊ฒ ๋ค. 2. Stored Outline Stored Outline ์ผ๋ก Plan ์๊ณ ์ ์ํฌ์์๋ค. ๊ธฐ์กด์๋ฌธ์ ๋๋์ฟผ๋ฆฌ๋ฅผ๊ฐ์ง๊ณ , Outline ์์์ฑํ์ฌํด๋น SQL Text ์๋ํ Plan ์ Tunning ๋ SQL ์๋ํ Plan ์ผ๋กํด๊ฒฐ๋๋๋ก์ ๋ํ์ฌ์์ ๋ถ๊ฐํ SQL ์๋ํ Tunning ์์๋ํ ์์๋ค. 2.1. Outline ์ฌ์ฉํ User ์๊ฒ Outline ์คํํ ์์๋๊ถํ๋ถ์ฌ SQL> conn /as sysdba SQL> grant create any outline to nero; SQL> grant execute on dbms_outln to nero; SQL> grant execute on dbms_outln_edit to nero; 2.2. Outline Test ํ๊ฒฝ์กฐ์ฑ SQL> create table nero_detail as select scott.dept; SQL> create table nero_master as select scott.emp; Index ์์ฑ - 4
SQL> alter table nero_detail add constraint nero_detail_pk primary key(empno); SQL> alter table nero_master add constraint nero_master_pk primary key(deptno); 2.3. Outline ์์ฑํ Table ์ํต๊ณ์ ๋ณด์์ฑ SQL> exec dbms_stats.gather_table_stats(user,'nero_detail', cascade=>true, no_invalidate=>false); SQL> exec dbms_stats.gather_table_stats(user,'nero_master', cascade=>true, no_invalidate=>false); 2.4. ์์ ํ ์์๋ SQL ์ Plan ํ์ธ SQL> set autotrace traceonly explain SQL> select e.ename from nero_detail e, nero_master d where e.deptno = d.deptno; --------------------------------------------------------------------------------- Id Operation Name Rows Bytes Cost (%CPU) Time --------------------------------------------------------------------------------- 0 SELECT STATEMENT 14 168 3 (0) 00:00:01 1 NESTED LOOPS 14 168 3 (0) 00:00:01 * 2 TABLE ACCESS FULL nero_detail 14 126 3 (0) 00:00:01 * 3 INDEX UNIQUE SCAN nero_master_pk 1 3 0 (0) 00:00:01 --------------------------------------------------------------------------------- Nested Loop Join ์ผ๋กํ๋ฆฌ๋๊ฒ์ํ์ธํ๋ค. 2.5. ์์ ํ ์์๋ SQL ๋ก Outline ์์ฑ SQL> set autotrace off SQL> create or replace outline ORG_OTLN on select e.ename from nero_detail e, nero_master d where e.deptno = d.deptno; 2.6. Tunning ๋ SQL Plan ํ์ธ SQL> set autotrace traceonly explain SQL> select /*+ use_hash(d) */ e.ename from nero_detail e, nero_master d where e.deptno = d.deptno; --------------------------------------------------------------------------------- Id Operation Name Rows Bytes Cost (%CPU) Time --------------------------------------------------------------------------------- - 5
0 SELECT STATEMENT 14 168 5 (20) 00:00:01 * 1 HASH JOIN 14 168 5 (20) 00:00:01 2 INDEX FULL SCAN nero_master_pk 4 12 1 (0) 00:00:01 * 3 TABLE ACCESS FULL nero_detail 14 126 3 (0) 00:00:01 --------------------------------------------------------------------------------- 2.7. Tunning ๋ SQL ๋ก Outline ์์ฑ SQL> create or replace outline NEW_OTLN on select /*+ use_hash(d) */ e.ename from nero_detail e, nero_master d where e.deptno = d.deptno; 2.8. ์์ฑ๋ Outline ๋คํ์ธ SQL> select name, sql_text from user_outlines; NAME SQL_TEXT --------- -------------------------------------------------------------------------------- ORG_OTLN select e.ename from nero_detail e, nero_master d where e.deptno = d.deptno NEW_OTLN select /*+ use_hash(d) */ e.ename from nero_detail e, nero_master d where e.dept 2.9. Outline ์์ - Stored Outline ์์์ ํ๊ธฐ์ํ์ฌ outln user ๋ก๋ณ๊ฒฝ SQL> conn outln/outln - ORG_OTLN, NEW_OTLN ์๋ํ์ค์ Outline ํ์ธํ๋ค. SQL> select ol_name, hint#, hint_text from ol$hints; OL_NAME HINT# HINT_TEXT --------- ------ ----------------------------------------------------------- ORG_OTLN ORG_OTLN ORG_OTLN ORG_OTLN ORG_OTLN ORG_OTLN 1 USE_NL(@"SEL$1" "D"@"SEL$1") 2 LEADING(@"SEL$1" "E"@"SEL$1" "D"@"SEL$1") 3 INDEX(@"SEL$1" "D"@"SEL$1" ("nero_master"."deptno")) 4 FULL(@"SEL$1" "E"@"SEL$1") 5 OUTLINE_LEAF(@"SEL$1") 6 ALL_ROWS - 6
ORG_OTLN ORG_OTLN NEW_OTLN NEW_OTLN NEW_OTLN NEW_OTLN NEW_OTLN NEW_OTLN NEW_OTLN NEW_OTLN 7 OPTIMIZER_FEATURES_ENABLE('10.2.0.1') 8 IGNORE_OPTIM_EMBEDDED_HINTS 2 LEADING(@"SEL$1" "D"@"SEL$1" "E"@"SEL$1") 3 FULL(@"SEL$1" "E"@"SEL$1") 4 INDEX(@"SEL$1" "D"@"SEL$1" ("nero_master"."deptno")) 5 OUTLINE_LEAF(@"SEL$1") 6 ALL_ROWS 7 OPTIMIZER_FEATURES_ENABLE('10.2.0.1') 8 IGNORE_OPTIM_EMBEDDED_HINTS 1 USE_HASH(@"SEL$1" "E"@"SEL$1") SQL> select ol_name, sql_text, category, hintcount from outln.ol$; OL_NAME SQL_TEXT CATEGORY HINTCOUNT ---------- ------------------------------------------------- -------- ---------- NEW_OTLN select /*+ use_hash(d) */ e.ename from o DEFAULT 8 ORG_OTLN select e.ename from nero_detail e, nero_master d DEFAULT 8 - ํํธ์นด์ดํธ๋ฅผ๋ง์ถ๊ธฐ์ํด ORG_OTLN ์ Hintcount ๋ฅผ NEW_OTLN ์ Hintcount ๋ก๋ณ๊ฒฝ SQL> update outln.ol$ set hintcount = (select hintcount from outln.ol$ where ol_name='new_otln') where ol_name='org_otln'; 1 row updated - Nested Loop ์คํ๊ณํ์์ฝ๋ณธ์์ด์ ๋์ด์ํ์์์ผ๋ฏ๋ก ORG_OTLN ์ํ ํธ๋ค์์ญ์ SQL> delete from ol$hints where ol_name = 'ORG_OTLN'; 8 rows deleted. - ORG_OTLN ์ํํธ๋ฅผ์ญ์ ํ NEW_OTLN ๋ฒ์ํํธ๋ค์ด ORG_OTLN ์ํํธ๊ฐ ๋๋๋ก OL_NAME ์๋ฐ๊ฟ. SQL> update ol$hints set ol_name = 'ORG_OTLN' where ol_name='new_otln'; - 7
8 rows updated. - ol$nodes ๋ทฐ์ ORG_OTLN ์๋ด์ฉ์ญ์ ํ์ ๋ฐ์ดํธ SQL> select * from ol$nodes where ol_name in('org_otln','new_otln'); OL_NAME CATEGORY NODE_ID PARENT_ID NODE_TYPE NODE_TEXTLEN NODE_TEXTOFF NODE_NAME --------- --------- -------- ---------- ---------- ------------ ------------ ---------- ORG_OTLN DEFAULT 1 0 3 65 1 SEL$1 NEW_OTLN DEFAULT 1 0 3 84 1 SEL$1 SQL> delete from ol$nodes where ol_name ='ORG_OTLN'; 1 row deleted. - ORG_OTLN ์ํํธ๋ฅผ์ญ์ ํ NEW_OTLN ๋ฒ์ํํธ๋ค์ ORG_OTLN ์ํํธ๊ฐ๋๋๋ก OL_NAME ์๋ฐ๊ฟ๋ฒ๋ฆผ SQL> update ol$nodes set ol_name = 'ORG_OTLN' where ol_name ='NEW_OTLN'; 1 row updated SQL> commit; 2.10. Tunning ๋ Plan ์คํ์ฌ๋ถํ์ธ. SQL> conn nero/nero SQL> alter session set use_stored_outlines=true; SQL> set autotrace traceonly explain SQL> select e.ename from nero_detail e, nero_master d where e.deptno = d.deptno; -- ๊ฐ์ SQL ๋ฌธ์ฅ์ด์ง๋ง์คํ๊ณํ์ Hash Join ์ผ๋ก์ํ๋จ์ํ์ธ๊ฐ๋ฅํจ. --------------------------------------------------------------------------------- Id Operation Name Rows Bytes Cost (%CPU) Time --------------------------------------------------------------------------------- - 8
0 SELECT STATEMENT 14 168 5 (20) 00:00:01 * 1 HASH JOIN 14 168 5 (20) 00:00:01 2 INDEX FULL SCAN nero_master_pk 4 12 1 (0) 00:00:01 * 3 TABLE ACCESS FULL nero_detail 14 126 3 (0) 00:00:01 --------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 1 - access("e"."deptno"="d"."deptno") 3 - filter("e"."deptno" IS NOT NULL) Note ----- - outline "ORG_OTLN" used for this statement 2.11. Outline Tunning ์์ ์์ฌํญ 1) ๋ฐ๊ฟ PLAN ์ด USE_CONCAT ํํธ๋ฅผ์ฌ์ฉํ๋ OR-Expansion ์๊ฒฝ์ฐ์๋ concatenation ๋๋ 2 ๋ฒ์งธ์ฟผ๋ฆฌ๋ธ๋ญ์ OL$HINTS ์์ HINT ์ ์ด๋ฅผํ ์์๋ค. 2) 10.2.0.3 ์ดํ๋ฒ์ ์์๋ Outline ์์ฑํ LCO ๋ฅผ invalidation ์์ผ์ผํ๋๋ฐ, Cursor ๋ณ Purge ๊ธฐ๋ฅ (patch5614566 - aix,linux ๋ 10203 ์๋ backporting ๋จ ) ์์ฌ์ฉํ์ง๋ชปํ๋ค. 3) cursor_sharing = force(similar) ๋ฅผ์ฌ์ฉํ๋๊ฒฝ์ฐ์ฌ์ฉํ ์์๋ค. 4) Outline ์ด SQL Text ๋ฅผ๋น๊ตํ๋๋ฐฉ์์ด๋ผ์ SQL Text ๊ฐ์๋ฒฝํ๊ฒ๋์ผํด์ผํ๋ค. 3. SQL Profile ( 10g ) Stored Outline ์ Full, Index ์๊ฐ์ Direct Hint ๋ก๊ตฌ์ฑ๋์ง๋ง, SQL Profile ์ OPT_ESTIMATE ์๊ฐ์ด Cardinality ๋ฅผ์ ์ดํ๋ Hint ๋ก๊ตฌ์ฑ๋์ด์๋ค. 3.1. SQL Profile ์ฌ์ฉํ User ์๊ฒํ์๊ถํ๋ถ์ฌ SQL> grant ALTER ANY SQL PROFILE to sqlprf; 3.2. SQL Profile Test ํ๊ฒฝ์กฐ์ฑ SQL> create table lms_mst as select * from scott.dept; SQL> create table lms_dtl as select * from scott.emp; - 9
Index ์์ฑ SQL> alter table lms_dtl add constraint nero_detail_pk primary key(empno); SQL> alter table lms_mst add constraint nero_master_pk primary key(deptno); 3.3. ์์ ํ ์์๋ SQL Plan ํ์ธ SQL> explain plan for select /*+ use_nl(e d) */ e.ename from lms_dtl e, lms_mst d where e.deptno = d.deptno; SQL> select * from table(dbms_xplan.display); ------------------------------------------------------------------------------ Id Operation Name Rows Bytes Cost (%CPU) Time ------------------------------------------------------------------------------ 0 SELECT STATEMENT 14 168 8 (0) 00:00:01 1 NESTED LOOPS 14 168 8 (0) 00:00:01 2 TABLE ACCESS FULL LMS_MST 4 12 3 (0) 00:00:01 * 3 TABLE ACCESS FULL LMS_DTL 4 36 1 (0) 00:00:01 ------------------------------------------------------------------------------ 3.4. Tunning ๋ SQL ์ Full Hint Naming ํ์ธ Advanced Option ์์ด์ฉํ์ฌ Full Hint Naming ์ํ์ธํ๋ค. SQL Profile ์์๋์กฐ์ ํ๋ ค๋ฉด Full Hint Naming Convention ์์ฌ์ฉํด์ผํ๋ค. SQ> explain plan for select /*+ use_hash(d) */ e.ename from lms_dtl e, lms_mst d where e.deptno = d.deptno; SQL> select * from table(dbms_xplan.display(null, null, advanced)); ------------------------------------------------------------------------------ Id Operation Name Rows Bytes Cost (%CPU) Time ------------------------------------------------------------------------------ 0 SELECT STATEMENT 14 168 7 (15) 00:00:01 * 1 HASH JOIN 14 168 7 (15) 00:00:01 2 TABLE ACCESS FULL LMS_MST 4 12 3 (0) 00:00:01 3 TABLE ACCESS FULL LMS_DTL 14 126 3 (0) 00:00:01 ------------------------------------------------------------------------------ - 10
PLAN_TABLE_OUTPUT ------------------------------------------------------------- Query Block Name / Object Alias (identified by operation id): ------------------------------------------------------------- 1 - SEL$1 2 - SEL$1 / D@SEL$1 3 - SEL$1 / E@SEL$1 Outline Data ------------- /*+ PLAN_TABLE_OUTPUT ------------------------------------------------------------- BEGIN_OUTLINE_DATA USE_HASH(@"SEL$1" "E"@"SEL$1") LEADING(@"SEL$1" "D"@"SEL$1" "E"@"SEL$1") FULL(@"SEL$1" "E"@"SEL$1") FULL(@"SEL$1" "D"@"SEL$1") OUTLINE_LEAF(@"SEL$1") ALL_ROWS OPTIMIZER_FEATURES_ENABLE('10.2.0.4') IGNORE_OPTIM_EMBEDDED_HINTS END_OUTLINE_DATA */ PLAN_TABLE_OUTPUT ------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 1 - access("e"."deptno"="d"."deptno") Column Projection Information (identified by operation id): - 11
----------------------------------------------------------- 1 - (#keys=1) "E"."ENAME"[VARCHAR2,10] 2 - "D"."DEPTNO"[NUMBER,22] PLAN_TABLE_OUTPUT ------------------------------------------------------------- 3 - "E"."ENAME"[VARCHAR2,10], "E"."DEPTNO"[NUMBER,22] 3.5. SQL Profile ์๋์์ฑ SQL Profile ์๊ตฌ์ฑํ Hint ์๋ชฉ๋ก์์ ๊ณตํด์ผํ๋ฉฐ, ๋ฐ๋์ Full Hint Naming Convention ์์ฌ์ฉํด์ผํ๋ค. Tunning ๋ Full Hint Name ์, ์์ ํ ์์๋ SQL ๊ณผํจ๊ป import ํ๋ค. SQL> begin dbms_sqltune.import_sql_profile( name=>'nero_prof', sql_text=> 'select /*+ use_nl(e d) */ e.ename from lms_dtl e, lms_mst d where e.deptno = d.deptno', profile=>sqlprof_attr('use_hash(@"sel$1" "E"@"SEL$1")', 'LEADING(@"SEL$1" "D"@"SEL$1" "E"@"SEL$1")', 'FULL(@"SEL$1" "E"@"SEL$1")', 'FULL(@"SEL$1" "D"@"SEL$1")') ); end; / PL/SQL procedure successfully completed. 3.6. Tunning ๋ Plan ์คํ์ฌ๋ถํ์ธ. SQL> explain plan for select /*+ use_nl(e d) */ e.ename from lms_dtl e, lms_mst d where e.deptno = d.deptno; SQL> select * from table(dbms_xplan.display); ----------------------------------------------------------------------------- - 12
Id Operation Name Rows Bytes Cost (%CPU) Time ------------------------------------------------------------------------------ 0 SELECT STATEMENT 14 168 7 (15) 00:00:01 * 1 HASH JOIN 14 168 7 (15) 00:00:01 2 TABLE ACCESS FULL LMS_MST 4 12 3 (0) 00:00:01 3 TABLE ACCESS FULL LMS_DTL 14 126 3 (0) 00:00:01 ------------------------------------------------------------------------------ PLAN_TABLE_OUTPUT -------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 1 - access("e"."deptno"="d"."deptno") Note ----- - SQL profile "nero_prof" used for this statement 4. SQL Plan Baseline ( 11g ) - SQL Magement Base SQL Plan Management ๋ฅผ๊ด๋ฆฌํ๋ Library - Statement Log ๋ฐ๋ณต๋๋ SQL ๋ฌธ์ฅ์ plan history ์๊ธฐ๋ก, ํ์์๋ง๋คํ์ฉ - Automatic SQL Tuning Task Load ๋์ด๋ง์ SQL ๋ฌธ์๋๋ฑํ๊ฑฐ๋๋๋์๊ณํ๋ง์์ฌ์ฉ - SMB (Segment Management Base) SQL Profile, Plan History, Plan Baselines, Plan History, Statement Log 4.1. SQL Plan Baseline Test ํ๊ฒฝ์กฐ์ฑ SQL> create table bls_mst as select * from scott.dept; SQL> create table bls_dtl as select * from scott.emp; - 13
Index ์์ฑ SQL> alter table bls_dtl add constraint nero_detail_pk primary key(empno); SQL> alter table bls_mst add constraint nero_master_pk primary key(deptno); ํต๊ณ์ ๋ณด์์ฑ SQL> exec dbms_stats.gather_table_stats(user, 'bls_mst', no_invalidate=>false); SQL> exec dbms_stats.gather_table_stats(user, 'bls_dtl', no_invalidate=>false); 4.2. Tunning ๋์ SQL ์ Plan Baseline Capture SQL> alter session set optimizer_capture_sql_plan_baselines = true; SQL> select /*+ use_nl(e d) */ e.ename from bls_dtl e, bls_mst d where e.deptno = d.deptno; ENAME ---------- SMITH ALLEN WARD JONES MARTIN BLAKE CLARK SCOTT KING TURNER ADAMS JAMES FORD MILLER 14 rows selected. SQL> select /*+ use_nl(e d) */ e.ename from bls_dtl e, bls_mst d where e.deptno = d.deptno; ENAME - 14
---------- SMITH ALLEN WARD JONES MARTIN BLAKE CLARK SCOTT KING TURNER ADAMS JAMES FORD MILLER 14 rows selected. SQL> alter session set optimizer_capture_sql_plan_baselines = false; 4.3. Tunning ๋์ SQL ์ Plan Baseline ํ์ธ SQL> col sql_handle new_value v_sql_handle SQL> select sql_handle from dba_sql_plan_baselines 2 where sql_text like 'select /*+ use_nl(e d) */ e.ename%'; SQL_HANDLE ------------------------------ SYS_SQL_ae6d7e4ca2ffd67c SQL> select * from dba_sql_plan_baselines where sql_handle ='SYS_SQL_ae6d7e4ca2ffd67c'; 1.2569E+19 SYS_SQL_ae6d7e4ca2ffd67c select /*+ use_nl(e d) */ e.ename from bls_dtl e, bls_mst d where e.deptno = d.d SYS_SQL_PLAN_a2ffd67c3c380fcf SQLBSLN AUTO-CAPTURE SQLBSLN 11.1.0.6.0 09/03/03 20:47:11.000000 09/03/03-15
20:47:11.000000 09/03/03 20:47:36.000000 YES YES NO YES 3 SQL*Plus Column Value ๋งํฌํจํ์์. 4.4. Tunning ๋ SQL Plan ์ผ๋ก๋ณ๊ฒฝ SQL> select /*+ use_hash(d) */ e.ename from bls_dtl e, bls_mst d where e.deptno = d.deptno; ENAME ---------- SMITH ALLEN WARD JONES MARTIN BLAKE CLARK SCOTT KING TURNER ADAMS JAMES FORD MILLER 14 rows selected. SQL> select sql_id, plan_hash_value from v$sql where sql_text like 'select /*+ use_hash(d) */ e.ename%'; SQL_ID PLAN_HASH_VALUE ------------- --------------- 0qwm4r9r8dyyn 1553352241 SQL> var nero_var number; - 16
SQL> exec :nero_var := dbms_spm.load_plans_from_cursor_cache(- sql_id=>'0qwm4r9r8dyyn', - plan_hash_value=>1553352241, - sql_handle=>'sys_sql_ae6d7e4ca2ffd67c'); PL/SQL procedure successfully completed. 4.5. ์์ฑ๋ Baseline ํ์ธ. Tunning ๋์ง๋ชปํ SQL Text ๋ก๋๊ฐ์ Baseline ์ด์์ฑ๋๊ฒ์ํ์ธํ๋ค. SQL> select sql_handle, sql_text from dba_sql_plan_baselines where sql_text like 'select /*+ use_nl(e d) */ e.ename%'; SQL_HANDLE ------------------------------ SQL_TEXT -------------------------------------------------------------------------------- SYS_SQL_ae6d7e4ca2ffd67c select /*+ use_nl(e d) */ e.ename from bls_dtl e, bls_mst d where e.deptno = d.d SYS_SQL_ae6d7e4ca2ffd67c select /*+ use_nl(e d) */ e.ename from bls_dtl e, bls_mst d where e.deptno = d.d 4.6. ๋ฑ๋กํ Plan History ํ์ธ. SQL> select * from table(dbms_xplan.display_sql_plan_baseline(' SYS_SQL_ae6d7e4ca2ffd67c )); PLAN_TABLE_OUTPUT -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- SQL handle: SYS_SQL_ae6d7e4ca2ffd67c SQL text: select /*+ use_nl(e d) */ e.ename from bls_dtl e, bls_mst d where e.deptno = d.deptno -------------------------------------------------------------------------------- - 17
-------------------------------------------------------------------------------- Plan name: SYS_SQL_PLAN_a2ffd67c3c380fcf Enabled: YES Fixed: NO Accepted: YES Origin: AUTO-CAPTURE -------------------------------------------------------------------------------- Plan hash value: 386123697 ------------------------------------------------------------------------------------- Id Operation Name Rows Bytes Cost (%CPU) Time ------------------------------------------------------------------------------------- 0 SELECT STATEMENT 14 168 3 (0) 00:00:01 1 NESTED LOOPS 14 168 3 (0) 00:00:01 2 TABLE ACCESS FULL BLS_DTL 14 126 3 (0) 00:00:01 * 3 INDEX UNIQUE SCAN NERO_MASTER_PK 1 3 0 (0) 00:00:01 ------------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 3 - access("e"."deptno"="d"."deptno") -------------------------------------------------------------------------------- Plan name: SYS_SQL_PLAN_a2ffd67c694097be Enabled: YES Fixed: NO Accepted: YES Origin: MANUAL-LOAD -------------------------------------------------------------------------------- Plan hash value: 1553352241 ------------------------------------------------------------------------------------- Id Operation Name Rows Bytes Cost (%CPU) Time ------------------------------------------------------------------------------------- 0 SELECT STATEMENT 14 168 5 (20) 00:00:01 * 1 HASH JOIN 14 168 5 (20) 00:00:01 2 INDEX FULL SCAN NERO_MASTER_PK 4 12 1 (0) 00:00:01 3 TABLE ACCESS FULL BLS_DTL 14 126 3 (0) 00:00:01 ------------------------------------------------------------------------------------- Predicate Information (identified by operation id): - 18
--------------------------------------------------- 1 - access("e"."deptno"="d"."deptno") 4.7. Tunning ๋ Plan ํ์ฉํ์ธ SQL> alter session set optimizer_use_sql_plan_baselines = true; SQL> explain plan for 2 select /*+ use_nl(e d) */ e.ename from bls_dtl e, bls_mst d where e.deptno = d.deptno; SQL> select * from table(dbms_xplan.display); Plan hash value: 386123697 ------------------------------------------------------------------------------------- Id Operation Name Rows Bytes Cost (%CPU) Time ------------------------------------------------------------------------------------- 0 SELECT STATEMENT 14 168 3 (0) 00:00:01 1 NESTED LOOPS 14 168 3 (0) 00:00:01 2 TABLE ACCESS FULL BLS_DTL 14 126 3 (0) 00:00:01 * 3 INDEX UNIQUE SCAN NERO_MASTER_PK 1 3 0 (0) 00:00:01 ------------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 3 - access("e"."deptno"="d"."deptno") Note ----- - SQL plan baseline "SYS_SQL_PLAN_a2ffd67c3c380fcf" used for this statement ์กฐ์ ๋ Query ๊ฐ Cost ๊ฐ๋์ผ๋ฏ๋ก, ์กฐ์ ์ ์ Query ์ Plan ์ด์คํ๋๋ค. - 19