欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 教育 > 锐评 > 【Oracle生产运维】表空间利用率不足处理

【Oracle生产运维】表空间利用率不足处理

2024/10/24 22:23:21 来源:https://blog.csdn.net/qq_34595089/article/details/139552474  浏览:    关键词:【Oracle生产运维】表空间利用率不足处理

1 前言

在生产环境中,一般设置表空间告警阈值是90%,在接到监控报警后,并不是需要立刻对表空间进行扩容。

决定是否扩容主要看表空间最近的增量是多少,假如剩余10%的空间还能支持1个月的增量,那就不需要急着扩容。如果剩余的空间只能坚持几天,那么最好是立即扩容,以防止数据突增

接到告警后,一般工作过程如下:

  1. 查看表空间利用率和剩余容量;
  2. 查看表空间增量;
  3. 扩容或者释放空间;
  4. 找出数据增量异常的对象。

根据下面的常用sql脚本排查。

2 处理流程

2.1 查看表空间利用率

col tablespace_name for a20
col pct_used for a10
select a.tablespace_name,a.total_mb,a.total_mb - b.free_mb used_mb,b.free_mb,case when a.total_mb <> 0 then round((a.total_mb - b.free_mb) / a.total_mb * 100,2)else null end || '%' pct_usedfrom (select ts.tablespace_name,round(sum(bytes) / 1024 / 1024,2) total_mbfrom dba_tablespaces ts,dba_data_files df  where ts.tablespace_name = df.tablespace_namegroup by ts.tablespace_name) a,(select fs.tablespace_name,round(sum(bytes) / 1024 /1024,2) free_mbfrom dba_free_space fsgroup by fs.tablespace_name) bwhere a.tablespace_name = b.tablespace_nameand a.tablespace_name = '&tsb_name'order by 1;

2.2 查看表空间增量

日增量

set line 200
col ts_name for a30
col pct_used for a10
SELECT a.snap_id,c.tablespace_name ts_name,to_char(to_date(a.rtime, 'mm/dd/yyyy hh24:mi:ss'), 'yyyy-mm-dd hh24:mi') rtime,round(a.tablespace_size * c.block_size / 1024 / 1024, 2) ts_size_mb,round(a.tablespace_usedsize * c.block_size / 1024 / 1024, 2) ts_used_mb,round((a.tablespace_size - a.tablespace_usedsize) * c.block_size / 1024 / 1024,2) ts_free_mb,round(a.tablespace_usedsize * c.block_size / 1024 / 1024, 2) - lag(round(a.tablespace_usedsize * c.block_size / 1024 / 1024, 2),1) over(order by a.tablespace_id,to_date(a.rtime, 'mm/dd/yyyy hh24:mi:ss')) inc_mb,round(a.tablespace_usedsize / a.tablespace_size * 100, 2) || '%' pct_usedFROM dba_hist_tbspc_space_usage a, (SELECT tablespace_id,substr(rtime, 1, 10) rtime,max(snap_id) snap_idFROM dba_hist_tbspc_space_usage nbgroup by tablespace_id, substr(rtime, 1, 10)) b,dba_tablespaces c,v$tablespace dwhere a.snap_id = b.snap_idand a.tablespace_id = b.tablespace_idand a.tablespace_id=d.TS#and d.NAME=c.tablespace_name  and d.NAME = '&tbs_name'and to_date(a.rtime, 'mm/dd/yyyy hh24:mi:ss') >=sysdate-30order by a.tablespace_id,to_date(a.rtime, 'mm/dd/yyyy hh24:mi:ss') desc;

累计增量,根据awr保留时间而定,默认为8天:

set line 200
col ts_name for a30
col pct_used for a10
with ts as(SELECT a.snap_id,c.tablespace_name ts_name,to_char(to_date(a.rtime, 'mm/dd/yyyy hh24:mi:ss'), 'yyyy-mm-dd hh24:mi') rtime,round(a.tablespace_size * c.block_size / 1024 / 1024, 2) ts_mb,round(a.tablespace_usedsize * c.block_size / 1024 / 1024, 2) ts_used_mb,round((a.tablespace_size - a.tablespace_usedsize) * c.block_size / 1024 / 1024,2) ts_free_mb,round(a.tablespace_usedsize / a.tablespace_size * 100, 2) || '%' pct_usedFROM dba_hist_tbspc_space_usage a, (SELECT tablespace_id,substr(rtime, 1, 10) rtime,max(snap_id) snap_idFROM dba_hist_tbspc_space_usage nbgroup by tablespace_id, substr(rtime, 1, 10)) b,dba_tablespaces c,v$tablespace dwhere a.snap_id = b.snap_idand a.tablespace_id = b.tablespace_idand a.tablespace_id=d.TS#and d.NAME=c.tablespace_nameand to_date(a.rtime, 'mm/dd/yyyy hh24:mi:ss') >=sysdate-30)
select f.ts_name,f.ts_mb,f.ts_used_mb begin_used_mb,f.rtime begin_time,t.ts_used_mb end_used_mb,t.rtime end_time,t.ts_used_mb - f.ts_used_mb inc_mb,round(to_date(t.rtime,'yyyy-mm-dd hh24:mi:ss') - to_date(f.rtime,'yyyy-mm-dd hh24:mi:ss'),2) inc_daysfrom (select a.*,row_number()over(partition by a.ts_name order by a.snap_id desc) rnfrom ts a) t,  (select b.*,row_number()over(partition by b.ts_name order by b.snap_id) rnfrom ts b) fwhere t.rn = 1 and f.rn = 1 and t.ts_name = f.ts_nameand t.ts_name = '&ts_name';

根据上述查出来的表空间日增量和累计增量结果,可以大概估算出剩余的空间可以坚持多久,根据实际情况决定是否扩容。

2.3 查看数据文件路径

此步骤主要是查看表空间数据文件路径,为表空间扩容添加数据文件做好环境调研。

set lines 200
set pagesize 300
col file_name for a60
col size_mb for 999999.99
select * from (select file_name,file_id,tablespace_name,round(bytes / 1024 / 1024,2) size_mb,status,autoextensiblefrom dba_data_fileswhere tablespace_name = '&ts_name'order by 2 desc)where rownum <= 10;

3 表空间扩容

表空间扩容可以选择添加数据文件,或者拓展数据文件。

3.1 添加数据文件

添加数据文件的时候一定要注意:

  • 在RAC集群环境中,切记不要将数据文件创建到本地,这样就会造成集群节点间的不一致,可能会导致其他节点起不来。
  • 也不要将数据文件创建到其他磁盘组中,这样不够规范。

以表空间ts_test为例:

--ASM:
SQL> alter tablespace ts_test add datafile '+DATA' size 100M;--File System:
SQL> alter tablespace ts_test datafile '/u01/app/oracle/oradata/datafile/ts_test02.dbf' size 100M;

3.2 拓展数据文件

假设原来ts_test.274.1171146701大小为100M,我们可以将其拓展到200M以达到扩容的目的:

alter database datafile'+DATA/orcl/datafile/ts_test.274.1171146701' resize 200M;

3.3 扩容后检查

扩容后需要检查表空间使用率是否下降:

col tablespace_name for a20
col pct_used for a10
select a.tablespace_name,a.total_mb,a.total_mb - b.free_mb used_mb,b.free_mb,case when a.total_mb <> 0 then round((a.total_mb - b.free_mb) / a.total_mb * 100,2)else null end || '%' pct_usedfrom (select ts.tablespace_name,round(sum(bytes) / 1024 / 1024,2) total_mbfrom dba_tablespaces ts,dba_data_files df  where ts.tablespace_name = df.tablespace_namegroup by ts.tablespace_name) a,(select fs.tablespace_name,round(sum(bytes) / 1024 /1024,2) free_mbfrom dba_free_space fsgroup by fs.tablespace_name) bwhere a.tablespace_name = b.tablespace_nameand a.tablespace_name = '&tsb_name'order by 1;

4 后续排查

如果表空间时短时间内激增,则在扩容后还需要排查,找出是哪个对象数据突增影响的。

4.1 查看snap_id

set line 200
select distinct snap_id,
to_char(begin_interval_time,‘yyyy-mm-dd hh24:mi:ss’) begin_interval_time,
to_char(end_interval_time,‘yyyy-mm-dd hh24:mi:ss’) end_interval_time
from dba_hist_snapshot
where to_char(begin_interval_time,‘yyyy-mm-dd hh24:mi:ss’) >=
to_char(sysdate - &day_ago,‘yyyy-mm-dd hh24:mi:ss’)
order by snap_id desc;

4.2 查看某个表空间下增量最多的对象

set lines 200
col object_name for a30
select * from
(select obj.owner,obj.object_name,sum(hs.db_block_changes_delta) db_block_changes_delta,
round(sum(hs.space_used_delta) / 1024 / 1024,2) space_delta_mb
from dba_hist_seg_stat hs,
v$tablespace ts,
dba_objects obj,
dba_hist_snapshot sn
where hs.ts# = ts.ts#
and hs.snap_id = sn.snap_id
and hs.obj# = obj.object_id
and ts.name = ‘&tbs_name’
and sn.begin_interval_time >= sysdate - &day_ago
group by obj.owner,obj.object_name
order by space_delta_mb desc)
where rownum <= 10;

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com