表空间space使用查询

本文提供了查询Oracle数据库表空间使用情况的SQL语句,包括各表空间的已使用空间和剩余空间,以及空间使用率,帮助进行数据库空间管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

--表空间使用

select tablespace_name, sum(bytes / 1024 / 1024) || ' MB' as Used_MB
  from dba_segments
 where segment_name not like 'BIN$%'
 group by tablespace_name;   --直接查各表空间使用空间

 

select total.tablespace_name,
       total.filebytes / 1024 / 1024 || ' MB' as "File_Bytes_MB",
       total.bytes / 1024 / 1024 || ' MB' "Total_MB",
       (total.bytes - free.bytes) / 1024 / 1024 || ' MB' "Used_MB",
       free.bytes / 1024 / 1024 || ' MB' "Free_MB",
       round(((total.bytes - free.bytes) / total.bytes) * 100, 2) || '%' "Percent_Used"
  from (select tablespace_name, sum(bytes) filebytes, sum(user_bytes) bytes
          from dba_data_files
         group by tablespace_name) total,
       (select tablespace_name, sum(bytes) bytes
          from dba_free_space
         group by tablespace_name) free
 where total.tablespace_name =  free.tablespace_name(+)
 order by ((total.bytes - free.bytes) / total.bytes) desc;    --查各表空间使用、剩余等。

 

--数据文件是否可收缩

select /*+ ordered use_hash(a,b,c) */
 a.file_id,
 a.file_name,
 a.filesize_Mb,
 nvl(b.freesize_Mb, 0) as freesize_Mb,
 (a.filesize_Mb - nvl(b.freesize_Mb, 0)) used_size_Mb,
 c.HWMsize_Mb,
 c.HWMsize_Mb - (a.filesize_Mb - nvl(b.freesize_Mb, 0)) unusedsize_belowhwm_Mb,
 a.filesize_Mb - c.HWMsize_Mb can_shrink_size_Mb
  from (select file_id, file_name, round(bytes / 1024 / 1024) filesize_Mb
          from dba_data_files) a,
       (select file_id, round(sum(dfs.bytes) / 1024 / 1024) freesize_Mb
          from dba_free_space dfs
         group by file_id) b,
       (select file_id, round(max(block_id) * 8 / 1024) HWMsize_Mb
          from dba_extents
         group by file_id) c
 where a.file_id = b.file_id(+)
   and a.file_id = c.file_id
 order by a.file_id;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值