1: --查詢表空間使用情況 2: SELECT Upper(F.TABLESPACE_NAME) "表空間名", 3: D.TOT_GROOTTE_MB "表空間大小(M)", 4: D.TOT_GROOTTE_MB - F.TOTAL_BYTES "已使用空間(M)", 5: To... ...
1: --查詢表空間使用情況
2: SELECT Upper(F.TABLESPACE_NAME) "表空間名",
3: D.TOT_GROOTTE_MB "表空間大小(M)",
4: D.TOT_GROOTTE_MB - F.TOTAL_BYTES "已使用空間(M)",
5: To_char(Round(( D.TOT_GROOTTE_MB - F.TOTAL_BYTES ) / D.TOT_GROOTTE_MB * 100, 2), '990.99')
6: || '%' "使用比",
7: F.TOTAL_BYTES "空閑空間(M)",
8: F.MAX_BYTES "最大塊(M)",
9: D.AUTOEXTENSIBLE "是否自增長",
10: D.INCREMENTSIZE "自增長大小(M)"
11: FROM (SELECT TABLESPACE_NAME,
12: Round(Sum(BYTES) / ( 1024 * 1024 ), 2) TOTAL_BYTES,
13: Round(Max(BYTES) / ( 1024 * 1024 ), 2) MAX_BYTES
14: FROM SYS.DBA_FREE_SPACE
15: GROUP BY TABLESPACE_NAME) F,
16: (SELECT DD.TABLESPACE_NAME,
17: Round(Sum(DD.BYTES) / ( 1024 * 1024 ), 2) TOT_GROOTTE_MB,
18: AUTOEXTENSIBLE,
19: --INCREMENT_BY的單位是數據塊的大小。數據塊大小一般為預設8k
20: Round(INCREMENT_BY * (select value/1024 from v$parameter where name='db_block_size')/1024, 2) INCREMENTSIZE
21: FROM SYS.DBA_DATA_FILES DD
22: GROUP BY DD.TABLESPACE_NAME,AUTOEXTENSIBLE,INCREMENT_BY) D
23: WHERE D.TABLESPACE_NAME = F.TABLESPACE_NAME
24: ORDER BY "表空間名"
顯示結果如下:
參考:https://www.cnblogs.com/xwdreamer/p/3511047.html
自己進行了一些優化,根據表空間的創建時間進行排序。在優化時之所以沒有使用三表直接聯合查詢(from a,b,c where a.xx=b.xx and b.xx=c.xx的方式),是為了提高查詢效率。
SELECT Upper(A.TABLESPACE_NAME) "表空間名", D.TOT_GROOTTE_MB "表空間大小(M)", D.TOT_GROOTTE_MB - A.TOTAL_BYTES "已使用空間(M)", To_char(Round((D.TOT_GROOTTE_MB - A.TOTAL_BYTES) / D.TOT_GROOTTE_MB * 100, 2), '990.99') || '%' "使用比", A.TOTAL_BYTES "空閑空間(M)", A.MAX_BYTES "最大塊(M)", D.AUTOEXTENSIBLE "是否自增長", D.incrementSize "自增長大小(M)", D.CREATE_TIME "創建時間" FROM (select T.TABLESPACE_NAME, Round(Sum(T.BYTES) / (1024 * 1024), 2) AS TOTAL_BYTES, Round(Max(T.BYTES) / (1024 * 1024), 2) AS MAX_BYTES from SYS.DBA_FREE_SPACE t group by t.TABLESPACE_NAME) A, (SELECT B.autoextensible, Round(Sum(B.BYTES) / (1024 * 1024), 2) TOT_GROOTTE_MB, Round(B.increment_by * (select value from v$parameter where name = 'db_block_size') / (1024 * 1024), 2) AS incrementSize,--自增長大小為資料庫中數據塊的個數,一個數據塊一般為8k B.TABLESPACE_NAME, min(C.CREATION_TIME) CREATE_TIME--獲取創建時間,如果有多個數據文件,獲取到最初時間 FROM SYS.DBA_DATA_FILES B, v$DATAFILE C WHERE B.file_id = C.FILE# group by B.TABLESPACE_NAME, B.autoextensible, B.increment_by) D WHERE A.TABLESPACE_NAME = D.TABLESPACE_NAME order by D.CREATE_TIME;
顯示結果如下: