CREATE TABLE DOCUMENTS (
DOC_ID VARCHAR2(5),
TITLE VARCHAR2(50),
AUTHOR VARCHAR2(20),
CONTENTS VARCHAR2(2000),
STATUS VARCHAR2(2),
CONSTRAINT PK_DOC PRIMARYKEY (DOC_ID)
)
ORGANIZATION INDEX ---(A)
TABLESPACE DATA01 ---(B)
PCTTHRESHOLD 20 ---(C)
INCLUDING CONTENTS ---(D)
OVERFLOW TABLESPACE IDX01 ---(E)
;
1) 인덱스 일체형 테이블 생성
CREATE TABLE T (
A INT
,B INT
,PRIMARY KEY (A,B)
)
ORGANIZATION INDEX
2) 보조인덱스 생성
CREATE INDEX T_IDX ON T(B)
3)쿼리문 실행
SQL> SET AUTOTRACE TRACEONLY EXPLAIN
SQL> SELECT A,B FROM T WHERE B=55;
Execution Plan
----------------------------------------------------------
0 SELECT STATEMENT Optimizer=CHOOSE (Cost=1 Card=1 Bytes=26)
1 0 INDEX (RANGE SCAN) OF 'T_IDX' (NON-UNIQUE) (Cost=1 Card=1Bytes=26)
4) 해설
B칼럼에 설정된 T_IDX 인덱스가 사용되었고 SELECT절에 A칼럼에 대한 요청이 있슴에도 불구하고
[TABLE ACCESS BY ROWID]에 의한 테이블 엑세스가 나타나지 않고 있다.
이는 인덱스구조의 논리적인 ROWID로 부터 칼럼 A의 값을 얻을 수 있다는 사실을 알고 있기에 테이블 엑세스를 하지 않은 것이다.
이처럼 보조 인덱스를 생성하여 디스크 공간을 절약할 수 있다.
5) 만일 분리형 테이블이었다면 다음과 같이 테이블을 생성해야 테이블 엑세스가 발생하지 않을 것이다.
CREATE TABLE T (
A INT
,B INT
,PRIMARY KEY (A,B)
)
/*T_IDX 인덱스를 B,A 두개의 칼럼에 만든다*/
CREATE INDEX T_IDX ON T(B,A)
SQL> SELECT A,B FROM T WHERE B=55;
Execution Plan
----------------------------------------------------------
0 SELECT STATEMENT Optimizer=CHOOSE
1 0 INDEX (RANGE SCAN) OF 'T_IDX' (NON-UNIQUE)
1.테이블 생성
CREATE TABLE IOT (
USERNAME VARCHAR2(30),
DOCUMENT_NAME VARCHAR2(30),
OTHER_DATA CHAR(100),
CONSTRAINT IOT_PK PRIMARY KEY (USERNAME,DOCUMENT_NAME)
)
ORGANIZATION INDEX
CREATE TABLE HEAP (
USERNAME VARCHAR2(30),
DOCUMENT_NAME VARCHAR2(30),
OTHER_DATA CHAR(100),
CONSTRAINT HEAP_PK PRIMARY KEY (USERNAME,DOCUMENT_NAME)
)
2.데이터입력
BEGIN
FOR I IN 1..100
LOOP
FOR X IN (SELECT USERNAME FROM ALL_USERS)
LOOP
INSERT INTO HEAP (USERNAME,DOCUMENT_NAME,OTHER_DATA)
VALUES
(X.USERNAME,X.USERNAME||'_'||I,'X');
INSERT INTO IOT (USERNAME,DOCUMENT_NAME,OTHER_DATA)
VALUES
(X.USERNAME,X.USERNAME||'_'||I,'X');
END LOOP;
END LOOP;
COMMIT;
END;
/
3.데이터출력
DECLARE
TYPE T_ARRAY IS TABLE OF VARCHAR2(100);
ARRAY1 T_ARRAY;
ARRAY2 T_ARRAY;
ARRAY3 T_ARRAY;
BEGIN
FOR I IN 1..10
LOOP
FOR X IN (SELECT USERNAME FROM ALL_USERS)
LOOP
FOR Y IN (SELECT * FROM HEAP SINGLE_ROW WHERE USERNAME=X.USERNAME)
LOOP
NULL;
END LOOP;
FOR Y IN (SELECT * FROM IOT SINGLE_ROW WHERE USERNAME=X.USERNAME)
LOOP
NULL;
END LOOP;
SELECT * BULK COLLECT INTO ARRAY1,ARRAY2,ARRAY3
FROM HEAP BULK_COLLECT
WHERE USERNAME=X.USERNAME;
SELECT * BULK COLLECT INTO ARRAY1,ARRAY2,ARRAY3
FROM IOT BULK_COLLECT
WHERE USERNAME=X.USERNAME;
END LOOP;
END LOOP;
END;
/
4.TRACE 분석
1) HEAP SINGLE_ROW
SELECT * FROM HEAP SINGLE_ROW WHERE USERNAME=:b1
call count cpu elapsed disk query current rows
------- ------ -------- ---------- ---------- ---------- ---------- ----------
Parse 1 0.00 0.00 0 0 0 0
Execute 310 0.01 0.00 0 0 0 0
Fetch 31310 0.32 0.29 0 62610 0 31000
------- ------ -------- ---------- ---------- ---------- ---------- ----------
total 31621 0.34 0.30 0 62610 0 31000
Rows Execution Plan
------- ---------------------------------------------------
0 SELECT STATEMENT GOAL: CHOOSE
0 TABLE ACCESS (BY INDEX ROWID) OF 'HEAP'
0 INDEX (RANGE SCAN) OF 'HEAP_PK' (UNIQUE)
2) IOT SINGLE_ROW
SELECT * FROM IOT SINGLE_ROW WHERE USERNAME=:b1
call count cpu elapsed disk query current rows
------- ------ -------- ---------- ---------- ---------- ---------- ----------
Parse 1 0.00 0.00 0 0 0 0
Execute 310 0.00 0.00 0 0 0 0
Fetch 31310 0.26 0.26 0 31610 0 31000
------- ------ -------- ---------- ---------- ---------- ---------- ----------
total 31621 0.26 0.26 0 31610 0 31000
Rows Execution Plan
------- ---------------------------------------------------
0 SELECT STATEMENT GOAL: CHOOSE
0 INDEX (RANGE SCAN) OF 'IOT_PK' (UNIQUE)
3) HEAP BULK_COLLECT
SELECT * FROM HEAP BULK_COLLECT WHERE USERNAME=:b1
call count cpu elapsed disk query current rows
------- ------ -------- ---------- ---------- ---------- ---------- ----------
Parse 1 0.00 0.00 0 0 0 0
Execute 310 0.00 0.00 0 0 0 0
Fetch 310 0.15 0.13 0 19620 0 31000
------- ------ -------- ---------- ---------- ---------- ---------- ----------
total 621 0.15 0.14 0 19620 0 31000
Rows Execution Plan
------- ---------------------------------------------------
0 SELECT STATEMENT GOAL: CHOOSE
0 TABLE ACCESS (BY INDEX ROWID) OF 'HEAP'
0 INDEX (RANGE SCAN) OF 'HEAP_PK' (UNIQUE)
4) IOT BULK_COLLECT
SELECT * FROM IOT BULK_COLLECT WHERE USERNAME=:b1
call count cpu elapsed disk query current rows
------- ------ -------- ---------- ---------- ---------- ---------- ----------
Parse 1 0.00 0.00 0 0 0 0
Execute 310 0.00 0.00 0 0 0 0
Fetch 310 0.07 0.07 0 1500 0 31000
------- ------ -------- ---------- ---------- ---------- ---------- ----------
total 621 0.07 0.07 0 1500 0 31000
Rows Execution Plan
------- ---------------------------------------------------
0 SELECT STATEMENT GOAL: CHOOSE
0 INDEX (RANGE SCAN) OF 'IOT_PK' (UNIQUE)
- 강좌 URL : http://www.gurubee.net/lecture/3559
- 구루비 강좌는 개인의 학습용으로만 사용 할 수 있으며, 다른 웹 페이지에 게재할 경우에는 출처를 꼭 밝혀 주시면 고맙겠습니다.~^^
- 구루비 강좌는 서비스 제공을 위한 목적이나, 학원 홍보, 수익을 얻기 위한 용도로 사용 할 수 없습니다.