HIVE实际开发问题:select * 和 select count(*) 条数不一样

前置

目前是做一个大数据平台迁移的项目,传输完数据后,在部署生产环境之前需要对功能模块进行测试,测试时对hive每张表都造了一条数据

问题情况

检查表的时候
sql语句:

select * from table;

结果显示是这样的:
在这里插入图片描述

select count(*) from table;

当我count(*)时候 结果显示是这样的
在这里插入图片描述
明明表里面有数据,但是count 的结果是0

解决方法

  • 方法一

     select count(*) from table  limit 1;
    

    在sql 后加了 一个limit 1 后 运行sql会执行 MapReduce
    结果正常显示

  • 方法二 很多人建议使用这种方法

    set hive.compute.query.using.stats=fasle; 
    

    这个设置有什么作用我们来看一下描述

    hive.compute.query.using.stats=true
    
    Instructs Hive to use statistics when generating query plans
    
  • 方法三 正解

    ANALYZE TABLE tablename COMPUTE STATISTICS;
    
    ANALYZE TABLE tablename partition(p=$1) COMPUTE STATISTICS;
    

原因

官方文档:Apache Hive Performance Tuning
在hive中有个配置 hive.stats.autogather

hive.stats.autogather=true

Enables automated gathering of table-level statistics for newly created tables and table partitions, such as tables created with 
the INSERT OVERWRITE statement. The parameter does not produce column-level statistics, such as those generated by
CBO. If disabled, administrators must manually generate the table-level statistics for newly generated tables and table
partitions with the ANALYZE TABLE statement.

支持自动收集新创建的表和表分区(如用其创建的表)的表级统计信息
INSERT OVERWRITE语句。该参数不会生成列级统计信息  
如果禁用,管理员必须手动为新生成的表和表生成表级统计信息
使用ANALYZE TABLE语句进行分区。

版权声明:本文为weixin_44421339原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。