侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

Oracle表空间数据库文件收缩案例解析

数据库  /  管理员 发布于 6年前   134

我们经常会遇到数据库磁盘空间爆满的问题,或由于归档日志突增、或由于数据文件过多、大导致磁盘使用紧俏。这里主要说的场景是磁盘空间本身很大,但表空间对应的数据文件初始化的时候就直接顶满了磁盘空间,导致经常收到磁盘空间满的报警。

一、错误信息

告警内容如下:

【发现异常】地产客储系统数据库Oracle_192.168.xx.xx,192.168.xx.xx,数据库customer,连接错误,0 ORA-00257: archiver error. Connect internal only, until freed.

【发生时间】2018.07.04 09:12:21

二、错误原因

上述错误一看大致就知道是由于磁盘空间不足,导致归档无法完成所致,我们只需要清理足够的磁盘空间即可。但在磁盘清理的时候发现磁盘空间本身可清理的不多,被很多很大的数据文件占用,而实际使用的segment大小总共不足400G,磁盘空间本身1T,所以我们可以通过收缩数据文件的方式回收磁盘空间。

数据文件初始化方式:

1.我们创建表空间一般有两种方式初始化其数据文件,即指定初始大小为32G(很大的值)或指定初始大小为100M(很小的值)然后通过自动扩展方式慢慢按需增长。

2.第一种初始数据文件方法坏处就是开始不管你用不用到那么大,都会占用这么大的磁盘空间(这种数据迁移的时候可以使用)。第二种初始化方法按需增长,比较好的监控实际使用磁盘空间,所以推荐初始值很小,使用自动扩展慢慢增长的方式。

三、处理步骤

1.查看磁盘空间大小

2.查看数据库表空间大小

#!/bin/bashsqlplus -S /nolog  <<EOFconn /as sysdba;set echo off heading on underline on;column inst_num heading "Inst Num" new_value inst_num format 99999;column inst_name heading "Instance" new_value inst_name format a12;column db_name  heading "DB Name"  new_value db_name  format a12;column dbid   heading "DB Id"   new_value dbid   format 9999999999 just c;promptprompt Current Instanceprompt ~~~~~~~~~~~~~~~~select d.dbid      dbid   , d.name      db_name   , i.instance_number inst_num   , i.instance_name  inst_name from v\$database d,    v\$instance i;set term on feedback off lines 130 pagesize 999 tab off trims oncolumn MB format 999,999,999 heading "Total MB"column free format 9,999,999 heading "Free MB"column used format 99,999,999 heading "Used MB"column Largest format 999,999 heading "LrgstMB"column tablespace_name format a20 heading "Tablespace"column status format a3 truncatedcolumn max_extents format 99999999999 heading "MaxExt"col extent_management      for a1 trunc  head "M"col allocation_type       for a1 trunc  head "A"col Ext_Size for a4 trunc head "Init"column pfree format a3 trunc heading "%Fr"break on reportcompute sum of MB on reportcompute sum of free on reportcompute sum of used on reportselect  d.tablespace_name,  decode(d.status,   'ONLINE', 'OLN',  'READ ONLY', 'R/O',  d.status) status, d.extent_management,  decode(d.allocation_type,  'USER','',  d.allocation_type) allocation_type, (case   when initial_extent < 1048576   then lpad(round(initial_extent/1024,0),3)||'K'   else lpad(round(initial_extent/1024/1024,0),3)||'M'  end) Ext_Size, NVL (a.bytes / 1024 / 1024, 0) MB, NVL (f.bytes / 1024 / 1024, 0) free,  (NVL (a.bytes / 1024 / 1024, 0) - NVL (f.bytes / 1024 / 1024, 0)) used, NVL (l.large / 1024 / 1024, 0) largest,  d.MAX_EXTENTS , lpad(round((f.bytes/a.bytes)*100,0),3) pfree, (case when round(f.bytes/a.bytes*100,0) >= 20 then ' ' else '*' end) alrtFROM sys.dba_tablespaces d, (SELECT  tablespace_name, SUM(bytes) bytes  FROM dba_data_files  GROUP BY tablespace_name) a, (SELECT  tablespace_name, SUM(bytes) bytes  FROM dba_free_space  GROUP BY tablespace_name) f, (SELECT  tablespace_name, MAX(bytes) large  FROM dba_free_space  GROUP BY tablespace_name) lWHERE d.tablespace_name = a.tablespace_name(+) AND d.tablespace_name = f.tablespace_name(+) AND d.tablespace_name = l.tablespace_name(+) AND NOT (d.extent_management LIKE 'LOCAL' AND d.contents LIKE 'TEMPORARY')UNION ALLselect  d.tablespace_name,  decode(d.status,   'ONLINE', 'OLN',  'READ ONLY', 'R/O',  d.status) status, d.extent_management,  decode(d.allocation_type,  'UNIFORM','U',  'SYSTEM','A',  'USER','',  d.allocation_type) allocation_type, (case   when initial_extent < 1048576   then lpad(round(initial_extent/1024,0),3)||'K'   else lpad(round(initial_extent/1024/1024,0),3)||'M'  end) Ext_Size, NVL (a.bytes / 1024 / 1024, 0) MB, (NVL (a.bytes / 1024 / 1024, 0) - NVL (t.bytes / 1024 / 1024, 0)) free, NVL (t.bytes / 1024 / 1024, 0) used,  NVL (l.large / 1024 / 1024, 0) largest,  d.MAX_EXTENTS , lpad(round(nvl(((a.bytes-t.bytes)/NVL(a.bytes,0))*100,100),0),3) pfree, (case when nvl(round(((a.bytes-t.bytes)/NVL(a.bytes,0))*100,0),100) >= 20 then ' ' else '*' end) alrtFROM sys.dba_tablespaces d, (SELECT  tablespace_name, SUM(bytes) bytes  FROM dba_temp_files  GROUP BY tablespace_name order by tablespace_name) a, (SELECT  tablespace_name, SUM(bytes_used ) bytes  FROM v\$temp_extent_pool  GROUP BY tablespace_name) t, (SELECT  tablespace_name, MAX(bytes_cached) large  FROM v\$temp_extent_pool  GROUP BY tablespace_name order by tablespace_name) lWHERE d.tablespace_name = a.tablespace_name(+) AND d.tablespace_name = t.tablespace_name(+) AND d.tablespace_name = l.tablespace_name(+) AND d.extent_management LIKE 'LOCAL' AND d.contents LIKE 'TEMPORARY' ORDER by 1/promptexitEOF

3.查询可直接收缩表空间数据文件

这里查看的是可以直接收缩的数据文件大小,比如最开始初始化的数据文件为32G,在数据文件高水位以下的为20G,那么可直接回收的为12G。

select a.file#,a.name,a.bytes/1024/1024 CurrentMB,     ceil(HWM * a.block_size)/1024/1024 ResizeTo,     (a.bytes - HWM * a.block_size)/1024/1024 ReleaseMB,     'alter database datafile '''||a.name||''' resize '||     ceil(HWM * a.block_size/1024/1024) || 'M;' ResizeCMD  from v$datafile a,    (select file_id,max(block_id+blocks-1) HWM     from dba_extents     group by file_id) b where a.file# = b.file_id(+)  and (a.bytes - HWM *block_size)>0;

4.直接收缩数据文件

alter database datafile '/oracle/oradata/bi/data01.dbf' resize 1548M;

5.再次查看磁盘空间,已释放很多,可手动完成归档测试。

四、总结

针对oracle的数据文件收缩(磁盘空间收缩),我们一般可通过当前磁盘空间查看(df -h)――>执行可直接收缩的查询命令和收缩命令――>执行大表高水位收缩――>执行表空间高水位收缩(降低文件高水位线)――>再次执行直接回收表空间数据文件命令

直接收缩数据文件的方式参考本文上述步骤即可完成。

那么如何降低表空间的数据文件高水位,进而完成表空间数据文件回收呢?

1.查看大于10G的数据文件

select file_name,file_id,tablespace_name,(bytes/1024/1024/1024) file_size_gb from dba_data_files where (bytes/1024/1024/1024) >10 order by file_id;

2.查看大于10G的数据文件对应的数据块信息

select file_id,max(block_id+blocks-1) HWM,block_id       from dba_extents       where file_id =14       group by file_id,block_id       order by hwm desc ;

3.查看大表对应的数据块信息

##查看大表select file_name,file_id,tablespace_name,(bytes/1024/1024/1024) file_size_gb from dba_data_files where (bytes/1024/1024/1024) >10 order by file_id;##查看大表对应的块 select owner,segment_name,file_id,block_id,blocks from dba_extents where segment_name='TABLE_NAME';

4.降低表的高水位

alter table table_name move;alter index idx_name rebuild;

5.查看数据文件对应的最大的block_id

SELECT MAX(block_id)     FROM dba_extents     WHERE tablespace_name = 'TABLESPACE_NAME'; 

6.执行数据文件收缩

(block_id+blocks-1)数据文件的HWMalter database datafile '/oracle/oradata/bi/data01.dbf' resize xxxM;

总结

以上所述是小编给大家介绍的Oracle表空间数据库文件收缩案例解析,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对站的支持!


  • 上一条:
    Oracle 日志挖掘(LogMiner)使用详解
    下一条:
    浅析Oracle中sys、system和Scott用户下的数据库连接问题
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 分库分表的目的、优缺点及具体实现方式介绍(0个评论)
    • DevDB - 在 VS 代码中直接访问数据库(0个评论)
    • 在ubuntu系统中实现mysql数据存储目录迁移流程步骤(0个评论)
    • 在mysql中使用存储过程批量新增测试数据流程步骤(0个评论)
    • php+mysql数据库批量根据条件快速更新、连表更新sql实现(0个评论)
    • 近期文章
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2017-06
    • 2017-08
    • 2017-09
    • 2017-10
    • 2017-11
    • 2018-01
    • 2018-05
    • 2018-10
    • 2018-11
    • 2020-02
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-09
    • 2021-02
    • 2021-04
    • 2021-07
    • 2021-08
    • 2021-11
    • 2021-12
    • 2022-02
    • 2022-03
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-07
    • 2023-08
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-03
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客