/****************1、建立临时表********************/
create table temp_table_size
(
table_name varchar2(30)not null,--表名
main_sizenumber(12,3)default 0 not null,--表主段空间(单位:MB)
lob_size number(12,3)default 0 not null,--LOB大字段空间
data_sizenumber(12,3)default 0 not null,--数据空间(主空间 + LOB)
index_size number(12,3)default 0 not null,--索引空间
total_size number(12,3)default 0 not null,--总占用空间(加索引空间)
record_count number(15)default 0 not null --记录数
);
alter table temp_table_size add constraint pk_temp_table_size primary key (table_name);
insert into temp_table_size (table_name) select table_name from user_tables;
commit;
/**************2、表各项统计*****************/
declare
v_size_1 number(12,3);
v_size_2 number(12,3);
v_size_3 number(12,3);
v_countnumber(15);
begin
--数据初始化
update temp_table_size
set main_size= 0,
lob_size = 0,
data_size= 0,
index_size = 0,
total_size = 0,
record_count = 0;
for v_rec in (select table_name from user_tables) loop
--主数据段空间
select sum(bytes) / 1024 / 1024
into v_size_1
from user_segments
where segment_name = v_rec.table_name;
--LOB空间
select nvl(sum(bytes),0) / 1024 / 1024
into v_size_2
from user_segments
where segment_name in
(
select segment_name
from user_lobs
where table_name= v_rec.table_name
);
--索引空间
select nvl(sum(bytes),0) / 1024 / 1024
into v_size_3
from user_segments
where segment_name in
(
select index_name
from user_indexes
where table_name= v_rec.table_name
);
--表记录数统计
execute immediate 'select count(*) from ' v_rec.table_name into v_count;
--写统计结果
update temp_table_size
set main_size= v_size_1,
lob_size = v_size_2,
index_size = v_size_3,
data_size= v_size_1 + v_size_2,
total_size = v_size_1 + v_size_2 + v_size_3,
record_count = v_count
where table_name = v_rec.table_name;
end loop;
--保存结果
commit;
end;
/
/**************3、查看统计结果***************/
select *
from temp_table_size
order by table_name;