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

显示结果如下:

查看表空间使用情况(SQL)-风君雪科技博客

参考: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;

显示结果如下:

查看表空间使用情况(SQL)-风君雪科技博客

增加使用该表空间的用户信息:

--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

写的比较急,没有进行仔细优化,希望大家多多指教!