Skip to content

Inconsistent results in count queries #20

Open
@eclbg

Description

@eclbg

The following two queries yield different results while I understand they should be equivalent. The second one yields the correct result.

SELECT COUNT(1) FROM (SELECT DISTINCT report_id FROM clickhouse_reportusages) t;
 count
-------
  2000
SELECT COUNT(DISTINCT report_id) FROM clickhouse_reportusages;
 count
-------
  2956

Their respective plans are:

EXPLAIN VERBOSE SELECT COUNT(1) FROM (SELECT DISTINCT report_id FROM clickhouse_reportusages) t;
                                            QUERY PLAN
--------------------------------------------------------------------------------------------------
 Aggregate  (cost=1.51..1.52 rows=1 width=8)
   Output: count(1)
   ->  Unique  (cost=1.00..1.50 rows=1 width=4)
         Output: clickhouse_reportusages.report_id
         ->  Foreign Scan on public.clickhouse_reportusages  (cost=1.00..-1.00 rows=1000 width=4)
               Output: clickhouse_reportusages.report_id
               Remote SQL: SELECT report_id FROM "default".reportusages ORDER BY report_id ASC
(7 rows)
EXPLAIN VERBOSE SELECT COUNT(DISTINCT report_id) FROM clickhouse_reportusages;
                                 QUERY PLAN
----------------------------------------------------------------------------
 Foreign Scan  (cost=1.00..-1.00 rows=1000 width=8)
   Output: (count(DISTINCT report_id))
   Relations: Aggregate on (clickhouse_reportusages)
   Remote SQL: SELECT count(DISTINCT report_id) FROM "default".reportusages
(4 rows)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions