h3 참고자료
http://scidb.tistory.com/118
where col1 = 1 and col2 = 'A' and col3 = '나' and col4 = 'a'
where col1 = 1 and col2 = 'A' and col3 = '나' and col4 >= 'a'
where col1 = 1 and col2 = 'A' and col3 between '가' and '다' and col4 = 'a'
where col1 = 1 and col2 = 'A' and col3 between '가' and '다' and col4 between 'a' and 'b'
where col1 between 1 and 2 and col2 = 'A' and col3 = '나' and col4 = 'a'
SQL> select * from v$version where rownum <2;
BANNER
----------------------------------------------------------------
Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - Prod
SQL> CREATE TABLE 매물아파트매매 AS
2 SELECT ROUND4) DBMS_RANDOM.VALUE(1,22)) AS "해당층"
3 , 1000 + ROUND( DBMS_RANDOM.VALUE(1,20)) * 100 AS "평당가"
4 , '2010'||LPAD(CEIL(ROWNUM/10000), 2, '0') AS "입력일"
5 , 'A0101135'||ROUND(DBMS_RANDOM.VALUE(1000, 2000) ) AS "아파트시세코드"
6 , DECODE(ROUND( DBMS_RANDOM.VALUE(1,10) ),1,12,2,15,3,18,4,21,5,28,6,32,7,42,8,48,9,59,10,68) AS "평형"
7 , DECODE(ROUND( DBMS_RANDOM.VALUE(1,4) ),1,'A',2,'B',3,'C',4,'D') AS "평형타입"
8 , ROUND( DBMS_RANDOM.VALUE(1,4)) -1 AS "인터넷매물"
9 FROM DUAL
10 CONNECT BY LEVEL < 1000000;
테이블이 생성되었습니다.
SQL> desc 매물아파트매매;
이름 널? 유형
----------------------------------------------------------------- -------- ----------------
해당층 NUMBER
평당가 NUMBER
입력일 VARCHAR2(8)
아파트시세코드 VARCHAR2)8)
평형 NUMBER
평형타입 VARCHAR2(1)
인터넷매물 NUMBER
SQL> create index inx_매물아파트매매_01 on 매물아파트매매(아파트시세코드, 평형, 평형타입, 인터넷매물)
2 ;
인덱스가 생성되었습니다.
SQL> create index inx_매물아파트매매_02 on 매물아파트매매(인터넷매물, 아파트시세코드, 평형, 평형타입);
인덱스가 생성되었습니다.
SQL> exec dbms_stats.gather_table_stats(user, '매물아파트매매');
PL/SQL 처리가 정상적으로 완료되었습니다.
SQL> SELECT * FROM 매물아파트매매 WHERE ROWNUM < 10
2 ;
해당층 평당가 입력일 아파트시세코드 평형 인터넷매물
---------- ---------- -------- ------------------------------------------------ ---------- - -------
4 1200 201001 A01011351613 15 D 2
17 2300 201001 A01011351758 42 D 1
2 2900 201001 A01011351021 18 D 2
21 1900 201001 A01011351574 48 B 2
17 2100 201001 A01011351094 32 D 2
3 2500 201001 A01011351365 21 C 2
18 2500 201001 A01011351980 48 A 2
7 1600 201001 A01011351527 59 D 1
11 1500 201001 A01011351292 32 A 0
9 개의 행이 선택되었습니다.
SQL> SELECT P.SPID SERVER
2 FROM V$PROCESS P, V$SESSION S
3 WHERE P.ADDR = S.PADDR
4 AND S.AUDSID = USERENV('SESSIONID')
5 ;
SERVER
------------
5928
SQL> alter session set events '10046 trace name context forever, level 1';
세션이 변경되었습니다.
-- 실수로 넘버 타입을 바차로 했는데.. 왜 타죠 인덱스 정말신기하내..
select /*+ index(매물아파트매매 INX_매물아파트매매_01) */*
from 매물아파트매매
where 아파트시세코드='A01011351574'
and 평형 = '48'
and 평형타입 = 'A'
and 인터넷매물 between '1' and '2'
order by 입력일 desc
call count cpu elapsed disk query current rows
------- ------ -------- ---------- ---------- ---------- ---------- ----------
Parse 1 0.00 0.00 0 0 0 0
Execute 1 0.00 0.00 0 0 0 0
Fetch 3 0.00 0.00 0 19 0 16
------- ------ -------- ---------- ---------- ---------- ---------- ----------
total 5 0.00 0.00 0 19 0 16
Misses in library cache during parse: 1
Optimizer mode: ALL_ROWS
Parsing user id: SYS
Rows Row Source Operation
------- ---------------------------------------------------
16 SORT ORDER BY (cr=19 pr=0 pw=0 time=212 us)
16 TABLE ACCESS BY INDEX ROWID 매물아파트매매 (cr=19 pr=0 pw=0 time=197 us)
16 INDEX RANGE SCAN INX_매물아파트매매_01 (cr=3 pr=0 pw=0 time=64 us)(object id 52944)
--넘버 타입으로 한거임
select /*+ index(매물아파트매매 INX_매물아파트매매_01) */*
from 매물아파트매매
where 아파트시세코드='A01011351574'
and 평형 = 48
and 평형타입 = 'A'
and 인터넷매물 between 1 and 2
order by 입력일 desc
call count cpu elapsed disk query current rows
------- ------ -------- ---------- ---------- ---------- ---------- ----------
Parse 1 0.01 0.00 0 0 0 0
Execute 1 0.00 0.00 0 0 0 0
Fetch 3 0.00 0.00 0 19 0 16
------- ------ -------- ---------- ---------- ---------- ---------- ----------
total 5 0.01 0.00 0 19 0 16
Misses in library cache during parse: 1
Optimizer mode: ALL_ROWS
Parsing user id: SYS
Rows Row Source Operation
------- ---------------------------------------------------
16 SORT ORDER BY (cr=19 pr=0 pw=0 time=228 us)
16 TABLE ACCESS BY INDEX ROWID 매물아파트매매 (cr=19 pr=0 pw=0 time=229 us)
16 INDEX RANGE SCAN INX_매물아파트매매_01 (cr=3 pr=0 pw=0 time=79 us)(object id 52944)
select /*+ index(매물아파트매매 inx_매물아파트매매_02) */*
from 매물아파트매매
where 아파트시세코드='A01011351574'
and 평형 = 48
and 평형타입 = 'A'
and 인터넷매물 between 1 and 2
order by 입력일 desc
call count cpu elapsed disk query current rows
------- ------ -------- ---------- ---------- ---------- ---------- ----------
Parse 1 0.00 0.01 0 0 0 0
Execute 1 0.00 0.00 0 0 0 0
Fetch 3 0.00 0.03 3 23 0 16
------- ------ -------- ---------- ---------- ---------- ---------- ----------
total 5 0.00 0.04 3 23 0 16
Misses in library cache during parse: 1
Optimizer mode: ALL_ROWS
Parsing user id: SYS
Rows Row Source Operation
------- ---------------------------------------------------
16 SORT ORDER BY (cr=23 pr=3 pw=0 time=30389 us)
16 TABLE ACCESS BY INDEX ROWID 매물아파트매매 (cr=23 pr=3 pw=0 time=19884 us)
16 INDEX SKIP SCAN INX_매물아파트매매_02 (cr=7 pr=3 pw=0 time=19752 us)(object id 52945)
SQL> ALTER SESSION SET SQL_TRACE=FALSE;
세션이 변경되었습니다.
select /*+ index(매물아파트매매 inx_매물아파트매매_02) */*
from 매물아파트매매
where 인터넷매물 in (1 , 2)
and 아파트시세코드='A01011351574'
and 평형 = 48
and 평형타입 = 'A'
order by 입력일 desc
call count cpu elapsed disk query current rows
------- ------ -------- ---------- ---------- ---------- ---------- ----------
Parse 1 0.01 0.03 0 0 0 0
Execute 1 0.00 0.00 0 0 0 0
Fetch 3 0.00 0.00 0 22 0 16
------- ------ -------- ---------- ---------- ---------- ---------- ----------
total 5 0.01 0.03 0 22 0 16
Misses in library cache during parse: 1
Optimizer mode: ALL_ROWS
Parsing user id: SYS
Rows Row Source Operation
------- ---------------------------------------------------
16 SORT ORDER BY (cr=22 pr=0 pw=0 time=284 us)
16 INLIST ITERATOR (cr=22 pr=0 pw=0 time=259 us)
16 TABLE ACCESS BY INDEX ROWID 매물아파트매매 (cr=22 pr=0 pw=0 time=225 us)
16 INDEX RANGE SCAN INX_매물아파트매매_02 (cr=6 pr=0 pw=0 time=92 us)(object id 52945)
create table 통합코드 as
select level - 1 as "코드"
, 'CD064' as "코드구분"
from dual
connect by level < 5
테이블이 생성되었습니다.
SQL> select * from 코드
2 ;
코드 코드
----- ----------
CD064 0
CD064 1
CD064 2
CD064 3
select /*+ index(ordered use_nl(b)*/*
from 통합코드 a, 매물아파트매매 b
where a.코드구분 = 'CD064'
and a.코드 between 1 and 2
and b.인터넷매물 = a.코드
and b.아파트시세코드='A01011351574'
and b.평형 = 48
and b.평형타입 = 'A'
call count cpu elapsed disk query current rows
------- ------ -------- ---------- ---------- ---------- ---------- ----------
Parse 1 0.00 0.01 0 1 0 0
Execute 1 0.00 0.00 0 0 0 0
Fetch 3 0.00 0.00 0 29 0 16
------- ------ -------- ---------- ---------- ---------- ---------- ----------
total 5 0.00 0.01 0 30 0 16
Misses in library cache during parse: 1
Optimizer mode: ALL_ROWS
Parsing user id: SYS
Rows Row Source Operation
------- ---------------------------------------------------
16 TABLE ACCESS BY INDEX ROWID 매물아파트매매 (cr=29 pr=0 pw=0 time=68 us)
19 NESTED LOOPS (cr=13 pr=0 pw=0 time=794 us)
2 TABLE ACCESS FULL 통합코드 (cr=5 pr=0 pw=0 time=29 us)
16 INDEX RANGE SCAN INX_매물아파트매매_02 (cr=8 pr=0 pw=0 time=35 us)(object id 52945)
SQL> create table 월별고객별판매집계 as
2 select rownum 고객번호
3 , '2008'||lpad(ceil(rownum/100000),2,'0') 판매월
4 , decode(mod(rownum, 12), 1, 'A','B') 판매구분
5 , round(dbms_random.value(1000,100000), -2) 판매액
6 from dual
7 connect by level <= 1200000;
테이블이 생성되었습니다.
SQL> create index idx_월별고객별판매집계_01 on 월별고객별판매집계( 판매구분, 판매월);
인덱스가 생성되었습니다.
SQL> create index idx_월별고객별판매집계_02 on 월별고객별판매집계( 판매월, 판매구분);
인덱스가 생성되었습니다.
select /*+ index(a idx_월별고객별판매집계_01) */count(*)
from 월별고객별판매집계 a
where 판매구분 = 'A'
and 판매월 between '200801' and '200812'
call count cpu elapsed disk query current rows
------- ------ -------- ---------- ---------- ---------- ---------- ----------
Parse 1 0.00 0.11 0 3 0 0
Execute 1 0.00 0.00 0 0 0 0
Fetch 2 0.03 0.02 0 281 0 1
------- ------ -------- ---------- ---------- ---------- ---------- ----------
total 4 0.03 0.14 0 284 0 1
Misses in library cache during parse: 1
Optimizer mode: ALL_ROWS
Parsing user id: SYS
Rows Row Source Operation
------- ---------------------------------------------------
1 SORT AGGREGATE (cr=281 pr=0 pw=0 time=28693 us)
100000 INDEX RANGE SCAN IDX_월별고객별판매집계_01 (cr=281 pr=0 pw=0 time=20 us)(object id 52949)
select /*+ index(a idx_월별고객별판매집계_02) */count(*)
from 월별고객별판매집계 a
where 판매구분 = 'A'
and 판매월 between '200801' and '200812'
call count cpu elapsed disk query current rows
------- ------ -------- ---------- ---------- ---------- ---------- ----------
Parse 1 0.00 0.23 0 3 0 0
Execute 1 0.00 0.00 0 0 0 0
Fetch 2 1.01 12.19 3089 3090 0 1
------- ------ -------- ---------- ---------- ---------- ---------- ----------
total 4 1.01 12.43 3089 3093 0 1
Misses in library cache during parse: 1
Optimizer mode: ALL_ROWS
Parsing user id: SYS
Rows Row Source Operation
------- ---------------------------------------------------
1 SORT AGGREGATE (cr=3090 pr=3089 pw=0 time=4662268 us)
100000 INDEX RANGE SCAN IDX_월별고객별판매집계_02 (cr=3090 pr=3089 pw=0 time=216012 us)(object id 52950)
SQL> select /*+ index(a idx_월별고객별판매집계_01) */ count(*)
2 from 월별고객별판매집계 a
3 where 판매구분 = 'A'
4 ;
COUNT(*)
----------
100000
select /*+ index(a idx_월별고객별판매집계_02) */count(*)
from 월별고객별판매집계 a
where 판매구분 = 'A'
and 판매월 in ('200801','200802','200803','200804','200805','200806','200807','200808','200809','200810','200811','200812')
call count cpu elapsed disk query current rows
------- ------ -------- ---------- ---------- ---------- ---------- ----------
Parse 1 0.00 0.02 0 3 0 0
Execute 1 0.00 0.00 0 0 0 0
Fetch 2 0.06 0.22 8 314 0 1
------- ------ -------- ---------- ---------- ---------- ---------- ----------
total 4 0.06 0.24 8 317 0 1
Misses in library cache during parse: 1
Optimizer mode: ALL_ROWS
Parsing user id: SYS
Rows Row Source Operation
------- ---------------------------------------------------
1 SORT AGGREGATE (cr=314 pr=8 pw=0 time=221951 us)
100000 INLIST ITERATOR (cr=314 pr=8 pw=0 time=100018 us)
100000 INDEX RANGE SCAN IDX_월별고객별판매집계_02 (cr=314 pr=8 pw=0 time=163641 us)(object id 52950)
select /*+ index_ss(a idx_월별고객별판매집계_02) */count(*)
from 월별고객별판매집계 a
where 판매구분 = 'A'
and 판매월 between '200801' and '200812'
call count cpu elapsed disk query current rows
------- ------ -------- ---------- ---------- ---------- ---------- ----------
Parse 1 0.00 0.05 0 3 0 0
Execute 1 0.00 0.00 0 0 0 0
Fetch 2 0.14 0.16 0 300 0 1
------- ------ -------- ---------- ---------- ---------- ---------- ----------
total 4 0.14 0.21 0 303 0 1
Misses in library cache during parse: 1
Optimizer mode: ALL_ROWS
Parsing user id: SYS
Rows Row Source Operation
------- ---------------------------------------------------
1 SORT AGGREGATE (cr=300 pr=0 pw=0 time=160153 us)
100000 INDEX SKIP SCAN IDX_월별고객별판매집계_02 (cr=300 pr=0 pw=0 time=100068 us)(object id 52950)
--쿼리 1
select 고객ID, 상품명, 지역,...
from 가입상품
where 회사 = :com
and 지역 = :reg
and 상품명 like :prod||'%'
--쿼리 2
select 고객ID, 상품명, 지역,...
from 가입상품
where 회사 = :com
and 상품명 like :prod||'%'
select 고객ID, 상품명, 지역,...
from 가입상품
where 회사 = :com
and 지역 like :reg||'%'
and 상품명 like :prod||'%'
select 고객ID, 상품명, 지역,...
from 가입상품
where :reg is not null
and 회사 = :com
and 지역 = :reg
and 상품명 like :prod||'%'
union all
select 고객ID, 상품명, 지역,...
from 가입상품
where :reg is null
and 회사 = :com
and 상품명 like :prod||'%'
select 위치명
from (select decode(sign(b.우편번호코드-40000),1,'',b.지역명1||' '||
nvl(b.지역명2,'')||' '||a.gis_위치명 위치명
, sqrt(power((a.gis_위도-:승객위도)*111000,2)+
power((a.gis_정도-:승객경도)*88000,2) 거리
from gis데이터 a, 우편번호 b
where a.gis_위도 between :승객위도 - 1 and :승객위도 + 1
and a.gis_경도 between :승객경도 - 1 and :승객경도 + 1
and sysdate between a.gis_시작일자 and a.gis_종료일자
order by 거리)
where rownum <= 1
select decode(sign(b.우편번호코드-40000),1,'',b.지역명1||' '||
nvl(b.지역명2,'')||' '||a.gis_위치명 위치명
from (select 위치명
from (select gis_지역코드, gis_위치명
, sqrt(power((a.gis_위도-:승객위도)*111000,2)+
power((a.gis_정도-:승객경도)*88000,2) 거리
from gis데이터 a, 우편번호 b
where a.gis_위도 between :승객위도 - 0.05 and :승객위도 + 0.05
and a.gis_경도 between :승객경도 - 0.05 and :승객경도 + 0.05
and sysdate between a.gis_시작일자 and a.gis_종료일자
order by 거리)
where rownum <= 1
)
where b.우편번호 = a.gis_지역코드
and rownum <=1;
SQL> create table 도서
2 as
3 select rownum 도서번호
4 , '오라클 ' || dbms_random.string('u', 8) 도서명
5 , round(dbms_random.value(1000, 100000), -3) 가격
6 , dbms_random.string('l', 10) 저자
7 , dbms_random.string('u', 10) 출판사
8 , lpad(mod(rownum, 10), 4, '0') || dbms_random.string('l', 4) ISBN
9 from dual
10 connect by level <= 99989;
테이블이 생성되었습니다.
SQL> insert into 도서
2 select 99990 도서번호
3 , '오라클 성능 고도화 원리와 해법 01' 도서명
4 , round(dbms_random.value(1000, 100000), -3) 가격
5 , dbms_random.string('l', 10) 저자
6 , dbms_random.string('u', 10) 출판사
7 , lpad(mod(rownum, 10), 4, '0') || dbms_random.string('l', 4) ISBN
8 from dual;
1 개의 행이 만들어졌습니다.
SQL> insert into 도서
2 select 99990+rownum 도서번호
3 , '오라클 성능 고도화 원리와 해법 ' || lpad(rownum, 2, '0') 도서명
4 , round(dbms_random.value(1000, 100000), -3) 가격
5 , dbms_random.string('l', 10) 저자
6 , dbms_random.string('u', 10) 출판사
7 , lpad(mod(rownum, 10), 4, '0') || dbms_random.string('l', 4) ISBN
8 from dual
9 connect by level <= 10;
10 개의 행이 만들어졌습니다.
SQL> create index 도시명_idx on 도서(도서명);
인덱스가 생성되었습니다.
SQL> exec dbms_stats.gather_table_stats(user, '도서', no_invalidate=>false);
PL/SQL 처리가 정상적으로 완료되었습니다.
SQL> column 저자 format a10;
SQL> column 출판사 format a10;
SQL> column isbn format a10;
SQL> column 도서명 format a40;
SQL> select *
2 from (
3 select ROWID ID, rownum rnum, 도서번호, 도서명, 가격, 저자, 출판사, isbn
4 from (
5 select 도서번호, 도서명, 가격, 저자, 출판사, isbn
6 from 도서
7 where 도서명 like ''|| '%'
8 order by 도서명
9 )
10 where rownum <= 110
11 )
12 where rnum >= 91 --> 10 페이지만 출력
13 ;
ID RNUM 도서번호 도서명 가격 저자 출판사 ISBN
------------------ ---------- ---------- ---------------------------------------- ---------- -------
AAAM8uAABAAAVRCABQ 91 52588 오라클 AARUHCXP 77000 caqzwodvjr VCRANMNMXL 0008tnhr
AAAM8uAABAAAVUbAAW 92 78340 오라클 AARUXMGU 60000 rsyykewoya BOYFCUKLRY 0000bfwk
AAAM8uAABAAAVSbABz 93 63207 오라클 AASAKAWZ 70000 ikmqrunmnj PGAROTHVRW 0007sdja
AAAM8uAABAAAVKjAAq 94 13648 오라클 AASBAABO 67000 mxflzzqora IRGQZGFCZD 0008gnky
AAAM8uAABAAAVWgAAY 95 94166 오라클 AASBLLMF 85000 dvttxkrgqv IWYDRTFJRX 0006eogt
AAAM8uAABAAAVJaABP 96 4919 오라클 AASCTDUE 19000 gzinxiivit FZEBHZTVVY 0009qewf
AAAM8uAABAAAVOtAA8 97 34843 오라클 AASEFRCC 86000 pwnoeatzmd BZEBHQXGCX 0003xtfr
AAAM8uAABAAAVVFAAt 98 83361 오라클 AASEJESI 50000 techftlanq DUPFYSWZSU 0001abgt
AAAM8uAABAAAVWMAA4 99 91818 오라클 AASKAFFX 18000 xrcjrjmepx JMDLMFBROP 0008hsyc
--> AAAM8uAABAAAVT8ABJ 100 74703 오라클 AATJENQO 20000 obvttsexjo BZKZEEEOUY 0003uvwi
AAAM8uAABAAAVSzABR 101 66029 오라클 AATJKUKD 31000 csvlocoxef VOFWLYASHA 0009cech
ID RNUM 도서번호 도서명 가격 저자 출판사 ISBN
------------------ ---------- ---------- ---------------------------------------- ---------- -------
AAAM8uAABAAAVWjABr 102 94606 오라클 AATLLCYS 15000 xftntnsgip XAKRCRKRGW 0006ejqn
AAAM8uAABAAAVWiAAH 103 94387 오라클 AATOELMV 69000 rvgmjyzqko WWVYZANSZQ 0007vqov
AAAM8uAABAAAVPqAAB 104 42039 오라클 AATQWSTC 44000 ibzdkrlbrf FFRIDTCLAW 0009axbt
AAAM8uAABAAAVRLAAG 105 53585 오라클 AATREKHY 43000 fszvicsbvf WFIWMETFRJ 0005jxgi
AAAM8uAABAAAVNFAAw 106 22458 오라클 AAUGWQHM 89000 tgyfbkfoxu ELTCEKHZCU 0008ncvs
AAAM8uAABAAAVMjAAS 107 18383 오라클 AAULZZYZ 60000 jpizcoydmh UMKPSPAOSH 0003fnml
AAAM8uAABAAAVKlABh 108 13941 오라클 AAUNLSGM 8000 dtikovpmcn NUPEINJDTC 0001ycpm
AAAM8uAABAAAVJjAAw 109 5975 오라클 AAUWMDUC 77000 vgjktoltaj WPMJLJEOKS 0005jhbp
AAAM8uAABAAAVTlABw 110 72006 오라클 AAVGVZPN 57000 ynptbmapos DABGIFIFMR 0006ndgq
20 개의 행이 선택되었습니다.
explain plan for select *
from (
select /*+ index(도서 도시명_idx) */
rowid rid, 도서번호, 도서명, 가격, 저자, 출판사, isbn
from 도서
where 도서명 like '오라클' || '%'
and 도서명 = '오라클 AATJENQO' -- 이전 페이지에서 출력된 마지막 도서명
and rowid > 'AAAM8uAABAAAVT8ABJ' -- 이전 페이지에서 출력된 마지막 도서의 rowid
union all
select /*+ index(도서 도시명_idx) */
rowid rid, 도서번호, 도서명, 가격, 저자, 출판사, isbn
from 도서
where 도서명 like '오라클'|| '%'
and 도서명 > '오라클 AATJENQO' -- 이전 페이지에서 출력된 마지막 도서명
)
where rownum <= 10
;
RID 도서번호 도서명 가격 저자 출판사 ISBN
------------------ ---------- ---------------------------------------- ---------- ---------- -------
AAAM8uAABAAAVSzABR 66029 오라클 AATJKUKD 31000 csvlocoxef VOFWLYASHA 0009cech
AAAM8uAABAAAVWjABr 94606 오라클 AATLLCYS 15000 xftntnsgip XAKRCRKRGW 0006ejqn
AAAM8uAABAAAVWiAAH 94387 오라클 AATOELMV 69000 rvgmjyzqko WWVYZANSZQ 0007vqov
AAAM8uAABAAAVPqAAB 42039 오라클 AATQWSTC 44000 ibzdkrlbrf FFRIDTCLAW 0009axbt
AAAM8uAABAAAVRLAAG 53585 오라클 AATREKHY 43000 fszvicsbvf WFIWMETFRJ 0005jxgi
AAAM8uAABAAAVNFAAw 22458 오라클 AAUGWQHM 89000 tgyfbkfoxu ELTCEKHZCU 0008ncvs
AAAM8uAABAAAVMjAAS 18383 오라클 AAULZZYZ 60000 jpizcoydmh UMKPSPAOSH 0003fnml
AAAM8uAABAAAVKlABh 13941 오라클 AAUNLSGM 8000 dtikovpmcn NUPEINJDTC 0001ycpm
AAAM8uAABAAAVJjAAw 5975 오라클 AAUWMDUC 77000 vgjktoltaj WPMJLJEOKS 0005jhbp
AAAM8uAABAAAVTlABw 72006 오라클 AAVGVZPN 57000 ynptbmapos DABGIFIFMR 0006ndgq
10 개의 행이 선택되었습니다.
SQL> SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY);
PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------------------------------
Plan hash value: 2234641167
------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 10 | 80460 | 13 (0)| 00:00:01 |
|* 1 | COUNT STOPKEY | | | | | |
| 2 | VIEW | | 11 | 88506 | 13 (0)| 00:00:01 |
| 3 | UNION-ALL | | | | | |
| 4 | TABLE ACCESS BY INDEX ROWID| 도서 | 1 | 55 | 2 (0)| 00:00:01 |
|* 5 | INDEX RANGE SCAN | 도시명_I| 1 | | 1 (0)| 00:00:01 |
PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------------------------------
| 6 | TABLE ACCESS BY INDEX ROWID| 도서 | 10 | 550 | 11 (0)| 00:00:01 |
|* 7 | INDEX RANGE SCAN | 도시명_I| 99999 | | 2 (0)| 00:00:01 |
------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter(ROWNUM<=10)
5 - access("도서명"='오라클 AATJENQO')
filter(ROWID>'AAAM8uAABAAAVT8ABJ' AND "도서명" LIKE '오라클%')
7 - access("도서명" LIKE '오라클%')
PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------------------------------
filter("도서명" LIKE '오라클%' AND "도서명">'오라클 AATJENQO')
23 개의 행이 선택되었습니다.
explain plan for select *
from (
select /*+ index(도서 도시명_idx) */
rowid rid, 도서번호, 도서명, 가격, 저자, 출판사, isbn
from 도서
where 도서명 like '오라클' || '%'
and 도서명 = '오라클 AATJENQO' -- 이전 페이지에서 출력된 마지막 도서명
and rowid > 'AAAM8uAABAAAVT8ABJ' -- 이전 페이지에서 출력된 마지막 도서의 rowid
union all
select /*+ index(도서 도시명_idx) */
rowid rid, 도서번호, 도서명, 가격, 저자, 출판사, isbn
from 도서
where RTRIM(도서명) like '오라클'|| '%'
and 도서명 > '오라클 AATJENQO' -- 이전 페이지에서 출력된 마지막 도서명
)
where rownum <= 10
SQL> SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY);
PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------------------------------
Plan hash value: 2234641167
------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 10 | 80460 | 14 (0)| 00:00:01 |
|* 1 | COUNT STOPKEY | | | | | |
| 2 | VIEW | | 11 | 88506 | 14 (0)| 00:00:01 |
| 3 | UNION-ALL | | | | | |
| 4 | TABLE ACCESS BY INDEX ROWID| 도서 | 1 | 55 | 2 (0)| 00:00:01 |
|* 5 | INDEX RANGE SCAN | 도시명_I| 1 | | 1 (0)| 00:00:01 |
PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------------------------------
| 6 | TABLE ACCESS BY INDEX ROWID| 도서 | 10 | 550 | 12 (0)| 00:00:01 |
|* 7 | INDEX RANGE SCAN | 도시명_I| 5000 | | 2 (0)| 00:00:01 |
------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter(ROWNUM<=10)
5 - access("도서명"='오라클 AATJENQO')
filter(ROWID>'AAAM8uAABAAAVT8ABJ' AND "도서명" LIKE '오라클%')
7 - access("도서명">'오라클 AATJENQO')
PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------------------------------
filter(RTRIM("도서명") LIKE '오라클%')
23 개의 행이 선택되었습니다.
select /*+ index(도서 도시명_idx) use_concat ordered_predicates */
rowid rid, 도서번호, 도서명, 가격, 저자, 출판사, isbn
from 도서
where 도서명 like '오라클' || '%'
and ((도서명 > '오라클 AATJENQO')
or
(도서명 = '오라클 AATJENQO' and rowid > 'AAAM8uAABAAAVT8ABJ') )
and rownum <= 10
;
select /*+ index(도서 도시명_idx) */
rowid rid, 도서번호, 도서명, 가격, 저자, 출판사, isbn
from 도서
where 도서명 like :book_nm || '%'
and 도서명 >= :last_book_nm
and lpad(도서명, 50) || rowid > lpad(:last_book_nm, 50) || :last_rid
and rownum <= 10
lpad(도서명, 50) || rowidtochar(rowid) > lpad(:last_book_nm, 50) || :last_rid
select greatest('b', 'a') from dual;
SQL>
SQL> select greatest('AAAH+WAAJAAAHxTAA9', 'AAAH+WAAJAAAHxTAA+') from dual;
GREATEST('AAAH+WAA
------------------
AAAH+WAAJAAAHxTAA9
SQL> select greatest( chartorowid('AAAH+WAAJAAAHxTAA9')
2 , chartorowid('AAAH+WAAJAAAHxTAA+') ) from dual;
GREATEST(CHARTOROW
------------------
AAAH+WAAJAAAHxTAA+
SQL>
select /*+ index(도서 도시명_idx) */
rowid rid, 도서번호, 도서명, 가격, 저자, 출판사, isbn
from 도서
where 도서명 like '오라클' || '%'
and 도서명 = '오라클 AATJENQO'
and rowid > 'AAAM8uAABAAAVT8ABJ'
create index 도시명_idx02 on 도서(도서명);
select /*+ index(도서 도시명_idx) */
rowid rid, 도서번호, 도서명, 가격, 저자, 출판사, isbn
from 도서
where 도서명 like '오라클' || '%'
and 도서명 = '오라클 AATJENQO'
and 도서번호 > 74703
SQL> create table t as select * from all_objects ;
SQL> insert into t select * from t;
SQL> insert into t select * from t;
SQL> insert into t select * from t;
SQL> insert into t select * from t;
SQL> select count(*) from t;
COUNT(*)
----------
799200
SQL> update t set object_id = rownum;
SQL> create index t_owner_idx on t(owner);
SQL> select /*+ index(t t_owner_idx) */ rowid
2 from t
3 where owner = 'SYS'
4 and rowid >= chartorowid('AAAM7MAABAAAUc5AAF')
5 and rownum <= 5 ;
ROWID
------------------
AAAM7MAABAAAUc5AAF
AAAM7MAABAAAUc5AAG
AAAM7MAABAAAUc5AAH
AAAM7MAABAAAUc5AAI
AAAM7MAABAAAUc5AAJ
Statistics
-------------------------------------------------------
0 recursive calls
0 db block gets
772 consistent gets <--
0 physical reads
0 redo size
554 bytes sent via SQL*Net to client
400 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
5 rows processed
SQL> set autotrace off
SQL> create index t_owner_idx2 on t(owner, object_id);
인덱스가 생성되었습니다.
SQL> select * from (
2 select object_id oid from t where owner = 'SYS' order by rowid desc
3 ) where rownum <= 5 ;
OID
----------
799200
799199
799198
799197
799196
SQL> set autotrace on statistics
SQL> select /*+ index(t t_owner_idx2) */ object_id
2 from t
3 where owner = 'SYS'
4 and object_id >= '799196'
5 and rownum <= 5 ;
OBJECT_ID
----------
799196
799197
799198
799199
799200
Statistics
----------------------------------------------------------
8 recursive calls
0 db block gets
154 consistent gets <--
0 physical reads
0 redo size
497 bytes sent via SQL*Net to client
400 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
5 rows processed
--select max(고객번호) from 월별고객별판매집계
SQL> insert into 월별고객별판매집계(고객번호, 판매월, 판매구분, 판매액)
2 select 1200000 + rownum as 고객번호
3 , decode(판매월, '200801', '200901', '200802', '200902') as 판매월
4 , 판매구분
5 , 판매액
6 from 월별고객별판매집계
7 where 판매월 between '200801' and '200802';
SQL> set autotrace on statistics
-- 쿼리 1
SQL> select /*+ index(t idx_월별고객별판매집계_02) */ count(*)
2 from 월별고객별판매집계 t
3 where 판매월 between '200901' and '200902'
4 and 판매구분 = 'A';
COUNT(*)
----------
16667
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
86 consistent gets <--
0 physical reads
0 redo size
426 bytes sent via SQL*Net to client
400 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
-- 쿼리 2
SQL> select /*+ index(t idx_월별고객별판매집계_02) */count(*)
2 from 월별고객별판매집계 t
3 where 판매월 like '2009%'
4 and 판매구분 = 'A';
COUNT(*)
----------
16667
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
88 consistent gets <--
0 physical reads
0 redo size
426 bytes sent via SQL*Net to client
400 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
-- 쿼리 3
SQL> select /*+ index(t idx_월별고객별판매집계_02) */ count(*)
2 from 월별고객별판매집계 t
3 where 판매월 >= '200901'
4 and 판매월 < '200903'
5 and 판매구분 = 'A';
COUNT(*)
----------
16667
Statistics
----------------------------------------------------------
1 recursive calls
0 db block gets
88 consistent gets
0 physical reads
0 redo size
426 bytes sent via SQL*Net to client
400 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
SQL> set autotrace off statistics;
SQL> create table 월별고객별판매집계2 as
2 select rownum 고객번호
3 , '2008'||lpad(ceil(rownum/100000),2,'0') 판매월
4 , decode(mod(rownum, 12), 1, 'B','A') 판매구분
5 , round(dbms_random.value(1000,100000), -2) 판매액
6 from dual
7 connect by level <= 1200000;
SQL> insert into 월별고객별판매집계2(고객번호, 판매월, 판매구분, 판매액)
2 select 1200000 + rownum as 고객번호
3 , decode(판매월, '200801', '200901', '200802', '200902') as 판매월
4 , 판매구분
5 , 판매액
6 from 월별고객별판매집계2
7 where 판매월 between '200801' and '200802';
200000 개의 행이 만들어졌습니다.
SQL> create index idx_월별고객별판매집계2_02 on 월별고객별판매집계2( 판매월, 판매구분);
--커리 4
SQL> select /*+ index(t idx_월별고객별판매집계2_02) */ count(*)
2 from 월별고객별판매집계2 t
3 where 판매월 between '200901' and '200902'
4 and 판매구분 = 'B';
COUNT(*)
----------
16667
Statistics
----------------------------------------------------------
5 recursive calls
0 db block gets
384 consistent gets
304 physical reads
0 redo size
426 bytes sent via SQL*Net to client
400 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
--커리 5
SQL> select /*+ index(t idx_월별고객별판매집계2_02) */count(*)
2 from 월별고객별판매집계2 t
3 where 판매월 like '2009%'
4 and 판매구분 = 'B';
COUNT(*)
----------
16667
Statistics
----------------------------------------------------------
5 recursive calls
0 db block gets
639 consistent gets
255 physical reads
0 redo size
426 bytes sent via SQL*Net to client
400 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
--커리 6
SQL> select /*+ index(t idx_월별고객별판매집계2_02) */ count(*)
2 from 월별고객별판매집계2 t
3 where 판매월 between '200900' and '200902'
4 and 판매구분 = 'B';
COUNT(*)
----------
16667
Statistics
----------------------------------------------------------
5 recursive calls
0 db block gets
639 consistent gets
0 physical reads
0 redo size
426 bytes sent via SQL*Net to client
400 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
--커리 7 결과 집합이 다름
select /*+ index(t idx_월별고객별판매집계2_02) */count(*)
from 월별고객별판매집계2 t
where 판매월 like '200901%'
and 판매구분 = 'B';
--커리 8 결과 집합이 다름
select /*+ index(t idx_월별고객별판매집계2_02) */count(*)
from 월별고객별판매집계2 t
where 판매월 = '200901'
and 판매구분 = 'B';
--커리 9
SQL> select /*+ index(t idx_월별고객별판매집계_02) */ count(*)
2 from 월별고객별판매집계 t
3 where 판매월 between '200901' and '200902'
4 and 판매구분 = 'B';
COUNT(*)
----------
183333
Statistics
----------------------------------------------------------
1 recursive calls
0 db block gets
465 consistent gets
0 physical reads
0 redo size
426 bytes sent via SQL*Net to client
400 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
--커리 10
SQL> select /*+ index(t idx_월별고객별판매집계_02) */count(*)
2 from 월별고객별판매집계 t
3 where 판매월 like '2009%'
4 and 판매구분 = 'B';
COUNT(*)
----------
183333
Statistics
----------------------------------------------------------
1 recursive calls
0 db block gets
466 consistent gets
0 physical reads
0 redo size
426 bytes sent via SQL*Net to client
400 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
--커리 11
SQL> select /*+ index(t idx_월별고객별판매집계_02) */ count(*)
2 from 월별고객별판매집계 t
3 where 판매월 between '200900' and '200902'
4 and 판매구분 = 'B';
COUNT(*)
----------
183333
Statistics
----------------------------------------------------------
1 recursive calls
0 db block gets
466 consistent gets
0 physical reads
0 redo size
426 bytes sent via SQL*Net to client
400 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
--선분이력
select a.고객번호, a.고객명, b.연락처, b.주소, b.연체금액, b.연체개월수
from 고객 a, 고객별연체금액 b
where a.고객번호 = '123'
and b.고객번호 = a.고객번호
and '20090505' between b.시작일 and b.종료일
--점이력
select a.고객번호, a.고객명, b.연락처, b.주소, b.연체금액, b.연체개월수
from 고객 a, 고객별연체금액 b
where a.고객번호 = '123'
and b.고객번호 = a.고객번호
and b.연체변경일자 = (select max(연체변경일자)
from 고객별연체금액
where 고객번호 = a.고객번호
and 변경일자 <= '20090505'
)
select 연체개월수, 연체금액
form 고객별연체금액
where 고객번호 = '123'
and :dt between 시작일 and 종료일
--현시점을 조회 맨마지막 이력 레코드는 종료일에 '99991231' 넣는다는 약속
select 연체개월수, 연체금액
form 고객별연체금액
where 고객번호 = '123'
and 종료일 = '99991231'
--선분이력 테이블에 정보를 미리 입력해 두는 경우
select 연체개월수, 연체금액
form 고객별연체금액
where 고객번호 = '123'
and to_char(sysdate, 'yyyymmdd') between 시작일 and 종료일
select *
from 고객별연체금액
where 고객번호 = '123'
and '20050131' between 시작일 and 종료일
--최종 레코드
{code:sql}
select /*+ index_desc(a idx_x01)*
from 고객별연체금액 a
where 고객번호 = '123'
and '20050131' between 시작일 and 종료일
and ronum <= 1
select *
from 고객별연체금액
where 고객번호 = '123'
and 시작년 = substr('20050131',1,4 )
and '20050131' between 시작일 and 종료일
--최종 레코드
{code:sql}
select /*+ index_desc(a idx_x02)*
from 고객별연체금액 a
where 고객번호 = '123'
and 시작년 = substr('20050131',1,4 )
and '20050131' between 시작일 and 종료일
and ronum <= 1
select /*+ index_desc(a idx_x01)*
from 고객별연체금액 a
where 고객번호 = '123'
and '20020930' between 시작일 and 종료일
and ronum <= 1
select *
from 고객별연체금액 a
where 고객번호 = '123'
and '20050131' between 시작일 and 종료일
and ronum <= 1
select *
from 고객별연체금액 a
where 고객번호 = '123'
and '20020930' between 시작일 and 종료일
and ronum <= 1
-- 고객 + 시작일 + 종료일
select /*+ index_desc( a idx_x01)*/ *
from 고객별연체금액 a
where 고객번호 = '123'
and '20031010' between 시작일 and 종료일
and ronum <= 1
-- 고객 + 종료일 + 과거일
select *
from 고객별연체금액 a
where 고객번호 = '123'
and '20031010' between 시작일 and 종료일
and ronum <= 1
SQL> create index emp_x01 on emp(deptno, job, sal, ename, mgr, comm);
인덱스가 생성되었습니다.
SQL> set autotrace traceonly explain;
SQL>
SQL> select /*+ ordered use_nl(e) index(e emp_x01) */ *
2 from dept d, emp e
3 where d.loc = 'CHICAGO'
4 and e.deptno = d.deptno
5 and e.job like 'SALE%'
6 and e.job between 'A' and 'Z'
7 and e.sal >= 1000
8 and e.ename like '%A%'
9 and trim(e.ename) = 'ALLEN'
10 and e.comm >= 300
11 and to_char(e.hiredate, 'yyyymmdd') like '198102%' ;
Execution Plan
----------------------------------------------------------
Plan hash value: 346692358
---------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 57 | 5 (0)| 00:00:01 |
|* 1 | TABLE ACCESS BY INDEX ROWID| EMP | 1 | 37 | 2 (0)| 00:00:01 |
| 2 | NESTED LOOPS | | 1 | 57 | 5 (0)| 00:00:01 |
|* 3 | TABLE ACCESS FULL | DEPT | 1 | 20 | 3 (0)| 00:00:01 |
|* 4 | INDEX RANGE SCAN | EMP_X01 | 1 | | 1 (0)| 00:00:01 |
---------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter(TO_CHAR(INTERNAL_FUNCTION("E"."HIREDATE"),'yyyymmdd') LIKE '198102%')
3 - filter("D"."LOC"='CHICAGO')
4 - access("E"."DEPTNO"="D"."DEPTNO" AND "E"."JOB" LIKE 'SALE%' AND
"E"."SAL">=1000 AND "E"."COMM">=300 AND "E"."SAL" IS NOT NULL)
filter("E"."ENAME" LIKE '%A%' AND "E"."JOB" LIKE 'SALE%' AND TRIM("E"."ENAME")='ALLEN' AND "E"."JOB">='A' AND
"E"."JOB"<='Z' AND
"E"."SAL">=1000 AND "E"."COMM">=300)
SQL>
SQL> create table t as select rownum no from dual connect by level <= 1000000 ;
테이블이 생성되었습니다.
SQL> create index t_idx on t(no) pctfree 0;
인덱스가 생성되었습니다.
SQL> delete from t where no <= 500000 ;
500000 행이 삭제되었습니다.
SQL> commit;
커밋이 완료되었습니다.
SQL> set autotrace on statistics
SQL> select * from t where no > 0 and rownum <= 1;
NO
----------
500001
Statistics
----------------------------------------------------------
15 recursive calls
0 db block gets
3159 consistent gets <-- delete 직후
5 physical reads
0 redo size
420 bytes sent via SQL*Net to client
400 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
SQL>
delete from 일별고객판매집계 where 판매일시 < trunc(sysdate) - 2;
insert into 일별고객판매집계
select to_char(sysdate, 'yyyymmdd'), 고객번호, sum(판매량), sum(판매금액);
from 판매
where 판매일시 between trunc(sysdate) and trunc(sysdate+1) -1/24/60/60
group by 고객번호;
commit;
!1-53.JPG!
{code:sql}
delete from 일별고객판매집계 where 판매일시 < trunc(sysdate) - 2;
commit;
insert into 일별고객판매집계
select to_char(sysdate, 'yyyymmdd'), 고객번호, sum(판매량), sum(판매금액);
from 판매
where 판매일시 between trunc(sysdate) and trunc(sysdate+1) -1/24/60/60
group by 고객번호;
commit;
!1-53.JPG!
{code:sql}
insert into 일별고객판매집계
select to_char(sysdate, 'yyyymmdd'), 고객번호, sum(판매량), sum(판매금액);
from 판매
where 판매일시 between trunc(sysdate) and trunc(sysdate+1) -1/24/60/60
group by 고객번호;
commit;
delete from 일별고객판매집계 where 판매일시 < trunc(sysdate) - 2;
commit;
SQL> create table t as select rownum no from dual connect by level <= 1000000 ;
SQL> create index t_idx on t(no) pctfree 0 ;
SQL> select /*+ index(t) */ count(*) from t where no > 0;
COUNT(*)
----------
1000000
Statistics
----------------------------------------------------------
32 recursive calls
0 db block gets
2083 consistent gets
2000 physical reads
0 redo size
424 bytes sent via SQL*Net to client
400 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
SQL> delete from t where mod(no, 10) < 5 ;
SQL> commit;
SQL> select /*+ index(t) */ count(*) from t where no > 0;
COUNT(*)
----------
500000
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
2001 consistent gets
0 physical reads
0 redo size
424 bytes sent via SQL*Net to client
400 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
SQL>
SQL> alter index t_idx coalesce;
SQL>
SQL> select /*+ index(t) */ count(*) from t where no > 0;
COUNT(*)
----------
500000
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
1002 consistent gets
0 physical reads
0 redo size
424 bytes sent via SQL*Net to client
400 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
alter index t_idx shrink space
-- coalsece 같은 명령어됨..
alter index t_idx shrink space compact
--coalesce나 shrink는 레코드를 건건이 지워다가 다시 입력하는 방식을 사용하므로 작업량이 많을때는 rebuild명령을 사용하는 편이 나을수도있다.
alter index t_idx rebuild;
alter index t_idx rebuild online;