【轉】Oracle 查詢表空間使用情況

--查詢表空間使用情況
SELECT UPPER(F.TABLESPACE_NAME) "表空間名",
       D.TOT_GROOTTE_MB "表空間大小(M)",
       D.TOT_GROOTTE_MB - F.TOTAL_BYTES "已使用空間(M)",
       TO_CHAR(ROUND((D.TOT_GROOTTE_MB - F.TOTAL_BYTES) / D.TOT_GROOTTE_MB * 100,2),'990.99') || '%' "使用比",
       F.TOTAL_BYTES "空閒空間(M)",
       F.MAX_BYTES "最大塊(M)"
FROM (SELECT TABLESPACE_NAME,
               ROUND(SUM(BYTES) / (1024 * 1024), 2) TOTAL_BYTES,
               ROUND(MAX(BYTES) / (1024 * 1024), 2) MAX_BYTES
          FROM SYS.DBA_FREE_SPACE
         GROUP BY TABLESPACE_NAME) F,
       (SELECT DD.TABLESPACE_NAME,
               ROUND(SUM(DD.BYTES) / (1024 * 1024), 2) TOT_GROOTTE_MB
          FROM SYS.DBA_DATA_FILES DD
         GROUP BY DD.TABLESPACE_NAME) D
WHERE D.TABLESPACE_NAME = F.TABLESPACE_NAME
ORDER BY 1

--查詢表空間的free space
select tablespace_name,
       count(*) as extends,
       round(sum(bytes) / 1024 / 1024, 2) as MB,
       sum(blocks) as blocks
from dba_free_space
group by tablespace_name;

--查詢表空間的總容量
select tablespace_name, sum(bytes) / 1024 / 1024 as MB
from dba_data_files
group by tablespace_name;

--查詢表空間使用率
select total.tablespace_name,
       round(total.MB, 2) as Total_MB,
       round(total.MB - free.MB, 2) as Used_MB,
       round((1 - free.MB / total.MB) * 100, 2) || '%' as Used_Pct
from (select tablespace_name, sum(bytes) / 1024 / 1024 as MB
          from dba_free_space
         group by tablespace_name) free,
       (select tablespace_name, sum(bytes) / 1024 / 1024 as MB
          from dba_data_files
         group by tablespace_name) total
where free.tablespace_name = total.tablespace_name;



1.建立表空間:create tablespace test datafile '/u01/test.dbf'  size 10M uniform size 128k #指定區尺寸爲128k ,塊大小爲默認8K   #大文件表空間 create bigfile tablespace big_tbs datafile  '/u01/big_tbs.dbf ' size 100G

2.建非標準表show parameter db alter system set db_2k_cache_size=10M create tablespace test datafile '/u01/test.dbf' size 10M blocksize 2K uniform size 128k   #常見錯誤 SQL> alter system set db_2k_cache_size=2M; alter system set db_2k_cache_size=2M ERROR at line 1: ORA-02097: parameter cannot be modified because specified value is invalid ORA-00384: Insufficient memory to grow cache #解決 SQL> alter system set sga_max_size=400M scope=spfile; SQL> shutdown immediate; SQL> startup SQL> alter system set db_2k_cache_size=10M; System altered.

3.查看區大小與塊大小#區大小 conn y / 123 create table t(i number) tablespace test; Insert into t values(10) select bytes/1024 from user_segments where segment_name=upper('t'); #塊大小 Show parameter block(默認64K) #非標準表空間的blocksize SQL> select * from v$dbfile; SQL> select name,block_size,status from v$datafile; SQL> select block_size from v$datafile where file#=14;

4.刪除表空間drop tablespace test including contents and datafiles

5.查表空間:#查數據文件 select * from v$dbfile; #所有表空間 select * from v$tablespace; #表空間的數據文件 select file_name,tablespace_name from dba_data_files;

6.建立undo表空間create undo tablespace undotbs01 datafile '/u01/undotbs01.dbf' size 5M; #切換到新建的undo表空間 alter system set undo_tablespace=undotbs01;

7.建立臨時表空間create temporary tablespace temp_data tempfile '/u01/temp.db'  size 5M; create bigfile temporary tablespace bigtem tempfile '/u01/bigtemp.db'  size 5M;

8.改變表空間狀態
(0.)查看狀態#表空間狀態 select tablespace_name,block_size,status from dba_tablespaces; #數據文件狀態 select name,block_size,status from v$datafile;

(1.)表空間脫機alter tablespace test offline #如果意外刪除了數據文件 alter tablespace test offline for recover

(2.)表空間聯機alter tablespace test online

(3.)數據文件脫機select * from v$dbfile; alter database datafile 3 offline

(4.)數據文件聯機recover datafile 3; alter database datafile 3 online;

(5.)使表空間只讀alter tablespace test read only

(6.)使表空間可讀寫alter tablespace test read write;

9.擴展表空間#首先查看錶空間的名字和所屬文件及空間 select tablespace_name, file_id, file_name,round(bytes/(1024*1024),0) total_space from dba_data_files order by tablespace_name;   #三種擴展方法 1.alter tablespace test add datafile '/u01/test02.dbf' size 10M(自動加一個datafile) 2.alter database datafile '/u01/test.dbf' resize 20M; 3.alter database datafile '/u01/test.dbf' autoextend on next 10M maxsize 1G;   #設定後查看錶空間信息 select a.tablespace_name,a.bytes total,b.bytes used,c.bytes free,(b.bytes*100)/a.bytes "% used",(c.bytes*100)/a.bytes "% free" from sys.sm$ts_avail a,sys.sm$ts_used b,sys.sm$ts_free c where a.tablespace_name=b.tablespace_name and a.tablespace_name=c.tablespace_name;

10.移動表空間的數據文件#先確定數據文件據在表空間 SQL>select tablespace_name,file_name from dba_data_files where file_name='/u01/test.dbf'; #open狀態 SQL>alter tablespace test offline; SQL>host move /u01/test.dbf  /u01/oracle/test.dbf; SQL>alter tablespace test rename datafile '/u01/test.dbf' to '/u01/oracle/test.dbf'; SQL>alter tablespace test offline;   #mount狀態 SQL>shutdown immediate; SQL>startup mount SQL>host move /u01/test.dbf  /u01/oracle/test.dbf; SQL>alter database rename file '/u01/test.dbf' to '/u01/oracle/test.dbf';

11.表空間和數據文件常用的數據字典與動態性能視圖v$dbfile v$datafile dba_segments user_segments dba_data_files v$tablespace dba_tablespaces user_tablespaces

表空間擴充參考:http://www.dbmotive.com/oracle_error_codes.php?errcode=1658

 

原文地址:http://foolraty.javaeye.com/blog/626282

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章