超详细的Oracle数据库表碎片整理规范,值得收藏

概述

生产环境中,经常会遇到表由于数据不断插入,导致空间越来越大,由于前期配置问题,没有做分区或者其他优化,而且生产数据实时向表插入。要删除历史数据来释放空间。所以DBA一般都需要定期去对Oracle表碎片做整理,简单整理表碎片整理流程如下:

1、定位存在碎片的对象

使用如下脚本,检查需要进行碎片整理的对象:

 
 
 
 
  1. --all tables(partition_tables + non_partition_tables ) 
  2. select a.owner, 
  3.  a.table_name, 
  4.  a.num_rows, 
  5.  a.avg_row_len, 
  6.  round(a.avg_row_len * a.num_rows / 1024 / 1024, 2) real_bytes_MB, 
  7.  round(b.seg_bytes_mb, 2) seg_bytes_mb, 
  8.  decode(a.num_rows, 
  9.  0, 
  10.  100, 
  11.  (1 - round(a.avg_row_len * a.num_rows / 1024 / 1024 / 
  12.  b.seg_bytes_mb, 
  13.  2)) * 100) || '%' frag_percent 
  14.  from dba_tables a, 
  15.  (select owner, segment_name, sum(bytes / 1024 / 1024) seg_bytes_mb 
  16.  from dba_segments 
  17.  group by owner, segment_name) b 
  18.  where a.table_name = b.segment_name 
  19.  and a.owner = b.owner 
  20.  and a.owner not in 
  21.  ('SYS', 'SYSTEM', 'OUTLN', 'DMSYS', 'TSMSYS', 'DBSNMP', 'WMSYS', 
  22.  'EXFSYS', 'CTXSYS', 'XDB', 'OLAPSYS', 'ORDSYS', 'MDSYS', 'SYSMAN') 
  23.  and decode(a.num_rows, 
  24.  0, 
  25.  100, 
  26.  (1 - round(a.avg_row_len * a.num_rows / 1024 / 1024 / 
  27.  b.seg_bytes_mb, 
  28.  2)) * 100) > 30 
  29.  order by b.seg_bytes_mb desc; 

2、统计信息检查

2.1 统计信息检查

查看统计信息收集日期,确保碎片查询结果准确:

 
 
 
 
  1. select owner,table_name,last_analyzed from dba_tables Where owner='' AND table_name=''; 

2.2 统计信息收集

如果统计信息过旧,则重新收集统计信息:

 
 
 
 
  1. exec dbms_stats.gather_table_stats(ownname=>'', tabname =>''); 

3、表碎片整理

3.1 打开行移动

 
 
 
 
  1. alter table  enable row movement ; 

3.2 进行表收缩

 
 
 
 
  1. alter table  shrink space cascade ; 

3.3 失效对象编译

语句可能会造成引用表 的对象(如存储过程、包、视图等)变为无效。

运行如下脚本,重新编译失效对象。

 
 
 
 
  1. @?/rdbms/admin/utlrp.sql 

4、对象收缩后的结果检查

运行如下脚本,确认对象空间是否已经完成收缩。

 
 
 
 
  1. --all tables(partition_tables + non_partition_tables ) 
  2. select a.owner, 
  3.  a.table_name, 
  4.  a.num_rows, 
  5.  a.avg_row_len, 
  6.  round(a.avg_row_len * a.num_rows / 1024 / 1024, 2) real_bytes_MB, 
  7.  round(b.seg_bytes_mb, 2) seg_bytes_mb, 
  8.  decode(a.num_rows, 
  9.  0, 
  10.  100, 
  11.  (1 - round(a.avg_row_len * a.num_rows / 1024 / 1024 / 
  12.  b.seg_bytes_mb, 
  13.  2)) * 100) || '%' frag_percent 
  14.  from dba_tables a, 
  15.  (select owner, segment_name, sum(bytes / 1024 / 1024) seg_bytes_mb 
  16.  from dba_segments 
  17.  group by owner, segment_name) b 
  18.  where a.table_name = b.segment_name 
  19.  and a.owner = b.owner 
  20.  and a.owner not in 
  21.  ('SYS', 'SYSTEM', 'OUTLN', 'DMSYS', 'TSMSYS', 'DBSNMP', 'WMSYS', 
  22.  'EXFSYS', 'CTXSYS', 'XDB', 'OLAPSYS', 'ORDSYS', 'MDSYS', 'SYSMAN') 
  23.  and decode(a.num_rows, 
  24.  0, 
  25.  100, 
  26.  (1 - round(a.avg_row_len * a.num_rows / 1024 / 1024 / 
  27.  b.seg_bytes_mb, 
  28.  2)) * 100) > 30 
  29.  order by b.seg_bytes_mb desc; 

5、性能监控

监控数据库会话,是否存在异常等待事件:

 
 
 
 
  1. select inst_id ,sid,serial#,sql_id,event,machine,module,program,seconds_in_wait from gv$session ; 
  2. --看会话在做什么操作 
  3. select sid, sql_text 
  4.  from v$session a, v$sql b 
  5.  where sid in(85,160) 
  6.  and(b.sql_id = a.sql_id or b.sql_id = a.prev_sql_id); 

分享名称:超详细的Oracle数据库表碎片整理规范,值得收藏
分享链接:http://www.csdahua.cn/qtweb/news29/425679.html

网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网