如何在一个查询中计算(*)多个表,大小和表空间

时间:2012-12-11 15:02:47

标签: sql oracle

我有2个正确输出的工作查询。 第一个查询给出了表名和计数列表, 第二个查询给出了表名列表,以MB为单位的大小和表空间名称。

如何将它们组合成一个查询?

查询1:

select table_name
,to_number(extractvalue(xmltype(
dbms_xmlgen.getxml(
'select count(*) c from '||table_name))
,'/ROWSET/ROW/C')) count
from user_tables 
where table_name like 'AUD_2012080%'
order by table_name desc;

查询2:

select segment_name,blocks*8192/1024/1024 as MB,tablespace_name 
from dba_segments 
where segment_name like 'AUD_2012080%' 
order by segment_name desc;

输出1:

TABLE_NAME                          COUNT
------------------------------ ----------
AUD_20120809                        27967 
AUD_20120808                        28269 
AUD_20120807                        29354 
AUD_20120806                        27155 
AUD_20120805                        18042 
AUD_20120804                        19033 
AUD_20120803                        24642 
AUD_20120802                        26760 
AUD_20120801                        25776 

输出2:

SEGMENT_NAME                 MB TABLESPACE_NAME              
-------------------- ---------- ------------------------------
AUD_20120809                 35 WSS_BIG_I                      
AUD_20120808                 35 WSS_BIG_I                      
AUD_20120807                 36 WSS_BIG_I                      
AUD_20120806                 34 WSS_BIG_I                      
AUD_20120805                 24 WSS_BIG_I                      
AUD_20120804                 24 WSS_BIG_I                      
AUD_20120803                 29 WSS_BIG_I                      
AUD_20120802                 32 WSS_BIG_I                      
AUD_20120801                 30 WSS_BIG_I  

我要找的结果是:

Table_name    count   owner    MB   Tablespace_name
AUD_20120801  25776   auditor  30   wss_big_i 
...
...  

2 个答案:

答案 0 :(得分:3)

由于该段通常被命名为表:

SELECT ut.table_name,
       to_number(extractvalue(xmltype (dbms_xmlgen.getxml ('select count(*) c from ' ||ut.table_name)),'/ROWSET/ROW/C')) row_count,
       db.blocks*8192/1024/1024 as MB,
       db.tablespace_name 
FROM user_tables ut
  join dba_segments db on db.segment_name = ut.table_name
WHERE ut.table_name LIKE 'AUD_2012080%'
ORDER BY ut.table_name DESC

答案 1 :(得分:1)

解决方案是:

SELECT ut.table_name
, NVL(extractvalue(xmltype (dbms_xmlgen.getxml ('select max(ID) x from ' || ut.table_name)),'/ROWSET/ROW/X'),0) maxID 
FROM all_tables ut
LEFT JOIN cols c ON c.TABLE_NAME = ut.TABLE_NAME
where ut.owner = 'OWNER'
AND c.COLUMN_NAME = 'ID';