暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

oracle表空间使用率查询

原创 stephen 2023-07-05
390

        dba的日常工作之一就是确保数据库的空间充足,在保证服务器系统空间充足(rac还有asm磁盘组空间需要充足)的同时,还需要监控表空间使用率,保证表空间的空间不会因为不够而报错。虽然现在大部分系统都有监控系统,但有时候还是需要dba手动去查询目前数据库各个表空间的使用情况。笔者总结了一下平时使用过的查询sql,分享给大家。

1、

select     a.tablespace_name,

    round(total_space,5) as total_space,

    round(total_space-used_space,5) as space_free,

    round(used_space,5) as space_used,

    round(used_space/total_space,4)*100 ratio

from   (select tablespace_name,sum(bytes)/1024/1024 as used_space from dba_segments

    group by tablespace_name) a,

    dba_tablespaces b,

    (select tablespace_name,sum(decode(AUTOEXTENSIBLE,'YES',maxbytes,'NO',bytes))/1024/1024 as total_space

    from dba_data_files group by tablespace_name) c

where  a.tablespace_name=b.tablespace_name

and a.tablespace_name=c.tablespace_name

and    b.contents='PERMANENT'

order by tablespace_name;

笔者最早使用的一个查询脚本,使用率以可拓展的最大空间计算,查询结果如下:


2、

set line 300

col tablespace_name for a40
col CUR_PCT for a10
col EXT_PCT for a10
select
tablespace,
current_used_mb,
total_mb,
can_extend_mb,
trunc(current_used_mb/total_mb*100,2)||'%' cur_pct,
trunc(current_used_mb/can_extend_mb*100,2)||'%' ext_pct,
count_file
from
(
select
a.tablespace_name tablespace,
trunc((b.total-a.free)/1024/1024,2) current_used_mb,
trunc(b.total/1024/1024,2) total_mb,
trunc(b.extent_total/1024/1024) can_extend_mb,
b.count_file count_file
from
(
select tablespace_name,
sum(nvl(bytes,0)) total,
sum(decode(maxbytes,0,bytes,maxbytes)) extent_total,
count(file_name) count_file
from dba_data_files
where tablespace_name not like '%UNDO%'
group by tablespace_name
) b
left join
(
select
tablespace_name,
sum(bytes) free
from dba_free_space
group by tablespace_name
) a
on a.tablespace_name=b.tablespace_name
)
order by EXT_PCT desc;
笔者常用的一个脚本,查询的结果比较详细:


3、

with undotbs as

(select a.ts#, a.name, b.status, b.contents
from v$tablespace a, dba_tablespaces b
where a.name = b.tablespace_name)
select a.name, b.used_space / c.max_size * 100 used_percent, a.status
from undotbs a,
dba_tablespace_usage_metrics b,
(select tablespace_id, sum(file_maxsize) max_size
from v$filespace_usage
group by tablespace_id) c
where a.name = b.tablespace_name(+)
and a.ts# = c.tablespace_id(+)
and a.contents not in ('UNDO', 'TEMPORARY')
union all
select b.name, tablespace_usedsize / tablespace_maxsize * 100, b.status
from dba_hist_tbspc_space_usage a, undotbs b
where a.tablespace_id = b.ts# and b.contents = 'UNDO' and
a.snap_id = (select max(snap_id) from dba_hist_snapshot)
;

最近记录的一个脚本,用到的查询视图之前没注意过,查询的结果大差不差,包括undo和temp表空间:


4、

select * from dba_tablespace_usage_metrics;

该视图使用到的是oracle自带的一个视图,能查询到所有表空间的情况,10g版本后都可以使用,语句简单,使用方便,查询的结果也准确:


     以上是笔者使用过的几个查询表空间的sql脚本,总体查询的结果大差不差,都能查询到目前数据库各个表空间的使用情况,然后根据实际情况进行及时调整,希望对大家会有帮助!

最后修改时间:2023-07-05 11:31:46
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论