概述
生产环境中,经常会遇到表由于数据不断插入,导致空间越来越大,由于前期
配置问题,没有做分区或者其他优化,而且生产数据实时向表插入。要删除历
史数据来释放空间。所以 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,
评论