Home > Database > Mysql Tutorial > Why Does a Hive `COUNT(*)` with a Non-Null Condition Return a Higher Count Than a Simple `COUNT(*)`?

Why Does a Hive `COUNT(*)` with a Non-Null Condition Return a Higher Count Than a Simple `COUNT(*)`?

Mary-Kate Olsen
Release: 2025-01-12 06:59:46
Original
260 people have browsed it

Why Does a Hive `COUNT(*)` with a Non-Null Condition Return a Higher Count Than a Simple `COUNT(*)`?

*Hive count () exception: SELECT with non-null condition returns higher count**

In Hive, a special behavior was observed: the row count with non-empty condition returned a higher value than the count of all rows. Let's investigate this anomaly.

The table named "mytable" contains a field named "master_id". Using the count(*) function, we get:

<code class="language-sql">select count(*) as c from mytable;
c
1129563</code>
Copy after login

However, when adding a condition to count only non-null values ​​of "master_id", we get a much larger count:

<code class="language-sql">select count(*) as c from mytable where master_id is not null;
c
1134041</code>
Copy after login

Surprisingly, a separate query shows that "master_id" has no null values:

<code class="language-sql">select count(*) as c from mytable where master_id is null;
c
0</code>
Copy after login

Why does adding a non-null condition produce a higher count when there are no null values?

The most likely explanation lies in Hive usage statistics. By default, Hive uses statistics to optimize query planning by estimating the number of rows. However, these statistics are not always accurate.

To solve this problem, disable the usage of statistics by setting the parameter:

<code class="language-sql">set hive.compute.query.using.stats=false;</code>
Copy after login

Re-executing the query should now produce consistent results.

Alternatively, you can calculate statistics manually using ANALYZE TABLE syntax.

Additionally, setting hive.stats.autogather=true will automatically collect statistics during INSERT OVERWRITE operations, ensuring more accurate statistics and preventing such anomalies.

The above is the detailed content of Why Does a Hive `COUNT(*)` with a Non-Null Condition Return a Higher Count Than a Simple `COUNT(*)`?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template