表空間查詢和管理

wangwenan6發表於2014-02-28

紅色是自由指定的~~
--查詢表空間
SELECT D.TABLESPACE_NAME,
       SPACE "SUM_SPACE(M)",
       SPACE - NVL(FREE_SPACE, 0) "USED_SPACE(M)",
       ROUND((1 - NVL(FREE_SPACE, 0) / SPACE) * 100, 2) "USED_RATE(%)",
       FREE_SPACE "FREE_SPACE(M)",
       MAX_SPACE,
       ROUND(((SPACE - NVL(FREE_SPACE, 0)) / MAX_SPACE) * 100, 2) "USED_RATE_MAX_SIZE(%)"
  FROM (SELECT TABLESPACE_NAME,
               ROUND(SUM(BYTES) / (1024 * 1024), 2) SPACE,
               ROUND(SUM(DECODE(SIGN(MAXBYTES - BYTES), 1, MAXBYTES, BYTES)) / 1024 / 1024,
                     2) MAX_SPACE
          FROM DBA_DATA_FILES
         GROUP BY TABLESPACE_NAME) D,
       (SELECT TABLESPACE_NAME,
               ROUND(SUM(BYTES) / (1024 * 1024), 2) FREE_SPACE
          FROM DBA_FREE_SPACE
         GROUP BY TABLESPACE_NAME) F
 WHERE D.TABLESPACE_NAME = F.TABLESPACE_NAME(+)
UNION ALL
SELECT D.TABLESPACE_NAME,
       SPACE "SUM_SPACE(M)",
       SPACE - NVL(FREE_SPACE, 0) "USED_SPACE(M)",
       ROUND((1 - NVL(FREE_SPACE, 0) / SPACE) * 100, 2) "USED_RATE(%)",
       FREE_SPACE "FREE_SPACE(M)",
       MAX_SPACE,
       ROUND(((SPACE - NVL(FREE_SPACE, 0)) / MAX_SPACE) * 100, 2) "USED_RATE_MAX_SIZE(%)"
  FROM (SELECT TABLESPACE_NAME,
               ROUND(SUM(BYTES) / (1024 * 1024), 2) SPACE,
               ROUND(SUM(DECODE(SIGN(MAXBYTES - BYTES), 1, MAXBYTES, BYTES)) / 1024 / 1024,
                     2) MAX_SPACE
          FROM DBA_TEMP_FILES
         GROUP BY TABLESPACE_NAME) D,
       (SELECT TABLESPACE_NAME,
               ROUND(SUM(BYTES_FREE) / (1024 * 1024), 2) FREE_SPACE
          FROM V$TEMP_SPACE_HEADER
         GROUP BY TABLESPACE_NAME) F
 WHERE D.TABLESPACE_NAME = F.TABLESPACE_NAME(+)
------------------------------------------   
--檢視錶空間屬性--
SELECT T.TABLESPACE_NAME,D.FILE_NAME,D.AUTOEXTENSIBLE,D.BYTES,D.MAXBYTES,D.STATUS  
FROM DBA_TABLESPACES T,DBA_DATA_FILES D  
WHERE T.TABLESPACE_NAME =D.TABLESPACE_NAME and T.TABLESPACE_NAME='DATA
ORDER BY TABLESPACE_NAME,FILE_NAME;
--新增資料檔案
ALTER TABLESPACE DATA ADD DATAFILE '/data/data01.dbf' SIZE 8192M;
--修改資料檔案大小(儘量避免改小,貌似有HWM的問題,要修改的話,貌似先整理表空間碎片?)
ALTER DATABASE DATAFILE '/data/data_01.dbf' RESIZE 8192M;
--為了防止把硬碟撐爆,關閉自動擴充比較好。
ALTER DATABASE DATAFILE '/data/data_01.dbf' AUTOEXTEND OFF;

-------------------------------------

----釋放磁碟空間-----
--首先用語句查詢容量大於1G的資料段
select segment_name,sum(bytes)/1024/1024/1024 as GB from dba_segments group by segment_name having sum(bytes)/1024/1024/1024>1
/*得到如下結果:
SYS_LOB0000136091C00003$$      255332M
SYS_LOB0000136441C00004$$      7170M
SYS_C0082042                   1305M
SYS_C0080433                   1340M*/
--根據LOB段查詢該該lob段屬於哪個表
select table_name,segment_name from dba_lobs where segment_name='SYS_LOB0000136091C00003$$'
/*經查得知是ADU_*表佔了很多容量
3.用Dbvisulizer/plsql連到資料庫,刪除重複的資料行,但刪除這些重複的資料後,並不會釋放出磁碟空間
4.然後釋放lob型別資料佔據的空間*/
alter table adu_* move tablespace BFPICK lob(content) store as (tablespace bfpick)
--之後就釋放了重複資料所佔的空間了,然後在對該表重建索引
alter index ***** rebuild;
----------------------------------------
--查詢程式數/操作使用者--
select count(*) from v$process
select value from v$parameter where name = 'processes'


SELECT osuser, a.username,cpu_time/executions/1000000||'s', sql_fulltext,machine 
from v$session a, v$sqlarea b 
where a.sql_address =b.address order by cpu_time/executions desc
-----------------------------------------
--清理分割槽表
ALTER TABLE table_1 truncate PARTITION PART_201303;
ALTER TABLE table_1 DROP PARTITION PART_201303;
ALTER TABLE table_1 truncate PARTITION PART_201304;
ALTER TABLE table_1 DROP PARTITION PART_201304;

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29510932/viewspace-1096957/,如需轉載,請註明出處,否則將追究法律責任。

相關文章