Skip to main content

01D

2023๋…„ 12์›” 27์ผLess than 1 minuteOracle DBcrashcourseoracleoracle-dboracle-sql

01D ๊ด€๋ จ


์ธ๋ฑ์Šค ๊ธฐ๋ณธ ์›๋ฆฌ

B*Tree ์ธ๋ฑ์Šค๋ฅผ ์ •์ƒ์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ๋ฒ”์œ„ ์Šค์บ” ์‹œ์ž‘์ง€์ ์„ ์ฐพ๊ธฐ ์œ„ํ•ด ๋ฃจํŠธ ๋ธ”๋ก๋ถ€ํ„ฐ ๋ฆฌํ”„๋ธ”๋ก๊นŒ์ง€์˜ ์ˆ˜์ง์  ํƒ์ƒ‰ ๊ณผ์ •์„ ๊ฑฐ์ณ์•ผ ํ•จ

์ธ๋ฑ์Šค ์‚ฌ์šฉ์ด ๋ถˆ๊ฐ€๋Šฅ ํ•˜๊ฑฐ๋‚˜ ๋ฒ”์œ„ ์Šค์บ”์ด ๋ถˆ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ

์ธ๋ฑ์Šค ์ปฌ๋Ÿผ์˜ ๊ฐ€๊ณต

์ธ๋ฑ์Šค ์ปฌ๋Ÿผ ๊ฐ€๊ณต ์‚ฌ๋ก€ํŠœ๋‹ ๋ฐฉ์•ˆ
substr(์—…์ฒด๋ช…, 1, 2) = '๋Œ€ํ•œ'์—…์ฒด๋ช… like '๋Œ€ํ•œ%'
์›”๊ธ‰์—ฌ * 12 = 36000000์›”๊ธ‰์—ฌ = 36000000 / 12
to_char(์ผ์‹œ, 'yyyymmdd') = :dt์ผ์‹œ >= to_date(:dt, 'yyyymmdd') and ์ผ์‹œ < to_date(:dt, 'yyyymmdd') + 1
`์—ฐ๋ น
`ํšŒ์›๋ฒˆํ˜ธ
nvl(์ฃผ๋ฌธ์ˆ˜๋Ÿ‰, 0) >= 100์ฃผ๋ฌธ์ˆ˜๋Ÿ‰ >= 100
nvl(์ฃผ๋ฌธ์ˆ˜๋Ÿ‰, 0) < 100create index ์ฃผ๋ฌธ_x01 on ์ฃผ๋ฌธ(nvl(์ฃผ๋ฌธ์ˆ˜๋Ÿ‰, 0) );
not null ์ปฌ๋Ÿผ์ด๋ฉด nvl์ œ๊ฑฐ, ์•„๋‹ˆ๋ฉด ํ•จ์ˆ˜๊ธฐ๋ฐ˜ ์ธ๋ฑ์Šค(FBI) ์ƒ์„ฑ ๊ณ ๋ ค

ํŠœ๋‹์‚ฌ๋ก€1

์ผ๋ณ„์ง€์ˆ˜์—…์ข…๋ณ„๊ฑฐ๋ž˜๋ฐ์‹œ์„ธ_PK : ์ง€์ˆ˜๊ตฌ๋ถ„์ฝ”๋“œ + ์ง€์ˆ˜์—…์ข…์ฝ”๋“œ + ๊ฑฐ๋ž˜์ผ์ž ์ผ๋ณ„์ง€์ˆ˜์—…์ข…๋ณ„๊ฑฐ๋ž˜๋ฐ์‹œ์„ธ_X01 : ๊ฑฐ๋ž˜์ผ์ž

๊ฑฐ๋ž˜์ผ์ž between :startDd and :endDd
and ์ง€์ˆ˜๊ตฌ๋ถ„์ฝ”๋“œ || ์ง€์ˆ˜์—…์ข…์ฝ”๋“œ in ('1001', '2003');  => ๊ฑฐ๋ž˜์ผ์ž ์ธ๋ฐ์Šค ์‚ฌ์šฉ ํ˜น์€ Full Table Scan
=>
๊ฑฐ๋ž˜์ผ์ž between :startDd and :endDd
and (์ง€์ˆ˜๊ตฌ๋ถ„์ฝ”๋“œ, ์ง€์ˆ˜์—…์ข…์ฝ”๋“œ) in (('1', '001'), ('2', '003')); => PK ์ธ๋ฑ์Šค ์‚ฌ์šฉ

ํŠœ๋‹์‚ฌ๋ก€2

์ ‘์ˆ˜์ •๋ณดํŒŒ์ผ_PK : ์ˆ˜์‹ ๋ฒˆํ˜ธ ์ ‘์ˆ˜์ •๋ณดํŒŒ์ผ_X01 : ์ •์ •๋Œ€์ƒ์ ‘์ˆ˜๋ฒˆํ˜ธ + ๊ธˆ๊ฐ์›์ ‘์ˆ˜๋ฒˆํ˜ธ

decode(์ •์ •๋Œ€์ƒ์ ‘์ˆ˜๋ฒˆํ˜ธ, lpad(' ', 14), ๊ธˆ๊ฐ์›์ ‘์ˆ˜๋ฒˆํ˜ธ, ์ •์ •๋Œ€์ƒ์ ‘์ˆ˜๋ฒˆํ˜ธ) = :์ ‘์ˆ˜๋ฒˆํ˜ธ => Full Table Scan
=>
์ •์ •๋Œ€์ƒ์ ‘์ˆ˜๋ฒˆํ˜ธ in (:์ ‘์ˆ˜๋ฒˆํ˜ธ, lpad(' ', 14))
and ๊ธˆ๊ฐ์›์ ‘์ˆ˜๋ฒˆํ˜ธ = decode(์ •์ •๋Œ€์ƒ์ ‘์ˆ˜๋ฒˆํ˜ธ, lpad(' ', 14), :์ ‘์ˆ˜๋ฒˆํ˜ธ, ๊ธˆ๊ฐ์›์ ‘์ˆ˜๋ฒˆํ˜ธ) 
=> ์ ‘์ˆ˜์ •๋ณดํŒŒ์ผ_X01 Index Range Scan

๋ฐ์ดํ„ฐํƒ€์ž… ์šฐ์„ ์ˆœ์œ„

์˜ˆ์ œ1

select * from EMP WHERE 1=1 AND empno || '' = 7900;
select *  from table(dbms_xplan.display);

์˜ˆ์ œ2

explain plan for
SELECT * FROM EMP WHERE 1=1 AND empno = 7900;
SELECT * FROM table(dbms_xplan.display);

์˜ˆ์ œ3

explain plan for
SELECT * FROM EMP WHERE 1=1 AND empno = '7900';
SELECT * FROM table(dbms_xplan.display);

์ด์ฐฌํฌ (MarkiiimarK)
Never Stop Learning.