深度解析dba_segments和sys.seg$中的细节差异(下) (r5笔记第28天)
公众号:杨建荣的学习笔记 · 作者:r5笔记第28天 · 发布:2015-05-06 23:23:55 · 原文链接
继续昨天的内容 http://blog.itpub.net/23718752/viewspace-1624762/
我们已经根据dba_segments和sys.seg$的不同发现最后的差距有2T左右,已经定位到了dba_segments的一些细节信息,可以发现其实还是一个层级的调用关系。

我们把SYS_DBA_SEGS是一个处于中间层的角色,它的定义是3个union all,可以从定义中看到,差别主要还是segment_type的不同,我们采用逐个击破的方法,一个一个来看。
–>第一个子查询
select NVL(u.name, ‘SYS’), sum(s.blocks)
from sys.user$ u, sys.obj$ o, sys.ts$ ts, sys.sys_objects so, sys.seg$ s,
sys.file$ f
where s.file# = so.header_file
and s.block# = so.header_block
and s.ts# = so.ts_number
and s.ts# = ts.ts#
and o.obj# = so.object_id
and o.owner# = u.user# (+)
and s.type# = so.segment_type_id
and o.type# = so.object_type_id
and s.ts# = f.ts#
and s.file# = f.relfile#
and u.name=’PRDAPPO’
group by u.name
NVL(U.NAME,’SYS’) SUM(S.BLOCKS)
—————————— ————-
PRDAPPO 323983920
SQL> select 32398390*8192/1024/1024 size_MB from dual;
SIZE_MB
———-
253112.422
–>第二个子查询。
select NVL(u.name, ‘SYS’),sum( s.blocks)
from sys.user$ u, sys.ts$ ts, sys.undo$ un, sys.seg$ s, sys.file$ f
where s.file# = un.file#
and s.block# = un.block#
and s.ts# = un.ts#
and s.ts# = ts.ts#
and s.user# = u.user# (+)
and s.type# in (1, 10)
and un.status$ != 1
and un.ts# = f.ts#
and un.file# = f.relfile#
and u.name=’PRDAPPO’
group by u.name
no rows selected
–>第三个子查询
select NVL(u.name, ‘SYS’), sum( s.blocks)
from sys.user$ u, sys.ts$ ts, sys.seg$ s, sys.file$ f
where s.ts# = ts.ts#
and s.user# = u.user# (+)
and s.type# not in (1, 5, 6, 8, 10)
and s.ts# = f.ts#
and s.file# = f.relfile#
and u.name=’PRDAPPO’
group by u.name
no rows selected
所以看来主要的数据还是在第一个子查询,但是如果细想,有点奇怪啊,基表中查到的数据是2.6T左右。那剩下的2T还没有找到原因,到底差在哪了。
我们这个时候可以往回看,sys.seg$里的信息得到的是2.6T,dba_segments里面得到的信息是5T左右。那么唯一的差别就在于sys_dba_segs了,是不是这个中间表做了什么操作呢。
我们截取相关的字段查看一下。
select sum(decode(bitand(segment_flags, 131072), 131072, blocks,
(decode(bitand(segment_flags,1),1,
dbms_space_admin.segment_number_blocks(tablespace_id, relative_fno,
header_block, segment_type_id, buffer_pool_id, segment_flags,
segment_objd, blocks), blocks))))
from sys_dba_segs where owner=’PRDAPPO’ ;
SUM(DECODE(BITAND(SEGMENT_FLAGS,131072),131072,BLOCKS,(DECODE(BITAND(SEGMENT_FLA
——————————————————————————–
607401104
这下数字就对上了,可以看到在统计过程中,做了大量的判断,可以从下面改动的语句中做一些基本的分析。
SQL> select
sum(decode(bitand(segment_flags, 131072), 131072,blocks)) col1,
sum(decode(bitand(segment_flags,1),1,dbms_space_admin.segment_number_blocks(tablespace_id, relative_fno,
header_block, segment_type_id, buffer_pool_id, segment_flags,
segment_objd, blocks))) col2
from sys_dba_segs where owner=’PRDAPPO’ group by segment_flags ;
12860336 12860336
4145504
209686704 210292912
385152992
