获取表空间输出

时间:2017-02-15 18:22:19

标签: oracle

我们的oracle Oracle企业管理器数据库版本是12.1.0.2。我试图在我的下面的查询中获取所有表空间总大小,used_size,free_size。查询给我一个错误。有了这个查询,我想在下面显示 1)GB中的目标名称,表空间名称,total_space,free_space,used_space。 2)我想要没有逗号的列别名 写了afiedt.buf文件

  1  select  * from
  2    ( select target_name,KEY_VALUE NAME
  3  ,decode(column_label,'Tablespace Allocated Space (MB)' ,total_space
  4                      ,'Tablespace Free Space (MB)',free_space,'Tablespace Us
ed Space (MB)',used_space,column_label) as column_label
  5                     ,value
  6      from sysman.mgmt$metric_current
  7    where COLUMN_LABEL IN('Tablespace Allocated Space (MB)','Tablespace Used
Space (MB)','Tablespace Free Space (MB)')
  8    and target_type = 'rac_database'
  9    )
 10      PIVOT(
 11    MAX(VALUE)
 12*   FOR COLUMN_LABEL IN( 'total_space ','used_space','free_space'))
SQL> /
                    ,'Tablespace Free Space (MB)',free_space,'Tablespace Used Sp
ace (MB)',used_space,column_label) as column_label

          *
ERROR at line 4:
ORA-00904: "USED_SPACE": invalid identifier


SQL>

2 个答案:

答案 0 :(得分:1)

我想你想要:

select  * from
      ( select target_name,KEY_VALUE NAME
    ,decode(column_label,'Tablespace Allocated Space (MB)' ,'total_space'
                        ,'Tablespace Free Space (MB)','free_space'
                        ,'Tablespace Used Space (MB)','used_space'
                        ,column_label) as column_label
                       ,value
        from sysman.mgmt$metric_current
      where COLUMN_LABEL IN('Tablespace Allocated Space (MB)','Tablespace Used Space (MB)','Tablespace Free Space (MB)')
      and target_type = 'rac_database'
      )
       PIVOT(
     MAX(VALUE)
    FOR COLUMN_LABEL IN( 'total_space','used_space','free_space'))

虽然在我的数据库(11g)上,我看不到标签"表空间自由空间(MB)"

注意,如果你想为表空间提供空间,你可以使用dba_free_space:

select tablespace_name, sum(bytes)/1024/1024 as mb_free 
from dba_free_space
group by tablespace_name;

答案 1 :(得分:0)

我认为你可以比@tbone回答更进一步简化:

select *
from (
  select column_label, key_value as tablespace_name, value
  from sysman.mgmt$metric_current
  where column_label in ('Tablespace Allocated Space (MB)',
    'Tablespace Used Space (MB)', 'Tablespace Free Space (MB)')
  and target_type = 'rac_database'
)
pivot (
  max(value)
  for column_label in ('Tablespace Allocated Space (MB)' as total_space,
    'Tablespace Used Space (MB)' as used_space, 'Tablespace Free Space (MB)' as free_space)
)
/

其中(在11gR2中)输出如下:

TABLESPACE_NAME                TOTAL_SPACE          USED_SPACE           FREE_SPACE          
------------------------------ -------------------- -------------------- --------------------
SYSAUX                         1770                 1630.25                                  
SYSTEM                         730                  727.125                                  
TEMP                           2009                 0                                        
UNDOTBS1                       3469                 20.5                                     
USERS                          832.25               54.0625                                  
...

在此版本中,mgmt$metric_current表没有可用空间条目;我目前无法检查是否已在12c中添加,但您可以从其他两个中进行计算。

如果您希望GB中的值而不是MD在子查询中除以1024,可能会舍入到合理的位数:

select *
from (
  select column_label, key_value as tablespace_name,
    round(to_number(value)/1024, 2) as value
  from sysman.mgmt$metric_current
  where column_label in ('Tablespace Allocated Space (MB)',
    'Tablespace Used Space (MB)', 'Tablespace Free Space (MB)')
  and target_type = 'rac_database'
)
pivot (
  max(value)
  for column_label in ('Tablespace Allocated Space (MB)' as total_space,
    'Tablespace Used Space (MB)' as used_space,
    'Tablespace Free Space (MB)' as free_space)
)
/

TABLESPACE_NAME                         TOTAL_SPACE           USED_SPACE           FREE_SPACE
------------------------------ -------------------- -------------------- --------------------
UNDOTBS1                                       3.39                  .02                     
TEMP                                           1.96                    0                     
SYSAUX                                         1.73                 1.59                     
USERS                                           .81                  .05                     
SYSTEM                                          .71                  .71                     
...

正如@tbone还建议的那样,还有其他一些视图可以提供这些信息,但在我的数据库上至少这个速度要慢得多:

select dfs.tablespace_name,
  (select round(sum(ddf.bytes)/power(1024, 3), 2) from dba_data_files ddf
    where ddf.tablespace_name = dfs.tablespace_name) as total_space,
  (select round(sum(ds.bytes)/power(1024, 3), 2) from dba_segments ds
    where ds.tablespace_name = dfs.tablespace_name) as used_space,
  round(sum(dfs.bytes)/power(1024, 3), 2) as free_space
from dba_free_space dfs
group by dfs.tablespace_name
/

TABLESPACE_NAME                         TOTAL_SPACE           USED_SPACE           FREE_SPACE
------------------------------ -------------------- -------------------- --------------------
SYSAUX                                         1.73                 1.59                  .13
UNDOTBS1                                       3.39                  .08                 3.31
USERS                                           .81                  .05                  .76
SYSTEM                                          .71                  .71                    0
...

...并且根据有关sysman拥有您正在查看的表的评论,或许您在连接到EM时无法看到DBA视图。

如果要对结果进行进一步计算,可以将数据结果放入内联视图或CTE,例如获取所用空间的百分比:

with cte as (
  select *
  from (
    select column_label, key_value as tablespace_name,
      to_number(value)/1024 as value
    from sysman.mgmt$metric_current
    where column_label in ('Tablespace Allocated Space (MB)',
      'Tablespace Used Space (MB)', 'Tablespace Free Space (MB)')
    and target_type = 'rac_database'
  )
  pivot (
    max(value)
    for column_label in ('Tablespace Allocated Space (MB)' as total_space,
      'Tablespace Used Space (MB)' as used_space,
      'Tablespace Free Space (MB)' as free_space)
  )
)
select tablespace_name, round(total_space, 2) as total_space,
  round(used_space, 2) as used_space, round(free_space, 2) as free_space,
  round(100 * used_space / total_space, 2) as pct_used
from cte;

在11g中,您还可以从总空间和已用空间轻松计算出自由空间:

with cte as (
  select *
  from (
    select column_label, key_value as tablespace_name,
      to_number(value)/1024 as value
    from sysman.mgmt$metric_current
    where column_label in ('Tablespace Allocated Space (MB)',
      'Tablespace Used Space (MB)')
    and target_type = 'rac_database'
  )
  pivot (
    max(value)
    for column_label in ('Tablespace Allocated Space (MB)' as total_space,
      'Tablespace Used Space (MB)' as used_space)
  )
)
select tablespace_name, round(total_space, 2) as total_space,
  round(used_space, 2) as used_space,
  round(total_space - used_space, 2) as free_space,
  round(100 * used_space / total_space, 2) as pct_used
from cte;

TABLESPACE_NAME                         TOTAL_SPACE           USED_SPACE           FREE_SPACE   PCT_USED
------------------------------ -------------------- -------------------- -------------------- ----------
UNDOTBS1                                       3.39                  .02                 3.37        .59
TEMP                                           1.96                    0                 1.96          0
SYSAUX                                         1.73                 1.59                  .14       92.1
USERS                                           .81                  .05                  .76        6.5
SYSTEM                                          .71                  .71                    0      99.61
...