這段時間發現一個不大不小的問題:在我每天維護數據庫檢查剩余表空間大小時,執行相應SQL,速度很慢,這麼多DB隻發現SFC這台如此,於是決定今天一定要找到原因!
SQL如下:
select a.tablespace_name,
round(a.bytes_alloc / 1024 / 1024, 2) megs_alloc,
round(nvl(b.bytes_free, 0) / 1024 / 1024, 2) megs_free,
round((a.bytes_alloc - nvl(b.bytes_free, 0)) / 1024 / 1024, 2) megs_used,
round((nvl(b.bytes_free, 0) / a.bytes_alloc) * 100,2) Pct_Free,
100 - round((nvl(b.bytes_free, 0) / a.bytes_alloc) * 100,2) Pct_used,
round(maxbytes/1048576,2) Max
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,
round(sum(h.bytes_free + h.bytes_used) / 1048576, 2) megs_alloc,
round(sum((h.bytes_free + h.bytes_used) - nvl(p.bytes_used, 0)) / 1048576, 2) megs_free,
round(sum(nvl(p.bytes_used, 0))/ 1048576, 2) megs_used,
round((sum((h.bytes_free + h.bytes_used) - nvl(p.bytes_used, 0)) / sum(h.bytes_used + h.bytes_free)) * 100,2) Pct_Free,
100 - round((sum((h.bytes_free + h.bytes_used) - nvl(p.bytes_used, 0)) / sum(h.bytes_used + h.bytes_free)) * 100,2) pct_used,
round(f.maxbytes / 1048576, 2) 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, f.maxbytes
ORDER BY 1
通過查看執行計劃可看到90%Cost都用在這個View中:
SELECT f.tablespace_name, SUM (f.BYTES) bytes_free
FROM dba_free_space f
GROUP BY tablespace_name
又集中於FTS方式的recyclebin$中:
SYS@sfcdb01:SQL>Selectcount(*) from sys.recyclebin$;
COUNT(*)
------------
134319
沒搞明白用戶不斷的drop/create table做什麼!導致回收站裡這麼多數據沒有及時清除,最終影響到我表空間SQL的執行。
我的數據庫版本是10.2.0.1的,最新引入recyclebin這個概念,而system空間足夠大,除非空間滿recyclebin才會自動清除,像這樣的情況隻能手動執行清理動作:
SQL>Purge dba_recyclebin;
不會出什麼問題了吧?怎麼半個小時了還沒有執行完成?於是我不斷的select count(*)的數量,看著它不斷的減少,心裡才得以安穩。
天!足足兩個多小時才purge完成!
現在重新執行以上SQL,眨眼閃的功夫就看到剩余空間拉...