oracle 数据库下所有表结构、数据量及缺失值统计

  查用户表

select * from all_tab_comments   --查询所有用户的表,视图等。
select * from all_col_comments   --查询所有用户的表的列名和注释。
select * from all_tab_columns    --查询所有用户的表的列名等信息。

select * from user_tab_comments   --查询本用户的表,视图等。
select * from user_col_comments   --查询本用户的表的列名和注释。
select * from user_tab_columns    --查询本用户的表的列名等信息。

select * from all_tab_partitions
select * from user_tab_partitions

--ORACLE下有三个视图
DBA_TABLES       --拥有DBA角色的用户可以查看系统中的所有表
USER_TABLES      --登录数据库的当前用户拥有的所有表
ALL_TABLES       --登录数据库的当前用户有权限查看的所有表

  表结构

SELECT
    t1.TABLE_NAME,
    t1.COLUMN_NAME,
    t1.DATA_TYPE || '(' || t1.DATA_LENGTH || ')',
    t2.COMMENTS
    -- missing_count(t1.TABLE_NAME, t1.COLUMN_NAME) counts
FROM
    USER_TAB_COLS t1, USER_COL_COMMENTS t2
WHERE
    t1.TABLE_NAME = t2.TABLE_NAME
    AND t1.COLUMN_NAME = t2.COLUMN_NAME
ORDER BY
    t1.TABLE_NAME, t1.COLUMN_NAME

  参考资料:oracle数据库导出所有表结构

  数据量

  直接执行下面代码

select t.table_name, t.num_rows from user_tables t

  如果上述代码无法得到结果,或数据不全,可执行下述代码

-- 新建函数
create or replace function count_rows(table_name in varchar2,
                                                        owner         in varchar2 default null)
  return number authid current_user IS
    num_rows number;
    stmt     varchar2(2000);
begin
    if owner is null then
        stmt := 'select count(*) from "' || table_name || '"';
    else
        stmt := 'select count(*) from "' || owner || '"."' || table_name || '"';
    end if;

    execute immediate stmt into num_rows;
    return num_rows;
end; 

-- 查询
select table_name, count_rows(table_name) nrows from user_tables

  参考资料:Oracle查询数据库中所有表的记录数

  缺失值统计

  经测试,未建立索引时,5000万数据量,使用sum(decode())用时82s,而使用count where用时105s,显然,sum(decode()) 效率更高;建立索引之后,前者用时7.57s,后者用时8.564s,此时count where效率更高。综合来看,推荐使用sum(decode()).

-- 缺失值统计函数
create or replace function missing_count(table_name in varchar2,
										 col_name in varchar2,
                                         owner      in varchar2 default null)
  return number authid current_user IS
    counts number;
    stmt     varchar2(2000);
begin
    if owner is null then
        stmt := 'select sum(decode(' || col_name || ', null, 1, 0)) from ' || table_name;
        --stmt := 'select count(*) from ' || table_name || ' where ' || col_name || ' is null';
    else
        stmt := 'select sum(decode(' || col_name || ', null, 1, 0)) from ' || owner || '.' || table_name;
        --stmt := 'select count(*) from ' || owner || '.' || table_name || ' where ' || col_name || ' is null';
    end if;
 
    execute immediate stmt into counts;
    return counts;
end;

-- 应用见-表结构

   参考链接:python 连接 oracle 统计指定表格所有字段的缺失值数

原文地址:https://www.cnblogs.com/iupoint/p/10916874.html