ORACLE

<색인(index)>

1.정의
테이블의 데이터를 보다 빠르게 검색할 수 있도록 지원해주는 객체


2. 구성요소

rowid, 색인 컬럼값

3. 생성
create index 인덱스명 on 테이블명(컬럼명...);

create index b_emp_name_idx
    on b_emp(name);


4.삭제
drop index 인덱스명;

drop index b_emp_name_idx;


create table emp3(
  no number,
  name varchar2(10),
  sal number
);

insert into emp3 VALUES (1, '홍길동', 300);
insert into emp3 VALUES (2, '김설희', 250);
insert into emp3 VALUES (3, '모병환', 430);
insert into emp3 VALUES (4, '도루묵', 220);
insert into emp3 VALUES (5, '나얼짱', 620);
  
commit;    

-- 인덱스 추가
create index idx_emp3_name on emp3(name);
-- 인덱스를 사용하지 않은 경우 (정렬이 수행되지 않음)
select name from emp3;

-- 인덱스를 사용하는 경우(검색을 하므로 인덱스를 사용한 정렬이 수행됨-실행계획 비교)

-- order by 사용하지 않아도 정렬이 된다.
select name from emp3 where name > '0';


=>출력

김설희
나얼짱
도루묵
모병환
홍길동


--13장 인덱스(index)
--p.640
create table test_auto (
id number not null,
name varchar2(10) not null
);

insert into test_auto values (10, '김철수');
insert into test_auto values (20, '홍길동');
insert into test_auto values (30, '박철수');
insert into test_auto values (40, '김기사');
insert into test_auto values (50, '이순신');

commit;

-- 인덱스를 사용하지 않은 경우
select * from test_auto;


=>

10    김철수
20    홍길동
30    박철수
40    김기사
50    이순신


-- name컬럼을 primary key 로 설정
create table test_auto (
id number not null,
name varchar2(10) not null,
constraint pk_test_auto_name primary key(name)
);


insert into test_auto values (10, '김철수');
insert into test_auto values (20, '홍길동');
insert into test_auto values (30, '박철수');
insert into test_auto values (40, '김기사');
insert into test_auto values (50, '이순신');

-- 데이터 사전 조회
select * from user_indexes where table_name='TEST_AUTO';


PK_TEST_AUTO_NAME    NORMAL    HR    TEST_AUTO    TABLE    UNIQUE    DISABLED        USERS    2    255    65536    1048576    1    2147483645                        10

-- 인덱스 추가
drop table test_auto;
commit;

create table test_auto (
id number not null,
name varchar2(10) not null
);

insert into test_auto values (10, '김철수');
insert into test_auto values (20, '홍길동');
insert into test_auto values (30, '박철수');
insert into test_auto values (40, '김기사');
insert into test_auto values (50, '이순신');

commit;

-- 인덱스
-- create index 인덱스 네임 on 테이블 (컬럼)
create index emp_name_idx on test_auto(name);


-- 데이터 사전 조회
select * from user_indexes where table_name='TEST_AUTO';


select * from test_auto;
select * from test_auto  where name > '0';

=>출력 결과

40    김기사
10    김철수
30    박철수
50    이순신
20    홍길동


-- PL/SQL
-- declare
--  변수 선언부
-- begin
-- SQL 명령어들
-- end; 
-- 변수 자료형 := 초기값
-- := 대입연산자
-- mod 나머지 연산자
-- A || B A와 B를 연결
-- to_char(숫자) 숫자를 문자열로

create table emp2(
i int ,
name varchar(20), 
sal int 
);
commit;

declare
  i int :=1;
  name varchar(20) := 'kim';
  sal int :=0;
  
begin
  while i < 1000000 loop
   if i mod 2 = 0 then
    name :='kim' || to_char(i);
    sal := 400;
    
   elsif i mod 3 =0 then
       name :='pack' || to_char(i);
       sal := 400;

   elsif i mod 5 =0 then
       name :='hong' || to_char(i);
       sal :=5400; 
   else
       name :='shin' || to_char(i);
       sal := 250;  
       end if;
       
   insert into emp2 values (i, name, sal);
   i :=i+1;  --i 의 값을 증가시킴
   
   end loop;
end;   

select * from emp2;

select * from emp2 where name='kim1240';

실행 시간 =>0.034초

=>결과
1240    kim1240    400

--인덱스 추가
create index emp2_name_idx on emp2(name);

select * from emp2 where name='kim1240';


실행 시간 =>0.002초

=>결과
1240    kim1240    400


-- table access full : full scan, 테이블 전체 검색
-- index range scan : 인덱스(색인)를 사용한 검색


 

 

 

 

 

 

 

 

 

 

 

 

 

about author

PHRASE

Level 60  머나먼나라

인간의 참된 가치는 그가 어느 정도까지 자기 자신에게서 해방될 수 있으며, 또 그가 자기 자신에게서 얻은 그 해방의 의미가 무엇인가에 의해서 주로 결정된다. -아인슈타인

댓글 ( 4)

댓글 남기기

작성