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

本文探讨了在大数据平台迁移项目中遇到的Hive表数据问题,即表中有数据但Count(*)结果为0的情况。通过三种方法解决了这一问题,并详细解释了原因,涉及到Hive统计信息的自动收集配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前置

目前是做一个大数据平台迁移的项目,传输完数据后,在部署生产环境之前需要对功能模块进行测试,测试时对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语句进行分区。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值