一般来说,查表占据了多少的物理空间用的是dba_segments这个数据字典,这个数据字典展示了segment_type(段类型,如table,index,function,procedure 等等的段类型),bytes(段大小),blocks(段占用了多少个数据块)extents(分配了多少个区)等吧,信息很详细,可以直接select * from dba_segments;查看,用具有查看数据字典权限的用户查看。(给用户授予了SELECT_CATALOG_ROLE角色,普通用户就具有了查询数据字典的权限。)一般不要给普通用户dba角色,保持权限最小化原则。
1. 查看一般表的大小
select segment_name,segment_type,bytes/1024/1024 M from dba_segments where segment_name='SETTINGS$' and segment_type='TABLE';
2. 查看带有LOB字段的表的大小
LOB字段是有额外的专门区域存放,查看带有LOB字段的表的大小比较复杂,分为了普通的字段大小,LOB字段大小,LOB索引字段大小,三个部分。
- SELECT (SELECT SUM(S.BYTES/1024/1024/1024)
- -- The Table Segment size
- FROM DBA_SEGMENTS S WHERE S.OWNER = UPPER('表的owner') AND (S.SEGMENT_NAME = UPPER('表名'))) + (SELECT
- SUM(S.BYTES/1024/1024/1024)
- -- The Lob Segment Size
- FROM DBA_SEGMENTS S, DBA_LOBS L WHERE S.OWNER = UPPER('表的owner') AND (L.SEGMENT_NAME = S.SEGMENT_NAME AND L.TABLE_NAME = UPPER('表名') AND L.OWNER =
- UPPER('表的owner'))) + (SELECT SUM(S.BYTES/1024/1024/1024)
- -- The Lob Index size
- FROM DBA_SEGMENTS S, DBA_INDEXES I WHERE S.OWNER = UPPER('表的owner') AND
- (I. INDEX_NAME = S.SEGMENT_NAME AND I.TABLE_NAME = UPPER('表名') AND INDEX_TYPE= 'LOB' AND I.OWNER = UPPER('表的owner'))) "TOTAL TABLE SIZE" FROM DUAL;
详细介绍内容可以查看Doc ID 118531.1
如果想查看这个库里有多少个带有LOB字段的表呢?通过dba_lobs可以查看。
例如:select distinct owner,table_name from dba_lobs where owner in ('***',’***’);
如果搜出几百个几千个LOB字段的表,一个个查大小,会很久…………
怎么把带有LOB字段的表一次性列出来呢?
先创建一个临时表:
- SQL> create table candidates (owner varchar2(30),segment_name varchar2(81));
再把上面查出来的带有LOB字段的表插入进临时表里:
- SQL> insert into candidates values ('owner','table_name');
然后利用游标:
- SQL> set serveroutput on
- SQL> declare
- cursor cur_temp is select owner, segment_name from candidates;
- v_owner varchar2(30);
- v_segment_name varchar2(81);
- v_total_table_size number;
- begin
- open cur_temp;
- fetch cur_temp into v_owner,v_segment_name;
- while cur_temp%FOUND
- loop
- SELECT
- (
- SELECT
- nvl(SUM(S.BYTES/1024/1024/1024),0)
- FROM
- DBA_SEGMENTS S
- WHERE
- S.OWNER = UPPER(v_owner) AND
- (S.SEGMENT_NAME = UPPER(v_segment_name)))
- +
- (SELECT
- nvl(SUM(S.BYTES/1024/1024/1024),0)
- FROM
- DBA_SEGMENTS S,
- DBA_LOBS L
- WHERE
- S.OWNER = UPPER(v_owner) AND
- (L.SEGMENT_NAME = S.SEGMENT_NAME AND L.TABLE_NAME = UPPER(v_segment_name) AND
- L.OWNER = UPPER(v_owner))
- )
- +
- (SELECT
- nvl(SUM(S.BYTES/1024/1024/1024),0)
- FROM
- DBA_SEGMENTS S,
- DBA_INDEXES I
- WHERE
- S.OWNER = UPPER(v_owner) AND
- (I.INDEX_NAME = S.SEGMENT_NAME AND I.TABLE_NAME = UPPER(v_segment_name) AND INDEX_TYPE= 'LOB' AND I.OWNER = UPPER(v_owner))
- ) "TOTAL TABLE SIZE" into v_total_table_size
- FROM
- DUAL;
- DBMS_OUTPUT.PUT_LINE('Table '||v_segment_name||': '||v_total_table_size);
- fetch cur_temp into v_owner,v_segment_name;
- end loop;
- end;
- /
就会把带有LOB字段的表大小都列出来了。
一般查大小就只查dba_segments了,查LOB的时候好像不多。偶尔查一下就用步骤2的方法就行,也是官方提供的。