查看錶空間使用狀況(SQL)

   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  "表空間名"

顯示結果以下:html

image

參考:http://www.javashuo.com/article/p-vthuqckt-bm.html數據庫

本身進行了一些優化,根據表空間的建立時間進行排序。在優化時之因此沒有使用三表直接聯合查詢(from a,b,c where a.xx=b.xx and b.xx=c.xx的方式),是爲了提升查詢效率。app

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;

顯示結果以下:優化

增長使用該表空間的用戶信息:spa

--1查詢表空間以及其使用狀況以及對應用戶
select zzz.TABLESPACE_NAME "表空間名",
       zzz.TOT_GROOTTE_MB  "表空間大小(M)",
       zzz.incrementSize   "自增加大小(M)",
       zzz.use_percentage  "使用比",
       zzz.CREATE_TIME     "建立時間",
       xxx.all_users       "用戶名"
  from (SELECT A.TABLESPACE_NAME,
               D.TOT_GROOTTE_MB,
               D.TOT_GROOTTE_MB - A.TOTAL_BYTES,
               To_char(Round((D.TOT_GROOTTE_MB - A.TOTAL_BYTES) /
                             D.TOT_GROOTTE_MB * 100,
                             2),
                       '990.99') || '%' use_percentage,
               A.TOTAL_BYTES freesize,
               A.MAX_BYTES maxblock,
               D.AUTOEXTENSIBLE,
               D.incrementSize,
               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) zzz
  left join (select t.default_tablespace,
                    to_char(wmsys.wm_concat(username)) all_users
               from dba_users t
              group by t.default_tablespace) xxx
    on zzz.TABLESPACE_NAME = xxx.default_tablespace

寫的比較急,沒有進行仔細優化,但願你們多多指教!3d

相關文章
相關標籤/搜索