COUNT(DISTINCT) returning incorrect results on Pri...
# questions-and-troubleshooting
a
COUNT(DISTINCT) returning incorrect results on Primary Key table Hello everyone, We’re experiencing an issue where
COUNT(DISTINCT)
is returning incorrect results on a Primary Key table. The table has a composite primary key
(agency_id, _id)
. Table DDL (simplified):
Copy code
CREATE TABLE `agent_contact_base` (
  `agency_id` varchar(48) NOT NULL,
  `_id` varchar(48) NOT NULL,
  -- other columns omitted for brevity
) ENGINE=OLAP
PRIMARY KEY(`agency_id`, `_id`)
DISTRIBUTED BY HASH(`agency_id`) BUCKETS 24
Issue: When counting distinct
_id
values for a specific agency, we get incorrect results:
Copy code
SELECT
    COUNT(_id) as count_id,
    COUNT(DISTINCT _id) as count_distinct_id
FROM agent_contact_base
WHERE agency_id = 'a4c5be3c-8097-4bb4-8b84-1a93f1e629be';
Result:
Copy code
count_id,count_distinct_id
3622868,720896
However, when we include the full primary key in the DISTINCT clause: sql
Copy code
SELECT
    COUNT(_id) as count_id,
    COUNT(DISTINCT _id) as count_distinct_id,
    COUNT(DISTINCT agency_id, _id) as count_distinct_pk
FROM agent_contact_base
WHERE agency_id = 'a4c5be3c-8097-4bb4-8b84-1a93f1e629be';
Result:
Copy code
count_id,count_distinct_id,count_distinct_pk
3622868,3622868,3622868
Additional context: •
_id
values are unique across our entire dataset (no duplicates even across different agencies) • We perform frequent upserts using both
agency_id
and
_id
using Kafka Connector • Verified no duplicate
_id
values exist within the agency using GROUP BY Is this a known issue with COUNT(DISTINCT) on Primary Key tables which have composite Keys? Any workarounds besides including the full PK in the DISTINCT clause?
k
will try to repro it. is it a shared nothing cluster or shared data cluster?
a
Thanks Kevin, it is a shared nothing cluster.
k
@Ashish Gupta can you provide the two query profiles, one is the
count(_id), count(DISTINCT _id)
and the other is
count(_id), count(DISTINCT _id), count(DISTINCT agency_id, _id)
?
a
For
explain analyze select count(_id), count(DISTINCT _id) from agent_contact_base c where agency_id = 'a4c5be3c-8097-4bb4-8b84-1a93f1e629be';
Profile:
Copy code
Explain String
[0mSummary[0m
    [0mQueryId: 1c8758ef-9708-11f0-b69b-3e4d8b108793[0m
    [0mVersion: 3.5.5-fd4e51b[0m
    [0mState: Finished[0m
    [0mTotalTime: 434ms[0m
"        [0mExecutionTime: 427.173ms [Scan: 50.763ms (11.88%), Network: 0ns (0.00%), ResultDeliverTime: 0ns (0.00%), ScheduleTime: 424.411ms (99.35%)][0m"
        [0mCollectProfileTime: 2ms[0m
        [0mFrontendProfileMergeTime: 1.334ms[0m
"    [0mQueryPeakMemoryUsage: ?, QueryAllocatedMemoryUsage: 810.219 MB[0m"
    [0mTop Most Time-consuming Nodes:[0m
"        [1m[31m1. AGGREGATION (id=2) [finalize, update]: 419.874ms (88.63%)[0m"
        [0m2. OLAP_SCAN (id=0) : 53.186ms (11.23%)[0m
        [0m3. PROJECT (id=1) : 544.627us (0.11%)[0m
        [0m4. RESULT_SINK: 146.238us (0.03%)[0m
    [0mTop Most Memory-consuming Nodes:[0m
    [0mNonDefaultVariables:[0m
        [0mbig_query_profile_threshold: 30s -> 1500ms[0m
        [0mcharacter_set_results: utf8 -> utf8mb4[0m
        [0menable_adaptive_sink_dop: false -> true[0m
        [0menable_async_profile: true -> false[0m
        [0menable_lake_tablet_internal_parallel: true -> false[0m
        [0menable_profile: false -> true[0m
        [0mnet_write_timeout: 60 -> 600[0m
        [0msql_mode_v2: 32 -> 2097184[0m
[0mFragment 0[0m
│   [0mBackendNum: 1[0m
"│   [0mInstancePeakMemoryUsage: 306.277 MB, InstanceAllocatedMemoryUsage: 810.219 MB[0m"
│   [0mPrepareTime: ?[0m
└──[0mRESULT_SINK[0m
   │   [0mTotalTime: 146.238us (0.03%) [CPUTime: 146.238us][0m
   │   [0mOutputRows: 1[0m
   │   [0mSinkType: MYSQL_PROTOCAL[0m
"   └──[1m[31mAGGREGATION (id=2) [finalize, update][0m"
"      │   [1m[31mEstimates: [row: 1, cpu: 1321751.17, memory: 16.00, network: 0.00, cost: 1321783.17][0m"
      │   [1m[31mTotalTime: 419.874ms (88.63%) [CPUTime: 419.874ms][0m
      │   [1m[31mOutputRows: 1[0m
"      │   [1m[31mPeakMemory: ?, AllocatedMemory: ?[0m"
"      │   [1m[31mAggExprs: [count(2: _id), multi_distinct_count(2: _id)][0m"
      │   [1m[31mSubordinateOperators: [0m
      │       [1m[31mLOCAL_EXCHANGE [Passthrough][0m
      │   [1m[31mDetail Timers: [0m
      │       [1m[31mAggComputeTime: 414.172ms[0m
      │       [1m[31mAggFuncComputeTime: 413.585ms[0m
      └──[0mPROJECT (id=1) [0m
"         │   [0mEstimates: [row: ?, cpu: ?, memory: ?, network: ?, cost: ?][0m"
         │   [0mTotalTime: 544.627us (0.11%) [CPUTime: 544.627us][0m
         │   [0mOutputRows: 3.623M (3622868)[0m
         │   [0mExpression: [2: _id][0m
         └──[0mOLAP_SCAN (id=0) [0m
"                [0mEstimates: [row: 18357, cpu: 1321751.17, memory: 0.00, network: 0.00, cost: 660875.59][0m"
"                [0mTotalTime: 53.186ms (11.23%) [CPUTime: 2.422ms, ScanTime: 50.763ms][0m"
                [0mOutputRows: 3.623M (3622868)[0m
                [0mTable: : agent_contact_base[0m
                [0mSubordinateOperators: [0m
                    [0mCHUNK_ACCUMULATE[0m
                    [0mNOOP[0m
                    [0mOLAP_SCAN_PREPARE[0m
[0m
For
explain analyze select count(_id), count(DISTINCT _id), count(DISTINCT agency_id, _id) from agent_contact_base c where agency_id = 'a4c5be3c-8097-4bb4-8b84-1a93f1e629be';
k
a
Got it. Attached both here.
👍 1
k