超詳細的Oracle數據庫表碎片整理規范,值得收藏
概述
生產環境中,經常會遇到表由于數據不斷插入,導致空間越來越大,由于前期配置問題,沒有做分區或者其他優化,而且生產數據實時向表插入。要刪除歷史數據來釋放空間。所以DBA一般都需要定期去對Oracle表碎片做整理,簡單整理表碎片整理流程如下:
1、定位存在碎片的對象
使用如下腳本,檢查需要進行碎片整理的對象:
- --all tables(partition_tables + non_partition_tables )
- select a.owner,
- a.table_name,
- a.num_rows,
- a.avg_row_len,
- round(a.avg_row_len * a.num_rows / 1024 / 1024, 2) real_bytes_MB,
- round(b.seg_bytes_mb, 2) seg_bytes_mb,
- decode(a.num_rows,
- 0,
- 100,
- (1 - round(a.avg_row_len * a.num_rows / 1024 / 1024 /
- b.seg_bytes_mb,
- 2)) * 100) || '%' frag_percent
- from dba_tables a,
- (select owner, segment_name, sum(bytes / 1024 / 1024) seg_bytes_mb
- from dba_segments
- group by owner, segment_name) b
- where a.table_name = b.segment_name
- and a.owner = b.owner
- and a.owner not in
- ('SYS', 'SYSTEM', 'OUTLN', 'DMSYS', 'TSMSYS', 'DBSNMP', 'WMSYS',
- 'EXFSYS', 'CTXSYS', 'XDB', 'OLAPSYS', 'ORDSYS', 'MDSYS', 'SYSMAN')
- and decode(a.num_rows,
- 0,
- 100,
- (1 - round(a.avg_row_len * a.num_rows / 1024 / 1024 /
- b.seg_bytes_mb,
- 2)) * 100) > 30
- order by b.seg_bytes_mb desc;
2、統計信息檢查
2.1 統計信息檢查
查看統計信息收集日期,確保碎片查詢結果準確:
- select owner,table_name,last_analyzed from dba_tables Where owner='<OWNER>' AND table_name='<TABLE_NAME>';
2.2 統計信息收集
如果統計信息過舊,則重新收集統計信息:
- exec dbms_stats.gather_table_stats(ownname=>'<OWNER>', tabname =>'<TABLE_NAME>');
3、表碎片整理
3.1 打開行移動
- alter table <TABLE_NAME> enable row movement ;
3.2 進行表收縮
- alter table <TABLE_NAME> shrink space cascade ;
3.3 失效對象編譯
語句可能會造成引用表
運行如下腳本,重新編譯失效對象。
- @?/rdbms/admin/utlrp.sql
4、對象收縮后的結果檢查
運行如下腳本,確認對象空間是否已經完成收縮。
- --all tables(partition_tables + non_partition_tables )
- select a.owner,
- a.table_name,
- a.num_rows,
- a.avg_row_len,
- round(a.avg_row_len * a.num_rows / 1024 / 1024, 2) real_bytes_MB,
- round(b.seg_bytes_mb, 2) seg_bytes_mb,
- decode(a.num_rows,
- 0,
- 100,
- (1 - round(a.avg_row_len * a.num_rows / 1024 / 1024 /
- b.seg_bytes_mb,
- 2)) * 100) || '%' frag_percent
- from dba_tables a,
- (select owner, segment_name, sum(bytes / 1024 / 1024) seg_bytes_mb
- from dba_segments
- group by owner, segment_name) b
- where a.table_name = b.segment_name
- and a.owner = b.owner
- and a.owner not in
- ('SYS', 'SYSTEM', 'OUTLN', 'DMSYS', 'TSMSYS', 'DBSNMP', 'WMSYS',
- 'EXFSYS', 'CTXSYS', 'XDB', 'OLAPSYS', 'ORDSYS', 'MDSYS', 'SYSMAN')
- and decode(a.num_rows,
- 0,
- 100,
- (1 - round(a.avg_row_len * a.num_rows / 1024 / 1024 /
- b.seg_bytes_mb,
- 2)) * 100) > 30
- order by b.seg_bytes_mb desc;
5、性能監控
監控數據庫會話,是否存在異常等待事件:
- select inst_id ,sid,serial#,sql_id,event,machine,module,program,seconds_in_wait from gv$session ;
- --看會話在做什么操作
- select sid, sql_text
- from v$session a, v$sql b
- where sid in(85,160)
- and(b.sql_id = a.sql_id or b.sql_id = a.prev_sql_id);
