[Oracle] DB reorg 작업1 (table move)

·2025년 9월 16일
0

오라클 관리

목록 보기
151/163

💡 DB reorg 작업이란?

  • 테이블과 인덱스를 다시 구성하면서 성능을 높이고 저장공간도 확보하는 작업
    (방청소와 비슷)
  • dba가 한달에 한번씩 수행

💡 DB reorg 작업 후 얻을 수 있는 효과 2가지

  1. 성능 향상
  2. 공간 확보

💡 DB reorg 작업순서

  1. HWM(High Water Mark)를 아래로 내림
  2. ROW Migration 현상이 일어난 row들을 정리
  3. 여유공간이 확보됨

💡 DB reorg 작업방법 3가지

  1. table export ----> table drop -------> table import (요즘 이렇게 안함)
  2. table을 다른 테이블스페이스로 move 하거나 같은 테이블 스페이스로 move (대부분 이렇게 함)
  3. table compack ---------> shrink (큰 기업들과 최근에 배운 사람들이 하는 방법)

[실습1] 다음과 같이 db reorg 작업을 수행합니다.


■  테이블을 move 하는 것으로 db reorg 하기 

  1. scott 유져에서 emp 테이블의 HWM 를 높인다. 

  SQL> insert  into emp
         select  *
           from emp;
  
  SQL>   /  엔터  <---------  10번 수행 
  SQL>  commit;
  SQL>  delete  from  emp  where  deptno in (10,20);
  SQL>  commit; 

 2. emp 테이블의 HWM 가 어떻게 되는지 확인한다.

 select  count(distinct dbms_rowid.rowid_block_number(rowid) ) blocks
    from  emp;

         169       <---- High Water Mark 까지의 블럭의 갯수

 3. emp 테이블이 어느 테이블스페이스에 있는지 조회하시오 !

   select  table_name, tablespace_name
     from  user_tables
     where  table_name='EMP';

   create  tablespace  ts101
      datafile  '/home/oracle/ts101.dbf' size 50m;

 4. emp 테이블을 ts100 테이블스페이스로 move 하시오 !

   alter  table  emp   move   tablespace  ts101;
  

 5. emp 테이블이 다른 테이블스페이스로 이동했는지 확인한다.

   select  table_name, tablespace_name
     from  user_tables
     where  table_name='EMP';

 6. High Water Mark 가 아래로 내려왔는지 확인하시오 !

 select  count(distinct dbms_rowid.rowid_block_number(rowid) ) blocks
    from  emp;
    
    76

문제1. 아래의 환경을 만들고 db reorg 작업을 하시오

0. 환경 만들기
   
   @demo.sql
   create  index  emp_empno  on  emp(empno);
   create  index  emp_ename  on  emp(ename);
   create  index  emp_sal    on  emp(sal);
   create  index  emp_job    on  emp(job);
   create  index  emp_detpno on  emp(deptno);

   select  index_name, status
      from  user_indexes
      where  table_name='EMP';

1. scott 유져에서 emp 테이블의 HWM 를 높인다. 

  SQL> insert  into emp
         select  *
           from emp;
  
  SQL>   /  엔터  <---------  10번 수행 
  SQL>  commit;
  SQL>  delete  from  emp  where  deptno in (10,20);
  SQL>  commit; 

문제2. db reorg 작업을 위와 같이하고 아래의 SQL을 수행하시오

select /*+ index(emp emp_empno) */ empno, ename,sal
 from emp
 where empno = 7499; 
 
 select /*+ full(emp) */ empno, ename,sal
 from emp
 where empno = 7499; 

💡 테이블을 다른 테이블스페이스로 move 했으면 관련된 인덱스들을 모두 rebuild 해줘야됨


문제3. emp 와 관련된 인덱스들을 모두 rebuild 하시오

select index_name, status
from user_indexes
where table_name='EMP';

select 'alter index ' || index_name || ' rebuild online;'
 from user_indexes
 where table_name='EMP' and status='UNUSABLE';
 
alter index EMP_EMPNO rebuild online;
alter index EMP_ENAME rebuild online;
alter index EMP_SAL rebuild online;
alter index EMP_JOB rebuild online;
alter index EMP_DETPNO rebuild online;

select /*+ index(emp emp_empno) */ empno, ename,sal
 from emp
 where empno = 7499; 

문제1. HR 계정에 employees 테이블의 인덱스가 뭐가있는지 조회하시오

select index_name, status
from user_indexes
where table_name='EMPLOYEES';

문제2. HR 계정의 employees 테이블이 어느 테이블 스페이스에 있는지 확인하시오

select table_name, tablespace_name
 from  user_tables
 where table_name='EMPLOYEES';

문제3. HR 계정의 employees 테이블을 users 테이블 스페이스로 옮기시오

alter table employees move tablespace users;

문제4. HR계정의 employees 테이블과 관련 unusuable된 인덱스들을 rebuild 하시오

select index_name, status
 from  user_indexes
 where table_name='EMPLOYEES';

 
select 'alter index  ' || index_name || ' rebuild online;'
 from  user_indexes
 where table_name='EMPLOYEES'  and status='UNUSABLE';
 
alter index  EMP_EMAIL_UK rebuild online;
alter index  EMP_EMP_ID_PK rebuild online;
alter index  EMP_DEPARTMENT_IX rebuild online;
alter index  EMP_JOB_IX rebuild online;
alter index  EMP_MANAGER_IX rebuild online;
alter index  EMP_NAME_IX rebuild online;

0개의 댓글