๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
ORACLE/Tunning

[Oracle] ์‹คํ–‰๊ณ„ํš ํ™•์ธ ๋ฐฉ๋ฒ• XPLAN, AutoTrace, SQL Trace

by ๐ŸŒปโ™š 2020. 10. 4.

์‹คํ–‰๊ณ„ํš

์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€๋•Œ๋ฅผ ๋– ์˜ฌ๋ ค๋ด…์‹œ๋‹ค. ๋ฌธ์ œ๋ฅผ ์ฝ๊ณ  ์–ด๋–ค ๋ฐฉ์‹์œผ๋กœ ํ’€์–ด์•ผ ์ •ํ™•๋„์™€ ํšจ์œจ์„ฑ์„ ์žก์„ ์ˆ˜ ์žˆ์„์ง€ ๊ณ ๋ฏผํ•ฉ๋‹ˆ๋‹ค. ์™„์ „ํƒ์ƒ‰์„ ํ•ด์•ผํ•˜๋Š”์ง€, ์ด๋ถ„ํƒ์œผ๋กœ ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ๋Š”์ง€, ํ˜น์€ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์–ด๋–ค ๊ฒƒ์„ ์„ ํƒํ•ด์•ผํ•˜๋Š”์ง€ ๋“ฑ ์—ฌ๋Ÿฌ ๊ณ ๋ฏผ์„ ํ•˜๊ณ  ์ตœ์ ์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค.

 

์‹คํ–‰๊ณ„ํš๋„ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ๋ฅผ ํ‘ธ๋Š” ๊ณผ์ •๊ณผ ๋น„์Šทํ•ฉ๋‹ˆ๋‹ค. ์˜ตํ‹ฐ๋งˆ์ด์ €๊ฐ€ ์–ด๋–ค ๋ฐฉ์‹์œผ๋กœ ์ฟผ๋ฆฌ๋ฅผ ์‹คํ–‰ํ•ด์•ผ ๋น ๋ฅธ์†๋„๋กœ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ๊ณ ๋ คํ•˜์—ฌ ๊ฒฐ๊ณผ๋ฅผ ์–ป๊ธฐ๊นŒ์ง€์˜ ๊ณผ์ •์„ ์•Œ๋ ค์ค๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ์ด๋Ÿฌํ•œ ์‹คํ–‰๊ณ„ํš์„ ํ™•์ธํ•˜๋ฉด์„œ SQL ์ฟผ๋ฆฌ๋ฌธ์ด ํšจ์œจ์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๊ณ  ์žˆ๋Š”์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ๊ฐ€ ๋Œ€๋Ÿ‰์œผ๋กœ ๋Š˜์–ด๋‚˜๋ฉด์„œ ํŠนํžˆ ์†๋„๊ฐ€ ์ค‘์š”ํ•œ ์š”์†Œ๊ฐ€ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๊ฐ„๋‹จํ•œ ํŠœ๋‹์„ ํ†ตํ•ด์„œ๋„ ์—„์ฒญ๋‚œ ํšจ์œจ์„ ์–ป์„ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ํŠœ๋‹์˜ ๊ธฐ๋ณธ์ธ ์‹คํ–‰๊ณ„ํš์„ ํ™•์ธํ•˜๋Š” ๋ฐฉ๋ฒ•์„ XPLAN, AutoTrace, SQL Trace ํฌ๊ฒŒ  3๊ฐ€์ง€ ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

 

์‹ค์Šต ์ค€๋น„

OS : Red Hat Enterprise Linux Server release 6.10
DB : Oracle 11.2.0.1
SID : orcl
user : jamong

์œ„์˜ ํ™˜๊ฒฝ์—์„œ ์‹ค์Šต์„ ์ง„ํ–‰ํ–ˆ๊ณ  ์‹คํ–‰๊ณ„ํš์„ ํ™•์ธํ•˜๋Š” ๋ฐฉ๋ฒ•๋“ค์—์„œ ์—ฌ๋Ÿฌ ๊ถŒํ•œ ๋ฐ ์‚ฌ์ „ ์ž‘์—…์ด ํ•„์š”ํ•˜์—ฌ ์ƒˆ๋กญ๊ฒŒ jamong์ด๋ผ๋Š” ์œ ์ €๋ฅผ ์ƒ์„ฑํ•ด์„œ ์ง„ํ–‰ํ–ˆ์Šต๋‹ˆ๋‹ค.

 

SYS@orcl> create user jamong identified by jamong;

User created.

SYS@orcl> grant connect,resource to jamong;

Grant succeeded.

SYS@orcl> create table jamong.dept as select * from scott.dept;

Table created.

SYS@orcl> create table jamong.emp as select * from scott.emp;

Table created.

SYS@orcl> conn jamong/jamong
Connected.

JAMONG@orcl> alter table dept add constraint pk_dept primary key(deptno);

Table altered.

JAMONG@orcl> alter table emp add constraint pk_emp primary key(empno);

Table altered.

scott ๊ณ„์ •์˜ dept, emp ํ…Œ์ด๋ธ”์„ ์‚ฌ์šฉํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

select
  owner,
  synonym_name,
  table_owner,
  table_name
from all_synonyms
where synonym_name = 'PLAN_TABLE';

SQL์— ๋Œ€ํ•œ ์‹คํ–‰๊ณ„ํš์ด plan_table์— ์ €์žฅ๋ฉ๋‹ˆ๋‹ค. oracle 10g ๋ถ€ํ„ฐ๋Š” ์„ค์น˜์‹œ ๊ธฐ๋ณธ์ ์œผ๋กœ ํ…Œ์ด๋ธ”์ด ์ƒ์„ฑ๋˜๊ณ  ์ด๋ฅผ ๊ฐ€๋ฅดํ‚ค๋Š” plan_table synonym์ด ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ํ•ด๋‹น synonym์ด ์—†๋‹ค๋ฉด ์˜ค๋ผํด์—์„œ ์ œ๊ณตํ•˜๋Š” ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‹คํ–‰ํ•ด์„œ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์—†์„ ์‹œ ์•„๋ž˜ ๋ช…๋ น์œผ๋กœ ์ƒ์„ฑ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

SYS@orcl> @ ?/rdbms/admin/utlxplan.sql

์ด์ œ ๋ชจ๋“  ์ค€๋น„๋Š” ๋๋‚ฌ๊ณ  ์‹คํ–‰๊ณ„ํš ํ™•์ธ๋ฐฉ๋ฒ•์„ ํ•˜๋‚˜์”ฉ ํ™•์ธํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

 

์‹คํ–‰๊ณ„ํš ์‹คํ–‰ ์ˆœ์„œ

์œ„์™€ ๊ฐ™์ด ์‹คํ–‰๊ณ„ํš์ด ์žˆ๋‹ค๊ณ  ํ–ˆ์„๋•Œ ์‹คํ–‰๋˜๋Š” ์ˆœ์„œ๋Š” ์–ด๋–ป๊ฒŒ ๋ ๊นŒ์š”? ๊ฐ€์žฅ ์•ˆ์—์„œ๋ถ€ํ„ฐ, ์œ„์—์„œ๋ถ€ํ„ฐ๋ฅผ ๊ธฐ์–ตํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์œ„์˜ ์‹คํ–‰๊ณ„ํš์˜ ์ˆœ์„œ๋Š” 3 - 2 - 5 - 4 - 1์ด ๋˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

1. AutoTrace

AutoTrace๋Š” ์‹คํ–‰๊ณ„ํš์„ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•œ ๋„๊ตฌ์ค‘ ํ•˜๋‚˜๋กœ ๊ฐ€์žฅ ์‰ฝ๊ฒŒ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ์ฟผ๋ฆฌ ์‹คํ–‰๊ณผ ๋™์‹œ์— ์‹คํ–‰๊ณ„ํš ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ์ˆ˜ํ–‰ ๊ฒฐ๊ณผ, ์‹คํ–‰ ๊ณ„ํš, ์‹คํ–‰ ํ†ต๊ณ„ 3๊ฐ€์ง€ ๋ถ€๋ถ„์„ ์˜ต์…˜์—๋”ฐ๋ผ ์กฐํ•ฉํ•ด์„œ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ˆ˜ํ–‰ ๊ฒฐ๊ณผ์™€ ์‹คํ–‰ ๊ณ„ํš์€ ๋ฐ”๋กœ ํ™•์ธ ๊ฐ€๋Šฅํ•˜์ง€๋งŒ, ์‹คํ–‰ ํ†ต๊ณ„ ๋ถ€๋ถ„์„ ํ•จ๊ป˜ ์ถœ๋ ฅํ•˜๋ ค๋ฉด v_$sesstat, v_$statname, v_$mystat ๋ทฐ์— ๋Œ€ํ•œ ์ฝ๊ธฐ ๊ถŒํ•œ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์˜ค๋ผํด์—์„œ๋Š” plustrce.sql ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ œ๊ณตํ•˜์—ฌ ์‹คํ–‰ ํ†ต๊ณ„๋ฅผ ์ถœ๋ ฅํ•˜๊ธฐ ์œ„ํ•œ ๋ทฐ๋ฅผ ๋กค์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

SYS@orcl> @ ?/sqlplus/admin/plustrce.sql
SYS@orcl> grant plustrace to jamong;

Grant succeeded.

plustrace ๋กค์„ ๋ถ€์—ฌํ•˜์—ฌ ์‹คํ–‰ํ†ต๊ณ„์— ๋Œ€ํ•œ ๋ถ€๋ถ„๋„ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.

 

AutoTrace ๋ช…๋ น์–ด ์กฐํ•ฉ

๋ช…๋ น ์ˆ˜ํ–‰ ๊ฒฐ๊ณผ ์‹คํ–‰ ๊ณ„ํš ์‹คํ–‰ ํ†ต๊ณ„ plustrace ๊ถŒํ•œ ์—ฌ๋ถ€
set autotrace on O O O O
set autotrace on explain O O X X
set autotrace on statistics O X O O
set autotrace traceonly --> SQL ์ˆ˜ํ–‰ X O O O
set autotrace traceonly explain --> SQL ์ˆ˜ํ–‰ X X O X X
set autotrace traceonly statistics --> SQL ์ˆ˜ํ–‰ X X O O

autotrace ๋ช…๋ น์€ ์•ฝ์–ด autot๋กœ ๋Œ€์ฒดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•ด๋‹น ๋ช…๋ น์–ด๋“ค์„ ์‚ฌ์šฉํ•˜์—ฌ ์ถœ๋ ฅํ•˜๊ณ  ์‹ถ์€ ๋‚ด์šฉ๋งŒ ์„ ํƒํ•ด์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์‹คํ–‰ ํ†ต๊ณ„์— ๋Œ€ํ•œ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” plustrace ๊ถŒํ•œ์ด ํ•„์š”ํ•˜๊ณ  ์‹คํ–‰ ๊ณ„ํš๋งŒ ํ™•์ธํ• ๋•Œ์—๋Š” SQL๋ฌธ์— ๋Œ€ํ•œ ์ˆ˜ํ–‰์—†์ด ์ถœ๋ ฅ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

 

์‹คํ–‰ ๊ณ„ํš ๋ถ„์„

JAMONG@orcl> set autot traceonly explain
JAMONG@orcl> select * from emp where empno > 7800 and sal > 1400;

์šฐ์„  ์‹คํ–‰ ๊ณ„ํš์—์„œ ๊ฐ ํ•ญ๋ชฉ์ด ์–ด๋–ค ๋œป์„ ๋‚˜ํƒ€๋‚ด๋Š”์ง€๋ฅผ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. XPLAN์„ ์ด์šฉํ•œ ๋ฐฉ์‹์—์„œ๋„ ์œ„์™€ ๋™์ผํ•œ ํ˜•ํƒœ์˜ ์‹คํ–‰๊ณ„ํš์ด ์ถœ๋ ฅ๋˜๋Š”๋ฐ ๋ชจ๋‘ AutoTrace๋ฅผ ์ด์šฉํ–ˆ์„ ๋•Œ์™€ ๊ฐ™์€ ๋œป์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

ํ•ญ๋ชฉ ์˜๋ฏธ
Id Operation์˜ Id. Predicate Information์— ์ •๋ณด๊ฐ€ ์žˆ๋‹ค๋ฉด Id์™€ ์—ฐ๊ฒฐ๋œ๋‹ค.
Operation ์ˆ˜ํ–‰๋˜๋Š” ์ผ
Name Operation์ด ์ˆ˜ํ–‰๋˜๋Š” ํ…Œ์ด๋ธ” ํ˜น์€ ์ธ๋ฑ์Šค --> ์˜ค๋ธŒ์ ํŠธ๋ช…
Rows ๊ฐ Operation์ด ๋๋‚ฌ์„ ๋•Œ ๋ฐ˜ํ™˜๋˜๋Š” ์˜ˆ์ƒ์น˜ ๊ฑด์ˆ˜
Bytes Access ํ•˜๋Š” byte ์ˆ˜ ์˜ˆ์ƒ์น˜
Cost(%CPU) Operation์˜ ๋น„์šฉ --> ๋ˆ„์ ์น˜
Time ์˜ˆ์ƒ ์ˆ˜ํ–‰ ์‹œ๊ฐ„
access Predicate block์„ ์ฝ๊ธฐ ์ „์— ์–ด๋–ค ๋ฐฉ๋ฒ•์œผ๋กœ block์„ ์ฝ์„ ๊ฒƒ์ธ์ง€ ๊ฒฐ์ •. --> index ? full table scan ? ๋“ฑ
filter Predicate block์„ ์ฝ์€ ํ›„ ๋ฐ์ดํ„ฐ๋ฅผ ํ•„ํ„ฐ๋ง๋˜์–ด ์‚ฌ์šฉ๋˜๋Š” ์กฐ๊ฑด

์–ด๋””๊นŒ์ง€๋‚˜ ์‹คํ–‰ ๊ณ„ํš์ด๋ฏ€๋กœ ํ†ต๊ณ„์ •๋ณด๋กœ ๊ฒฐ๊ณผ๊ฐ€ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค. ์‹ค์ œ ๋น„์šฉ๊ณผ๋Š” ๋‹ค๋ฅผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์‹คํ–‰ ํ†ต๊ณ„ ๋ถ„์„

JAMONG@orcl> set autot traceonly statistics
JAMONG@orcl> select * from emp where empno > 7800 and sal > 1400;

ํ•ญ๋ชฉ ์˜๋ฏธ
recursive calls ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋‚ด๋ถ€์—์„œ ๋ฐœ์ƒํ•œ Call์˜ ์ˆ˜๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์žฌ๊ท€๋ผ๋Š” ์˜๋ฏธ๋ฅผ ๊ฐ–๋Š” recursive๋กœ ์•„๋ž˜์™€ ๊ฐ™์ด ๋‚ด๋ถ€์ ์œผ๋กœ ์ˆ˜ํ–‰๋˜๋Š” ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ์ƒํ™ฉ์—์„œ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.
-Data Dictionary ์กฐํšŒ ์‹œ
-์‚ฌ์šฉ์ž ์ •์˜ ํ•จ์ˆ˜๋ฅผ ์ˆ˜ํ–‰ ์‹œ
-ํ”„๋กœ์‹œ์ € ๋ฐ ํŠธ๋ฆฌ๊ฑฐ ๋‚ด์—์„œ SQL ์ˆ˜ํ–‰ ์‹œ
db block gets current mode block ์ฝ๊ธฐ ์š”์ฒญ๋œ ํšŸ์ˆ˜(logical read)
consistent gets consistent mode block ์ฝ๊ธฐ ์š”์ฒญ๋œ ํšŸ์ˆ˜(logical read)
physical reads ๋””์Šคํฌ์—์„œ ๋ฌผ๋ฆฌ์ ์œผ๋กœ ์ฝ์€ block์˜ ์ˆ˜
redo size redo์˜ ํฌ๊ธฐ
bytes set via SQL*NET to client ํฌ๊ทธ๋ผ์šด๋“œ ํ”„๋กœ์„ธ์Šค์—์„œ ํด๋ผ์ด์–ธํŠธ๋กœ ๋ณด๋‚ธ ์ด ๋ฐ”์ดํŠธ ์ˆ˜
bytes received via SQL*NET from client Oracle Net์„ ํ†ตํ•ด ํด๋ผ์ด์–ธํŠธ์—์„œ ๋ฐ›์€ ์ด ๋ฐ”์ดํŠธ ์ˆ˜
SQL*NET roundtrips to/from client ํด๋ผ์ด์–ธํŠธ์™€ ์ฃผ๊ณ  ๋ฐ›์€ ์ด Oracle Net ๋ฉ”์‹œ์ง€ ์ˆ˜
sorts (memory) ๋ฉ”๋ชจ๋ฆฌ ๋‚ด์—์„œ์˜ ์ •๋ ฌ ์ž‘์—… ํšŸ์ˆ˜
sorts (disk) ํ• ๋‹น ๋ฐ›์€ sort area ๋‚ด์—์„œ ์ •๋ ฌ์„ ์™„๋ฃŒํ•˜์ง€ ๋ชปํ•ด ๋””์Šคํฌ ๊ณต๊ฐ„์„ ์‚ฌ์šฉํ•œ ์ •๋ ฌ ์ž‘์—… ํšŸ์ˆ˜
rows processed ์ตœ์ข…์ ์œผ๋กœ ์ฒ˜๋ฆฌ๋˜์–ด ๋ฐ˜ํ™˜๋œ ํ–‰์˜ ์ˆ˜

์‹คํ–‰ ํ†ต๊ณ„์—์„œ ์ถœ๋ ฅ๋˜๋Š” ํ•ญ๋ชฉ์€ ์œ„์™€ ๊ฐ™์€ ์˜๋ฏธ๋ฅผ ๊ฐ–๋Š”๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

 

 

2. SQL Trace

SQL ํŠœ๋‹์— ์žˆ์–ด ๊ฐ€์žฅ ์„ธ๋ถ€์ ์œผ๋กœ ๋‚ด์šฉ์„ ์•Œ๋ ค์ฃผ๋Š” ๊ฒƒ์ด SQL Trace ์ž…๋‹ˆ๋‹ค. AutoTrace์˜ ๊ฒฐ๊ณผ๋กœ ๋ฌธ์ œ๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์„ ๋•Œ SQL Trace๋ฅผ ํ†ตํ•ด ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Oracle Database์—๋Š” Trace ํŒŒ์ผ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ์„ค์ •์„ ๋ณ€๊ฒฝํ•˜์—ฌ ์งˆ์˜ํ•œ SQL ์ฟผ๋ฆฌ์— ๋Œ€ํ•ด ๋Œ€๊ธฐ ์ด๋ฒคํŠธ๋‚˜ ์„ฑ๋Šฅ๊ด€๋ จ ์„ธ๋ถ€ ์ •๋ณด๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

SQL Trace ๋‚จ๊ธฐ๊ธฐ

SYS@orcl> grant alter session to jamong;

์ผ๋ฐ˜ ์‚ฌ์šฉ์ž๋ผ๋ฉด session์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ’์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋Š” ๊ถŒํ•œ์„ ๋ถ€์—ฌํ•ด์ค˜์•ผํ•ฉ๋‹ˆ๋‹ค.

 

JAMONG@orcl> alter session set tracefile_identifier='test_case';
JAMONG@orcl> alter session set sql_trace=true;

JAMONG@orcl> select * from dept;

JAMONG@orcl> alter session set sql_trace = false;

 

Trace๋ฅผ ๋‚จ๊ธฐ๊ธฐ ์œ„ํ•œ ์ž‘์—…์„ ์ง„ํ–‰ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. trace_identifier๋ฅผ ์ง€์ •ํ•˜์—ฌ trace ํŒŒ์ผ์„ ์ฐพ๊ธฐ ์‰ฝ๋„๋ก ์„ค์ •ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ดํ›„ sql_traceํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ True๋กœ ์„ค์ •ํ•˜๊ณ  Trace ์ •๋ณด๋ฅผ ํ™•์ธํ•˜๊ณ  ์‹ถ์€ ์ฟผ๋ฆฌ๋ฅผ ์งˆ์˜ ํ›„ sql_traceํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ False๋กœ ๋ณ€๊ฒฝํ–ˆ์Šต๋‹ˆ๋‹ค. 

 

[oracle@601d2fce71dc app]$ cd $ORACLE_BASE/diag/rdbms/orcl/orcl/trace
[oracle@601d2fce71dc trace]$ ls *test_case*.trc
orcl_ora_3896_test_case.trc

์ดํ›„ TraceํŒŒ์ผ์ด ์ƒ์„ฑ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.

 

tkprof ์œ ํ‹ธ๋ฆฌํ‹ฐ

TraceํŒŒ์ผ์„ ์ง์ ‘ ์—ด์–ด๋ณด๋ฉด ์•Œ๊ธฐ ํž˜๋“  ํ˜•ํƒœ๋กœ ๋˜์–ด์žˆ์Šต๋‹ˆ๋‹ค. ์˜ค๋ผํด์—์„œ ์ œ๊ณตํ•˜๋Š” tkprof ์œ ํ‹ธ๋ฆฌํ‹ฐ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์‚ฌ์šฉ์ž๊ฐ€ ๋ณด๊ธฐ ํŽธํ•œ ํ˜•ํƒœ๋กœ ๋ถ„์„๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•ด์ค๋‹ˆ๋‹ค.

 

[oracle@601d2fce71dc trace]$ tkprof orcl_ora_3896_test_case.trc report.txt sys=no

report.txt๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜๋ฉฐ sys=no ์˜ต์…˜์€ ๋‚ด๋ถ€์ ์œผ๋กœ ์ˆ˜ํ–‰๋˜๋Š” SQL๋ฌธ์žฅ์€ ์ œ์™ธ์‹œ์ผœ์ค๋‹ˆ๋‹ค.

 

[oracle@601d2fce71dc trace]$ vi report.txt 

ํŒŒ์ผ์„ ๋‚ด๋ฆฌ๋‹ค๋ณด๋ฉด ์‹คํ–‰ํ•œ ์ฟผ๋ฆฌ์— ๋Œ€ํ•œ ์ •๋ณด๊ฐ€ ์žˆ๋Š”๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

SQL Trace ์ž๋™ํ™”

SQL Trace ์ž‘์—…์„ ์œ„์—์„œ ๊ฐ„๋‹จํ•˜๊ฒŒ ํ™•์ธํ•ด๋ดค๋Š”๋ฐ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•˜๊ธฐ ๊นŒ์ง€ ๋งค์šฐ ๋ฒˆ๊ฑฐ๋กœ์šด ์ž‘์—…์„ ์ง„ํ–‰ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ Trace๋ฅผ on,off ํ˜•ํƒœ๋กœ ์ง„ํ–‰ํ•  ์ˆ˜ ์žˆ๋„๋ก ์Šคํฌ๋ฆฝํŠธ๋กœ ์ž๋™ํ™”ํ•ด์„œ ์‚ฌ์šฉํ•˜๋ฉด ํŽธํ•ฉ๋‹ˆ๋‹ค. ์ž๋™ํ™” ์Šคํฌ๋ฆฝํŠธ์—์„œ๋Š” ์˜ค๋ผํด์—์„œ ์ œ๊ณตํ•˜๋Š” event Trace๋ฅผ ์ด์šฉํ•˜์—ฌ SQL Trace๋ฅผ ์ƒ์„ฑ ๋ฐ ํ™•์ธํ•˜๋Š” ์ž‘์—…์„ ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

์ด๋ฐฉ์‹์„ ์‚ฌ์šฉํ• ๋•Œ ๋ ˆ๋ฒจ์„ ์„ค์ •ํ•˜๋Š”๋ฐ ์ด๋ฅผ ํ†ตํ•ด ๋Œ€๊ธฐ ์ด๋ฒคํŠธ์™€ ๋ฐ”์ธ๋“œ ๋ณ€์ˆ˜์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Level ๊ธฐ๋ณธ์ •๋ณด Binding ์ •๋ณด Waiting ์ •๋ณด
1 O X X
4 O O X
8 O X O
12 O O O

 

alter session set events '10046 trace name context forever, level 12';
alter session set events '10046 trace name context off';

์œ„์™€ ๊ฐ™์€ ํ˜•ํƒœ๋กœ ์‚ฌ์šฉ๋˜๋Š”๋ฐ level๋งŒ ๋ณ€๊ฒฝํ•˜์—ฌ Trace๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

 

start.sql

conn jamong/jamong
alter session set max_dump_file_size=unlimited;
alter session set events '10046 trace name context forever, level 12';

TraceํŒŒ์ผ์€ ์„ธ์…˜๋‹จ์œ„๋กœ ๊ฐฑ์‹ ๋˜๊ธฐ ๋•Œ๋ฌธ์— start.sql ์‹คํ–‰ ํ›„ ์ž…๋ ฅ๋˜๋Š” ์ฟผ๋ฆฌ๋งŒ ๊ฒฐ๊ณผ๋กœ ์ถœ๋ ฅํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•  ์œ ์ €๋กœ ์—ฐ๊ฒฐ์„ ์ƒˆ๋กญ๊ฒŒ ํ•ด์ค๋‹ˆ๋‹ค.

 

end.sql

alter session set events '10046 trace name context off';

define _editor=vi

undefine trace
column trace_file new_value trace

SELECT value AS trace_file
FROM v$diag_info
WHERE name = 'Default Trace File';

!tkprof &trace output.trc sys=no
ed output.trc

์—๋””ํ„ฐ๋ฅผ vi๋กœ ์ง€์ •ํ•ด์ฃผ๊ณ  trace๋ผ๋Š” ๋ณ€์ˆ˜์— ํ˜„์žฌ TraceํŒŒ์ผ์˜ ๊ฒฝ๋กœ๋ฅผ ์ €์žฅํ•ด์ฃผ๊ณ  tkprof ์œ ํ‹ธ๋ฆฌํ‹ฐ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํŒŒ์ผ์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

 

์‚ฌ์šฉ๋ฒ•

JAMONG@orcl> @start
JAMONG@orcl> select * from emp where empno > 7800 and sal > 1400;
JAMONG@orcl> @end

script๊ฐ€ ์ €์žฅ๋œ ๊ฒฝ๋กœ์—์„œ start๋กœ ์‹œ์ž‘ํ•˜๊ณ  ์ฟผ๋ฆฌ์งˆ์˜๋ฅผ ํ•œ ์ดํ›„์— end๋กœ ๋๋‚ด๋ฉด TraceํŒŒ์ผ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

SQL Trace ๋ถ„์„

ํ•ญ๋ชฉ ์˜๋ฏธ
Parse SQL๋ฌธ์„ ํŒŒ์‹ฑํ•˜๊ณ  ์‹คํ–‰ ๊ณ„ํš์„ ์ƒ์„ฑํ•˜๋Š”๋ฐ์— ๋Œ€ํ•œ ํ†ต๊ณ„
Execute Oracle ์„œ๋ฒ„์— ์˜ํ•ด ์‹ค์ œ ๋ช…๋ น๋ฌธ์ด ์‹คํ–‰๋˜๋Š”๋ฐ์— ๋Œ€ํ•œ ํ†ต๊ณ„.  INSERT,UPDATE,DELETE
Fetch Select๋ฌธ์— ๋Œ€ํ•œ ํ†ต๊ณ„
count ๊ฐ ๋‹จ๊ณ„์— ๋Œ€ํ•œ ์‹คํ–‰  ํšŸ์ˆ˜
cpu ์ฒ˜๋ฆฌ์— ์†Œ์š”๋˜๋Š” ์‹œ๊ฐ„(์ดˆ)
elapsed ์‹คํ–‰์— ์†Œ์š”๋˜๋Š” ์‹œ๊ฐ„(์ดˆ)
disk ๋ฌผ๋ฆฌ์  ๋ธ”๋ก ์ฝ๊ธฐ ์ˆ˜
query consistent mode ๋ธ”๋ก ์ฝ๊ธฐ(๋…ผ๋ฆฌ์  ์ฝ๊ธฐ)
current current mode ๋ธ”๋ก ์ฝ๊ธฐ(๋…ผ๋ฆฌ์  ์ฝ๊ธฐ)
rows ๊ฐ ๋‹จ๊ณ„์—์„œ ์ฒ˜๋ฆฌ๋˜๋Š” ํ–‰์˜ ์ˆ˜

 

ํ•ญ๋ชฉ ์˜๋ฏธ
Rows ๊ฐ ์ˆ˜ํ–‰ ๋‹จ๊ณ„์— ์ถœ๋ ฅ๋œ ํ–‰์˜ ์ˆ˜
cr consistent mode ๋ธ”๋ก ์ฝ๊ธฐ ์ˆ˜
pr ๋ฌผ๋ฆฌ์  ๋ธ”๋ก ์ฝ๊ธฐ ์ˆ˜
pw ๋ฌผ๋ฆฌ์  ๋ธ”๋ก ์“ฐ๊ธฐ ์ˆ˜
time ๊ฐ ์ˆ˜ํ–‰ ๋‹จ๊ณ„์—์„œ ์†Œ์š”๋˜๋Š” ์‹œ๊ฐ„
cost ๋น„์šฉ
size ๋ฐ์ดํ„ฐ ํฌ๊ธฐ
card cardinality

 

 

3. XPLAN

explain plan ๋ช…๋ น์–ด

explain plan ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•ด์„œ  ์‹คํ–‰๊ณ„ํš์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. explain plan ๋ช…๋ น์„ ์‚ฌ์šฉํ•˜๋ฉด ์‹ค์Šต ์ค€๋น„์—์„œ ์ƒ์„ฑํ•œ plan_table์— ์‹คํ–‰๊ณ„ํš์„ ๋‹ด์Šต๋‹ˆ๋‹ค. ์ดํ›„ ์˜ค๋ผํด์—์„œ ์ œ๊ณตํ•˜๋Š” utlxpls ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ด์šฉํ•ด์„œ ํ™•์ธํ•˜๊ธฐ ํŽธ์•ˆํ•œ ํ˜•ํƒœ๋กœ ์‹คํ–‰๊ณ„ํš์„ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

JAMONG@orcl> explain plan for select * from emp;
JAMONG@orcl> @ ?/rdbms/admin/utlxpls

 

dbms_xplan.display

utlxpls ์Šคํฌ๋ฆฝํŠธ๋ฅผ ํ™•์ธํ•ด๋ณด๋ฉด ์•„๋ž˜์™€๊ฐ™์€ ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.

select plan_table_output from table(dbms_xplan.display('plan_table',null,'serial'));

dbms_xplan ํŒจํ‚ค์ง€๋ฅผ ์ด์šฉํ•ด์„œ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. dbms_xplan์€ ์˜ค๋ผํด์—์„œ ์ œ๊ณตํ•˜๋Š” ํŒจํ‚ค์ง€๋กœ ์‹คํ–‰๊ณ„ํš์„ ํ™•์ธํ•  ๋•Œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฒซ๋ฒˆ์งธ ์ธ์ž๋กœ plan_table์„ ๋„ฃ์–ด์ฃผ๊ณ  ๋‘๋ฒˆ์งธ ์ธ์ž์—๋Š” statement_id, ์„ธ๋ฒˆ์งธ๋Š” ์ถœ๋ ฅ ํฌ๋งท ์˜ต์…˜์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‘๋ฒˆ์งธ ์ธ์ž์— null๊ฐ’์„ ๋„ฃ์œผ๋ฉด ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰์— explain plan ๋ช…๋ น์„ ํ†ตํ•ด ์‚ฌ์šฉ๋œ ์ฟผ๋ฆฌ์— ๋Œ€ํ•œ ์‹คํ–‰๊ณ„ํš์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์•„๋ž˜์™€ ๊ฐ™์€ ํ˜•ํƒœ๋กœ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

explain plan set statement_id='test' for select * from emp;
select plan_table_output from table(dbms_xplan.display('plan_table','test','serial'));
select plan_table_output from table(dbms_xplan.display('plan_table','test','basic'));
select plan_table_output from table(dbms_xplan.display('plan_table','test','typical'));
select plan_table_output from table(dbms_xplan.display('plan_table','test','basic rows bytes cost partition parallel predicate projection alias remote note'));
select plan_table_output from table(dbms_xplan.display('plan_table','test','advanced'));

 

 

dbms_xplan.display_cursor

display_cursor ํ•จ์ˆ˜๋Š” display ํ•จ์ˆ˜์™€ ๋‹ค๋ฅด๊ฒŒ ์ผ๋ฐ˜ ์˜ˆ์ƒ ์‹คํ–‰๊ณ„ํš์ด ์•„๋‹Œ ์‹ค์ œ ์‹คํ–‰๊ณ„ํš์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฆ‰, Shared Pool์— ์˜ฌ๋ผ๊ฐ€ ์žˆ๋Š” ์‹คํ–‰๊ณ„ํš์„ ์ฝ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. display_cursor๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์˜ค๋ผํด์—์„œ ์‹ค์ œ SQL๋ฌธ์„ ์‹คํ–‰ํ•˜๋Š” ๋™์•ˆ์˜ ์‹คํ–‰ ๊ณ„ํš์„ ๊ฐ Row Source๋ณ„๋ฃจ ์ˆ˜ํ–‰ ํ†ต๊ณ„๋ฅผ ์ˆ˜์ง‘ํ•˜๊ธฐ ์œ„ํ•ด gather_plan_statistics ํžŒํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ statistics_level ํŒŒ๋ฆฌ๋ฏธํ„ฐ๋ฅผ all๋กœ ์„ค์ •ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.

 

๊ทธ๋ฆฌ๊ณ  ์ผ๋ฐ˜ ์œ ์ €๊ฐ€ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ˆ˜์ง‘๋œ ํ†ต๊ณ„๋ฅผ ์ €์žฅํ•˜๋Š” ๋ทฐ์—๋Œ€ํ•œ ์ ‘๊ทผ์ด ํ•„์š”ํ•˜์—ฌ ์•„๋ž˜ ๊ถŒํ•œ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

grant select on v_$session to jamong;
grant select on v_$sql to jamong;
grant select on v_$sql_plan to jamong;
grant select on v_$sql_plan_statistics to jamong;
grant select on v_$sql_plan_statistics_all to jamong;

 

ํžŒํŠธ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ์•„๋ž˜์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

JAMONG@orcl> select /*+gather_plan_statistics*/ e.empno,e.sal,d.dname from emp e inner join dept d on e.deptno = d.deptno where e.sal >= 3000;
JAMONG@orcl> select * from table(dbms_xplan.display_cursor(null,null,'allstats last')) ;

๊ฐ€์žฅ ๋งˆ์ง€๋ง‰ ์ฟผ๋ฆฌ์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ํ™”์ธํ•˜๊ธฐ ์œ„ํ•ด ์ฒซ ๋‘ ์ธ์ž ๊ฐ’์„ null๋กœ ํ‘œ์‹œํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

display_cursor ์‹คํ–‰๊ณ„ํš ๋ถ„์„

ํ•ญ๋ชฉ ์˜๋ฏธ
Id Operation Id
Operation ์‹คํ–‰๋˜๋Š” ์ผ
Name Operation์ด ์ˆ˜ํ–‰๋˜๋Š” ํ…Œ์ด๋ธ” ํ˜น์€ ์ธ๋ฑ์Šค --> ์˜ค๋ธŒ์ ํŠธ๋ช…
Starts ๊ฐ Operation์„ ๋ฐ˜๋ณต ์ˆ˜ํ–‰ํ•˜๋Š” ํšŸ์ˆ˜
E-Rows ๊ฐ Operation์ด ๋๋‚ฌ์„ ๋•Œ ๋ฐ˜ํ™˜๋˜๋Š” ํ–‰ ์ˆ˜ (์˜ˆ์ƒ์น˜)
A-Rows ๊ฐ Operation์ด ๋๋‚ฌ์„ ๋•Œ ๋ฐ˜ํ™˜๋˜๋Š” ํ–‰ ์ˆ˜ (์‹ค์ œ)
A-Time ์‹ค์ œ ์‹คํ–‰ ์‹œ๊ฐ„ --> ๋ˆ„์ ์น˜
Buffers ๊ฐ Operation์˜ ๋…ผ๋ฆฌ์  ๋ธ”๋ก ์ฝ๊ธฐ ์ˆ˜
Reads ๊ฐ Operation์˜ ๋ฌผ๋ฆฌ์  ๋ธ”๋ก ์ฝ๊ธฐ ์ˆ˜ --> ์žˆ๋Š” ๊ฒฝ์šฐ์—๋งŒ ์ถœ๋ ฅ
Writes ๊ฐ Operation์˜๋ฌผ๋ฆฌ์  ๋ธ”๋ก ์“ฐ๊ธฐ ์ˆ˜ --> ์žˆ๋Š” ๊ฒฝ์šฐ์—๋งŒ ์ถœ๋ ฅ

 

์ •๋ฆฌ

๊ฐ„๋‹จํ•˜๊ฒŒ ์‹คํ–‰๊ณ„ํš์„ ์•Œ์•„๋ณด๋Š” ๋ฐฉ๋ฒ•์„ ์ •๋ฆฌํ•ด๋ดค์Šต๋‹ˆ๋‹ค. ๊ฐ ์ƒํ™ฉ๊ณผ ํ™˜๊ฒฝ์— ๋งž๊ฒŒ ์•Œ๋งž๋Š” ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋ฉด ๋˜๋Š”๋ฐ ๊ฐœ์ธ์ ์œผ๋กœ๋Š” SQL Trace๋ฅผ ํ™•์ธํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€์žฅ ๊ฐ•๋ ฅํ•œ ๋ฐฉ๋ฒ•์ด๋ผ๋Š” ์ƒ๊ฐ์ด ๋“œ๋„ค์š”.

 

๋Œ“๊ธ€