[Oracle] View, Index

이혜원·2024년 8월 5일

Oracle

목록 보기
4/6

뷰(View)

하나의 가상 table이라고 할 수 있다.
table과는 달리 데이터를 저장하기 위한 물리적인 공간이 필요하지 않다. 데이터를 물리적으로 갖지 않지만 논리적인 집합을 갖고 있으며 table과 마찬가지로 select, insert, update, delete 명령이 가능하다.

자주 조회하는 데이터들을 뷰로 만들어 놓고 사용하면 편리하다.
또한, 데이터를 노출시키고 싶지 않을때 view를 사용하여 보여줄 데이터만 제공할 수 있어 보안에도 유리하다.

view 생성 ↴

create view v_emp(emp_id, first_name, job_id, dept_id)
as
select employee_id, first_name, job_id
from employee, department_id
;

view 조회 ↴
select * from 뷰이름;

view 삭제 ↴
drop view 뷰이름;


view 생성 예제문제 :
emp_id, name(ex. firstname lastname), dept_id, hire_date 를 출력하라. 단, 50번 부서의 사원만

create view v_exam(emp_id, name, dept_id, hire_date)
as
selcect employee_id, first_name || ' ' || last_name, department_id, hire_date
from employees
where department_id = 50
;

인덱스(index)

조회속도를 향상시키기 위한 데이터베이스 검색 기술

  • index 생성 원리
    index를 테이블의 특정 컬럼에 한 개 이상 주게 되면 index table이 따로 만들어진다.
    index 컬럼의 로우값*rowid값이 저장되며 로우값은 정렬된 트리 구조로 저장시켜 두었다가 검색시 좀 더 빠르게 해당 데이터를 찾는데 도움을 준다.
    // rowid : 테이블을 생성하고 컬럼을 만든 후 데이터를 삽입하면 하나의 row가 생성되며 이 row는 절대적인 주소를 가지게 되는데 이를 rowid라고 한다.

  • DML 명령을 사용할 때는 원본 table은 물론 index table에도 데이터를 갱신시켜주어야 하기 때문에 update, insert, delete 명령을 쓸 때 속도가 느려진다는 단점이 있다.

  • DML 구문과 index
    insert : 인덱스 split 현상으로 부하가 걸린다.
    delete : index에서는 데이터를 사용하지 않으므로 표시하고 지우지 않는다.
    update : index에서는 delete를 한 후, 새로운 데이터를 insert 작업한다.


  • index 생성 방법

< unique index >

create unique index 인덱스명
on 테이블명(컬럼);

< non-unique index >

create index 인덱스명
on 테이블명(컬럼);

index 예제

// 테스트를 위한 employees 테이블 복사 ↴

create table employees3 as select * from employees;

// employee_id가 200인 컬럼의 rowid 확인 방법 ↴

select rowid, employee_id
from employees3
where employee_id = 200

// unique index 생성 ↴

create unique index idx_emp_id
on employees3(employee_id);

// first_name이 'Kim'인 컬럼에 unique index 걸어주기 (*중복값이 존재할 시 index를 걸 수 없다.) ↴

delete employees3
where first_name = 'Kim'
;

// non-unique index

create index idx_emp_first
on employees3(first_name);

0개의 댓글