我们的应用程序失败了几次,因为'ORA-01536:超出了表空间的空间配额',我们希望能够通过定期检查表空间上的可用空间并在丢弃时发出警报来防止这种情况发生。低于一定水平。
有没有办法找出表空间中剩余多少可用空间?
经过一些研究(我不是DBA),我尝试了以下方法:
select max_bytes-bytes from user_ts_quotas;
select sum(nvl(bytes,0)) from user_free_space;
但这些查询会返回完全不同的结果。
答案 0 :(得分:85)
我使用此查询
column "Tablespace" format a13
column "Used MB" format 99,999,999
column "Free MB" format 99,999,999
column "Total MB" format 99,999,999
select
fs.tablespace_name "Tablespace",
(df.totalspace - fs.freespace) "Used MB",
fs.freespace "Free MB",
df.totalspace "Total MB",
round(100 * (fs.freespace / df.totalspace)) "Pct. Free"
from
(select
tablespace_name,
round(sum(bytes) / 1048576) TotalSpace
from
dba_data_files
group by
tablespace_name
) df,
(select
tablespace_name,
round(sum(bytes) / 1048576) FreeSpace
from
dba_free_space
group by
tablespace_name
) fs
where
df.tablespace_name = fs.tablespace_name;
答案 1 :(得分:28)
更准确的SQL语句
SELECT a.tablespace_name,
ROUND (((c.BYTES - NVL (b.BYTES, 0)) / c.BYTES) * 100,2) percentage_used,
c.BYTES / 1024 / 1024 space_allocated,
ROUND (c.BYTES / 1024 / 1024 - NVL (b.BYTES, 0) / 1024 / 1024,2) space_used,
ROUND (NVL (b.BYTES, 0) / 1024 / 1024, 2) space_free,
c.DATAFILES
FROM dba_tablespaces a,
( SELECT tablespace_name,
SUM (BYTES) BYTES
FROM dba_free_space
GROUP BY tablespace_name
) b,
( SELECT COUNT (1) DATAFILES,
SUM (BYTES) BYTES,
tablespace_name
FROM dba_data_files
GROUP BY tablespace_name
) c
WHERE b.tablespace_name(+) = a.tablespace_name
AND c.tablespace_name(+) = a.tablespace_name
ORDER BY NVL (((c.BYTES - NVL (b.BYTES, 0)) / c.BYTES), 0) DESC;
答案 2 :(得分:10)
这是我遇到的最简单的查询之一,我们也将它用于监控:
SELECT TABLESPACE_NAME,SUM(BYTES)/1024/1024/1024 "FREE SPACE(GB)"
FROM DBA_FREE_SPACE GROUP BY TABLESPACE_NAME;
关于Oracle Tablespace的完整文章: Tablespace
答案 3 :(得分:10)
有很多方法可以检查大小,但作为开发人员,我们没有太多访问查询元表的权限,我发现这个解决方案很容易 (注意:如果您收到错误消息ORA-01653 '引起ORA-01653错误是因为您需要向表空间添加空间。')
--Size of All Table Space
--1. Used Space
SELECT TABLESPACE_NAME,TO_CHAR(SUM(NVL(BYTES,0))/1024/1024/1024, '99,999,990.99') AS "USED SPACE(IN GB)" FROM USER_SEGMENTS GROUP BY TABLESPACE_NAME
--2. Free Space
SELECT TABLESPACE_NAME,TO_CHAR(SUM(NVL(BYTES,0))/1024/1024/1024, '99,999,990.99') AS "FREE SPACE(IN GB)" FROM USER_FREE_SPACE GROUP BY TABLESPACE_NAME
--3. Both Free & Used
SELECT USED.TABLESPACE_NAME, USED.USED_BYTES AS "USED SPACE(IN GB)", FREE.FREE_BYTES AS "FREE SPACE(IN GB)"
FROM
(SELECT TABLESPACE_NAME,TO_CHAR(SUM(NVL(BYTES,0))/1024/1024/1024, '99,999,990.99') AS USED_BYTES FROM USER_SEGMENTS GROUP BY TABLESPACE_NAME) USED
INNER JOIN
(SELECT TABLESPACE_NAME,TO_CHAR(SUM(NVL(BYTES,0))/1024/1024/1024, '99,999,990.99') AS FREE_BYTES FROM USER_FREE_SPACE GROUP BY TABLESPACE_NAME) FREE
ON (USED.TABLESPACE_NAME = FREE.TABLESPACE_NAME);
由于
答案 4 :(得分:1)
column pct_free format 999.99
select
used.tablespace_name,
(reserv.maxbytes - used.bytes)*100/reserv.maxbytes pct_free,
used.bytes/1024/1024/1024 used_gb,
reserv.maxbytes/1024/1024/1024 maxgb,
reserv.bytes/1024/1024/1024 gb,
(reserv.maxbytes - used.bytes)/1024/1024/1024 "max free bytes",
reserv.datafiles
from
(select tablespace_name, count(1) datafiles, sum(greatest(maxbytes,bytes)) maxbytes, sum(bytes) bytes from dba_data_files group by tablespace_name) reserv,
(select tablespace_name, sum(bytes) bytes from dba_segments group by tablespace_name) used
where used.tablespace_name = reserv.tablespace_name
order by 2
/
答案 5 :(得分:0)
除非我弄错了,否则上面的代码不考虑未分配的空间,所以如果你真的想知道何时达到硬限制,你应该使用maxbytes。
我认为下面的代码可以做到这一点。它将可用空间计算为“自由空间”+未分配空间。
select
free.tablespace_name,
free.bytes,
reserv.maxbytes,
reserv.bytes,
reserv.maxbytes - reserv.bytes + free.bytes "max free bytes",
reserv.datafiles
from
(select tablespace_name, count(1) datafiles, sum(maxbytes) maxbytes, sum(bytes) bytes from dba_data_files group by tablespace_name) reserv,
(select tablespace_name, sum(bytes) bytes from dba_free_space group by tablespace_name) free
where free.tablespace_name = reserv.tablespace_name;
答案 6 :(得分:0)
通过查看磁盘上文件的大小,您也可以大致了解表空间的使用情况。
我的数据库是在最大扩展范围内创建的,每个dbf文件只能增长到32gigs-因此,当最后一个达到32gigs时,您知道空间将用完,需要添加另一个。
答案 7 :(得分:0)
这是Oracle SQL Developer在其表空间视图中使用的查询
select a.tablespace_name as "Tablespace Name",
round(a.bytes_alloc / 1024 / 1024) "Allocated (MB)",
round(nvl(b.bytes_free, 0) / 1024 / 1024) "Free (MB)",
round((a.bytes_alloc - nvl(b.bytes_free, 0)) / 1024 / 1024) "Used (MB)",
round((nvl(b.bytes_free, 0) / a.bytes_alloc) * 100) "% Free",
100 - round((nvl(b.bytes_free, 0) / a.bytes_alloc) * 100) "% Used",
round(maxbytes/1024 / 1024) "Max. Bytes (MB)"
from ( select f.tablespace_name,
sum(f.bytes) bytes_alloc,
sum(decode(f.autoextensible, 'YES',f.maxbytes,'NO', f.bytes)) maxbytes
from dba_data_files f
group by tablespace_name) a,
( select f.tablespace_name,
sum(f.bytes) bytes_free
from dba_free_space f
group by tablespace_name) b
where a.tablespace_name = b.tablespace_name (+)
union all
select
h.tablespace_name as tablespace_name,
round(sum(h.bytes_free + h.bytes_used) / 1048576) megs_alloc,
round(sum((h.bytes_free + h.bytes_used) - nvl(p.bytes_used, 0)) / 1048576) megs_free,
round(sum(nvl(p.bytes_used, 0))/ 1048576) megs_used,
round((sum((h.bytes_free + h.bytes_used) - nvl(p.bytes_used, 0)) / sum(h.bytes_used + h.bytes_free)) * 100) Pct_Free,
100 - round((sum((h.bytes_free + h.bytes_used) - nvl(p.bytes_used, 0)) / sum(h.bytes_used + h.bytes_free)) * 100) pct_used,
round(sum(f.maxbytes) / 1048576) max
from sys.v_$TEMP_SPACE_HEADER h, sys.v_$Temp_extent_pool p, dba_temp_files f
where p.file_id(+) = h.file_id
and p.tablespace_name(+) = h.tablespace_name
and f.file_id = h.file_id
and f.tablespace_name = h.tablespace_name
group by h.tablespace_name
ORDER BY 2;
答案 8 :(得分:0)
以下查询将有助于找出表空间的可用空间(以MB为单位):
select tablespace_name , sum(bytes)/1024/1024 from dba_free_space group by tablespacE_name order by 1;
答案 9 :(得分:0)
您可以通过运行此查询check the tablespace free space:
SELECT DFQ.TABLESPACE_NAME "Tablespace Name"
,DFQ.TOTALSPACE "Total Size MB"
, (DFQ.TOTALSPACE - DSQ.TOTALUSEDSPACE) "Free Space MB"
,ROUND (100 * ( (DFQ.TOTALSPACE - DSQ.TOTALUSEDSPACE) / DFQ.TOTALSPACE)) || '%' "Free Space %" FROM (SELECT TABLESPACE_NAME, ROUND (SUM (BYTES) / 1048576) TOTALSPACE
FROM DBA_DATA_FILES
GROUP BY TABLESPACE_NAME) DFQ
,(SELECT TABLESPACE_NAME, ROUND (SUM (BYTES) / (1024 * 1024)) TOTALUSEDSPACE
FROM DBA_SEGMENTS
GROUP BY TABLESPACE_NAME) DSQ WHERE DFQ.TABLESPACE_NAME = DSQ.TABLESPACE_NAME(+);
在上面的查询中,我们选择了每个表空间的总大小以及以 MB 为单位的可用空间和占总大小的百分比的可用空间。因此,您可以使用此百分比列在可用空间百分比达到 20% 或任何您想要的百分比时跟踪和发出警报。
答案 10 :(得分:-2)
您可以在此有用的包中使用名为tablespaces.sh的脚本: http://dba-tips.blogspot.com/2014/02/oracle-database-administration-scripts.html