oracle整库统计库表数据量--存储过程


方案一:

analyze: analyze table table_name compute statistics;

1、生成analyze的sql。

1 select 'analyze table '|| table_name || ' compute statistics; ' from user_tables;

2、批量执行analyze的sql。

3、查询统计结果

select t.table_name, t.NUM_ROWS from user_tables t

查询大于一百万数据量的表

1 select t.table_name,t.num_rows from user_tables t where t.num_rows > 1000000

方案二: 存储过程

1、创建存储过程 ----统计数据库表数据量

 1 CREATE OR REPLACE PROCEDURE TABLES_ROWS_COUNT
 2 AS
 3   CREATE_SQL    VARCHAR2(2000);
 4   P_TABLE_NAME  VARCHAR2(2000);
 5 
 6 BEGIN
 7   -- EXECUTE IMMEDIATE 'DROP TABLE CHECK_TABLE_COUNT';
 8    EXECUTE IMMEDIATE 'CREATE TABLE T_ROWS_COUNT (TABLE_NAME varchar2(200)  ,COUNT number)';
 9   FOR RESOURCE_OBJ IN (SELECT T.TABLE_NAME
10                          FROM USER_TABLES T
11                         ORDER BY T.TABLE_NAME) LOOP
12     P_TABLE_NAME  := RESOURCE_OBJ.TABLE_NAME;
13     CREATE_SQL := 'insert into  T_ROWS_COUNT(TABLE_NAME,COUNT)  SELECT '''  ||P_TABLE_NAME||      '''AS  TABLE_NAME , COUNT(1) AS COUNT  FROM ' || P_TABLE_NAME;
14     EXECUTE IMMEDIATE CREATE_SQL;
15     COMMIT;
16   END LOOP;
17 
18 END TABLES_ROWS_COUNT;

2、执行存储过程 

begin
  -- Call the procedure
  TABLES_ROWS_COUNT;
end;

3、输出统计结果

1 select * from T_ROWS_COUNT ;