oracle查詢表空間的空間佔用情況
轉自: http://www.cnblogs.com/HondaHsu/archive/2009/08/14/1545914.html
select a.tablespace_name,a.bytes bytes_used,b.largest,round(((a.bytes - b.bytes)/a.bytes)*100,2) percent_used
from (select tablespace_name,sum(bytes) bytes from dba_data_files group by tablespace_name) a,
(select tablespace_name,sum(bytes) bytes,max(bytes) largest from dba_free_space group by tablespace_name) b
where a.tablespace_name=b.tablespace_name order by ((a.bytes - b.bytes) / a.bytes) desc
select a.tablespace_name,a.bytes/1024/1024 "Sum MB",(a.bytes-b.bytes)/1024/1024 "used MB",b.bytes/1024/1024 "free MB",
round(((a.bytes-b.bytes)/a.bytes)*100,2) "percent_used"
from
(select tablespace_name,sum(bytes) bytes from dba_data_files group by tablespace_name) a,
(select tablespace_name,sum(bytes) bytes,max(bytes) largest from dba_free_space group by tablespace_name) b
where a.tablespace_name=b.tablespace_name
order by ((a.bytes-b.bytes)/a.bytes) desc
查詢所有表空間的總容量、已經使用、剩餘、已經使用的百分比!
select a.tablespace_name,a.bytes/1024/1024 "Sum MB",(a.bytes-b.bytes)/1024/1024 "used MB",b.bytes/1024/1024 "free MB",
round(((a.bytes-b.bytes)/a.bytes)*100,2) "percent_used"
from
(select tablespace_name,sum(bytes) bytes from dba_data_files group by tablespace_name) a,
(select tablespace_name,sum(bytes) bytes,max(bytes) largest from dba_free_space group by tablespace_name) b
where a.tablespace_name=b.tablespace_name
order by ((a.bytes-b.bytes)/a.bytes) desc
一般來說可以把上面的複雜的查詢語句放入一個檔案中,需要時再呼叫,或者建立一個試圖,需要時可以查詢。
1 寫入檔案:#vi /home/mzl/percent_used_tablespace.sql
內容:
select a.tablespace_name,a.bytes/1024/1024 "Sum MB",(a.bytes-b.bytes)/1024/1024 "used MB",b.bytes/1024/1024 "free MB",
round(((a.bytes-b.bytes)/a.bytes)*100,2) "percent_used"
from
(select tablespace_name,sum(bytes) bytes from dba_data_files group by tablespace_name) a,
(select tablespace_name,sum(bytes) bytes,max(bytes) largest from dba_free_space group by tablespace_name) b
where a.tablespace_name=b.tablespace_name
order by ((a.bytes-b.bytes)/a.bytes) desc
2 匯入:
SQL> @/home/mzl/percent_used_tablespace.sql
SQL> l
1 select a.tablespace_name,a.bytes "Sum",a.bytes-b.bytes "used",b.bytes "free",
2 round(((a.bytes-b.bytes)/a.bytes)*100,2) "percent_used"
3 from
4 (select tablespace_name,sum(bytes) bytes from dba_data_files group by tablespace_name) a,
5 (select tablespace_name,sum(bytes) bytes,max(bytes) largest from dba_free_space group by tablespace_name) b
6 where a.tablespace_name=b.tablespace_name
7* order by ((a.bytes-b.bytes)/a.bytes) desc
SQL> /
或者建立檢視:
SQL>create view percent
SQL>as
SQL>select a.tablespace_name,a.bytes/1024/1024 "Sum MB",(a.bytes-b.bytes)/1024/1024 "used MB",b.bytes/1024/1024 "free MB",
SQL>round(((a.bytes-b.bytes)/a.bytes)*100,2) "percent_used"
SQL>from
SQL>(select tablespace_name,sum(bytes) bytes from dba_data_files group by tablespace_name) a,
SQL>(select tablespace_name,sum(bytes) bytes,max(bytes) largest from dba_free_space group by tablespace_name) b
SQL>where a.tablespace_name=b.tablespace_name
SQL>order by ((a.bytes-b.bytes)/a.bytes) desc;
SQL> select * from percent;
查看錶空間的資料檔案是否是自動擴充套件:
SQL> l
1* select file_name,tablespace_name,autoextensible from dba_data_files
SQL> /
FILE_NAME TABLESPACE_NAME AUT
--------------------------------------------- ------------------------------ ---
/u01/app/oracle/oradata/orcl/risenet.dbf RISENET
/u01/app/oracle/oradata/orcl/perfstat.dbf PERFSTAT NO
/u01/app/oracle/oradata/orcl/example01.dbf EXAMPLE YES
/u01/disk1/users01.dbf USERS YES
/u01/app/oracle/oradata/orcl/sysaux01.dbf SYSAUX YES
/u01/app/oracle/oradata/orcl/undotbs01.dbf UNDOTBS1
/u01/disk2/system01.dbf SYSTEM YES
/u01/app/oracle/oradata/orcl/undotbs02.dbf UNDOTBS2 NO
/u01/disk1/pioneer_data.dbf PIONEER_DATA YES
/u01/disk2/pioneer_indx.dbf PIONEER_INDX NO
/u01/disk3/pioneer_undo.dbf PIONEER_UNDO NO
FILE_NAME TABLESPACE_NAME AUT
--------------------------------------------- ------------------------------ ---
/u01/app/oracle/oradata/orcl/paul01.dbf PAUL NO
/u01/disk1/wenchuan.dbf WENCHUAN NO
13 rows selected.
比如表空間PIONEER_INDX已經用了83.33%,資料檔案不能自動擴充套件,可以修改成自動擴充套件,以免資料寫滿資料檔案。
SQL> alter database
2 datafile '/u01/disk2/pioneer_indx.dbf' autoextend on;
Database altered.
SQL> select file_name,tablespace_name,autoextensible from dba_data_files
2 where tablespace_name='PIONEER_INDX';
FILE_NAME TABLESPACE_NAME AUT
--------------------------------------------- ------------------------------ ---
/u01/disk2/pioneer_indx.dbf PIONEER_INDX YES
或者給表空間多加一個自動擴充套件的資料檔案,如果有多個硬碟,可以增加多個數據檔案(這樣多資料庫系統的併發性比較好)
SQL> alter tablespace pioneer_indx
2 add datafile size 30M;
Tablespace altered.
SQL> select file_name,tablespace_name,bytes/1024/1024 "MB" from dba_data_files
2 where tablespace_name='PIONEER_INDX';
FILE_NAME TABLESPACE_NAME
--------------------------------------------- ------------------------------
MB
----------
/u01/disk2/pioneer_indx.dbf PIONEER_INDX
6
fty_.dbf
30