zoukankan      html  css  js  c++  java
  • 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 统计指定表格所有字段的缺失值数

  • 相关阅读:
    1000F.One Ocurrence(可持久化线段树+思维)
    P2184.贪婪大陆(思维+树状数组)
    438D.The Child and Sequence(线段树+取模性质)
    P2894 [USACO08FEB]Hotel G(线段树维护区间子串)
    620E New Year Tree(线段树维护状压)
    P6492 [COCI2010-2011#6] STEP(线段树维护最长子串)
    242E.XOR on segment(线段树维护区间异或)
    1527D.MEX Tree(树上分类讨论+容斥)
    解决for循环中写异步函数,异步函数中输出下标一样问题
    vue拦截器
  • 原文地址:https://www.cnblogs.com/iupoint/p/10916874.html
Copyright © 2011-2022 走看看