Optimizing Oracle Optimizer (2011년)
통계 정보 관리 0 0 4,807

by 구루비스터디 dbms_stats 통계정보 [2018.07.14]


통계정보 비교하기


=> object 생성, Data 생성, 통계정보수집

drop table t1 purge;
create table t1(c1 int, c2 int);

insert into t1
select mod(level,100), level
  from dual
connect by level <= 100;
commit;

exec dbms_stats.gather_table_stats ('HYUN','t1',CASCADE=>TRUE,method_opt =>'FOR COLUMNS c1 SIZE 254');
select * from  table(dbms_stats.diff_table_stats_in_stattab('HYUN','t1','t1_stat'));


=> 현재 통계정보를 EXPORT 한다
exec dbms_stats.create_stat_table('HYUN','t1_stat');

exec dbms_stats.export_table_stats('HYUN','t1',null,'t1_stat');


=> Data의 분포를 크게 변경한 후 통계정보를 수집한다. 
insert into t1
select mod(level,2), level
  from dual
connect by level <= 10000;
commit;

exec dbms_stats.gather_table_stats ('HYUN','t1',CASCADE=>TRUE,method_opt =>'FOR COLUMNS c1 SIZE 254');
select * from  table(dbms_stats.diff_table_stats_in_stattab('HYUN','t1','t1_stat'));



통계정보 Backup/Recovery

통계정보 백업용 테이블 생성 방법

dbms_stats.create_stat_table('owner', '테이블명','테이블스페이스명');
ex) exec dbms_stats.create_stat_table('HYUN', 'T1', 'HYUN');


통계정보 백업 방법
  • 백업의 단위는 database 전체, schema, table, index, column 단위로 수행할 수 있다
  • schema 단위
dbms_stats.export_schema_stats ('schema name', 'stat table','stat_id','stat table owner');
  • table 단위
dbms_stats.export_table_stats ('table_owner', 'table name', null, 'stat table','stat_id',true,'stat table owner');



ex) exec dbms_stats.export_table_stats ('hyun', 't1', null, 'stat_tab','0414',true,'hyun');
Statid 는 restore 시 통계정보 버전별 식별자 역할을 함.


통계정보 restore 방법
  • 백업의 단위나 syntax 가 export 와 동일함


dbms_stats.import_table_stats ('table_owner', 'table name', null, 'stat table','stat_id',true,'stat table owner');

ex) exec dbms_stats.import_table_stats ('inv', 'mtl_system_items_b', null, 'stat_tab','0414',true,'apps');


통계정보 백업 및 restore 절차
  • 통계정보 생성 전 항상 기존 통계정보 백업을 실시
  1. 통계정보 백업
  2. 통계정보 생성
  3. 튜닝용 통계정보 restore
  4. 이상 발생시 통계정보 restore


TEST

-- 통계정보수집테이블생성
BEGIN dbms_stats.create_stat_table('hyun', 'TB_STAT_ANAL','ts_d01');
END;
/

=> object 생성, Data 생성, 통계정보수집

drop table t1 purge;
create table t1(c1 int, c2 int);

insert into t1
select mod(level,100), level
  from dual
connect by level <= 100;
commit;

exec dbms_stats.gather_table_stats ('HYUN','t1',CASCADE=>TRUE,method_opt =>'FOR COLUMNS c1 SIZE 254');


=> 통계정보 확인 (약식)
select table_name,num_rows 
from tabs
where table_name = 'T1';

TABLE_NAME                       NUM_ROWS
------------------------------ ----------
T1                                    100


=> 현재 통계정보를 EXPORT 한다
BEGIN dbms_stats.export_schema_stats ('HYUN', 'TB_STAT_ANAL','V1','HYUN');
END;
/



=> Data의 분포를 크게 변경한 후 통계정보를 수집한다. 
insert into t1
select mod(level,2), level
  from dual
connect by level <= 10000;
commit;

exec dbms_stats.gather_table_stats ('HYUN','t1',CASCADE=>TRUE,method_opt =>'FOR COLUMNS c1 SIZE 254');


=> 통계정보 확인 (약식)
select table_name,num_rows 
from tabs
where table_name = 'T1';

TABLE_NAME                       NUM_ROWS
------------------------------ ----------
T1                                  10100

=> 예전통계정보 복구
exec dbms_stats.import_table_stats ('hyun', 't1', null, 'TB_STAT_ANAL','V1',true,'hyun');

=> 확인
TABLE_NAME                       NUM_ROWS
------------------------------ ----------
T1                                    100

"데이터베이스 스터디모임" 에서 2009년에 "OPTIMIZING ORACLE OPTIMIZER " 도서를 스터디하면서 정리한 내용 입니다.

- 강좌 URL : http://www.gurubee.net/lecture/3972

- 구루비 강좌는 개인의 학습용으로만 사용 할 수 있으며, 다른 웹 페이지에 게재할 경우에는 출처를 꼭 밝혀 주시면 고맙겠습니다.~^^

- 구루비 강좌는 서비스 제공을 위한 목적이나, 학원 홍보, 수익을 얻기 위한 용도로 사용 할 수 없습니다.

댓글등록
SQL문을 포맷에 맞게(깔끔하게) 등록하려면 code() 버튼을 클릭하여 작성 하시면 됩니다.
로그인 사용자만 댓글을 작성 할 수 있습니다. 로그인, 회원가입