Oracle 统计用户下表的数据量

   要想统计用户下所有表的数据量,可以查看user_tables,此表里面是统计信息,当然这个可能不太准,要想非常精确,需要直接count表。下面的脚本有异常不中断,可以重复执行的特点。

所属用户  表名                 分区字段   数据量      收集时间

OWNER TABLE_NAME      PART_COL  ROW_S     GATHER_TIME
LCA F_AMRETIREORDER     CREATE_TIME   9973    2017/9/12 10:35:07
LCA MENU_LOG            CREATE_TIME   9908156   2017/9/12 10:31:26
LCA     PUB_LOGS           9881080   2017/9/12 10:31:21
LCA FT_ACTROLE           CODE         9663    2017/9/12 10:34:46

create table bk_count_tables

(
 owner VARCHAR2(30),
 table_name VARCHAR2(30),
 part_col varchar2(100),--分区字段
 row_s number,
 gather_time date
);

create index ind_bct_own_table on bk_count_tables(owner,table_name);

set serveroutput on
declare
cursor c_cursor is  select s.OWNER, s.TABLE_NAME, col.column_name part_col
   from dba_tables s,
        (select owner,
                name,
                listagg(column_name, ',') within group(order by null) column_name
           from (select owner, name, column_name
                   from dba_part_key_columns
                  where owner in ('TEST')
                    and object_type = 'TABLE'
                    and name not like 'BIN$%'
                 union all
                 select owner, name, column_name
                   from dba_subpart_key_columns
                  where owner in ('TEST')
                    and object_type = 'TABLE'
                    and name not like 'BIN$%')
          group by owner, name) col
  where s.OWNER in ('TEST')
    and not regexp_like(table_name, '[0-9]{3,8}')
    and s.table_name not like '%BAK%'
    and s.table_name not like '%A2K%'
    and s.table_name not like 'BK%'
    and s.table_name not like 'BIN%'
    and s.OWNER = col.owner(+)
    and s.TABLE_NAME = col.name(+)
order by s.TABLE_NAME ;
c_row c_cursor%rowtype;
t_rows number;
begin
  for c_row in c_cursor loop
    begin
      execute immediate 'select count(*) from bk_count_tables where owner=:1 and TABLE_NAME=:2 and rownum=1' 
      into t_rows using c_row.OWNER,c_row.TABLE_NAME ;
      if(t_rows = 0) then 
        execute immediate 'select count(*) from "'||c_row.TABLE_NAME||'"' into t_rows;
        insert into bk_count_tables values(c_row.OWNER,c_row.TABLE_NAME,c_row.part_col,t_rows,sysdate);
        commit; 
       end if;
    EXCEPTION
     WHEN OTHERS THEN
      DBMS_OUTPUT.PUT_LINE(c_row.OWNER||'---'||c_row.TABLE_NAME);
      rollback; 
    end;
  end loop;
end;
/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值