Count distinct becomes expensive because the engine must inspect a large share of the underlying events before it can safely deduplicate results. In growing pipelines, that means more disk scanning, more queueing, and slower end-to-end refreshes. When the same result can be derived from a user-centric first-event table, the workload shifts from events to users and becomes much cheaper.
Why count distinct slows down as volume grows
Distinct counting is expensive because the engine has to determine whether each incoming key has been seen before, which usually means extra comparison work, larger intermediate state, and more memory pressure as cardinality rises. In analytics pipelines, that cost is amplified by repeated scans, shuffles, and late-arriving data that force the system to keep deduplication state open longer.
At small scale, that overhead is often tolerable. At large scale, the problem is not just CPU, it is the whole execution path: more rows have to move through the pipeline, more partitions have to be reconciled, and the query often cannot be answered from a small pre-aggregated summary without risking incorrect double counting.
When the workload is event-centric, the engine may need to inspect a huge share of the raw fact table just to answer a question about unique users, devices, sessions, or accounts. That is why count distinct often becomes one of the first queries to expose weak partitioning, poor clustering, or a storage layout that is optimised for append speed rather than analytic reuse.
Why user-centric tables change the cost profile
A user-centric first-event table reduces the amount of work because the uniqueness decision is made once, upstream, and then reused. Instead of deduplicating many events every time a dashboard refreshes, the pipeline stores the user-level grain directly, so downstream queries read fewer rows and perform less runtime grouping.
This shifts the bottleneck from query-time deduplication to ingestion-time modelling. That is usually a good trade if the business question repeatedly asks for unique users, first actions, sign-ups, activations, or one-time conversions. The main design question is whether the chosen grain matches the stable reporting need, because a mismatch can create expensive rebuilds or force a second layer of correction logic later.
A well-designed summary table also improves predictability. The planner can estimate scan size more accurately, the warehouse has less intermediate state to manage, and refresh windows become more consistent because the query no longer depends on the full churn of raw event history for every run. For a practical pattern, see the CI/CD pipeline exploitation case study, which shows how operational complexity grows when sensitive workload state is handled repeatedly instead of being normalised early.
Where the pipeline depends on durable summaries, it is also worth reading NHIMG’s Ultimate Guide to NHIs for the broader governance pattern behind reusable automation state, and the NHI and Secrets Risk Report for how repeated operational dependence can create hidden scale costs.
Risk and Threat Considerations
Count distinct becomes a bottleneck when the system is forced to preserve correctness across too much raw history, too many partitions, or too much late data. The risk is not only slower dashboards, but also unstable refresh times, higher warehouse spend, and analyst workarounds that reintroduce duplicate records or inconsistent metric definitions.
Failure mechanism: The engine must maintain deduplication state for a large candidate set, and that state grows with cardinality and reprocessing. If the storage layout, clustering, or upstream modelling is poor, the query degrades into repeated broad scans and expensive reconciliation work.
Impact: Refresh latency rises, concurrency drops, and teams either accept stale metrics or add brittle pre-aggregation logic. At scale, the cost can spread beyond one query and become a recurring pipeline constraint that shapes schema design, job scheduling, and dashboard trust.
Standards & Framework Alignment
This section maps relevant standards and security frameworks to the operational risks and controls described in this guidance.
CIS Controls v8 and NIST CSF 2.0 set the governance and control requirements practitioners need to meet.
| Framework | Control / Reference | Relevance |
|---|---|---|
| CIS Controls v8 | CIS Control 7 — Continuous Vulnerability Management | Efficient analytics pipelines depend on predictable data volume and repeatable processing. |
| Recommendation — Reduce recurring scan burden by pruning unnecessary data and reusing precomputed summaries. | ||
| NIST CSF 2.0 | PR.DS — Data Security | The answer concerns data handling patterns that affect integrity, availability, and efficient use of analytic data. |
| Recommendation — Use data-centric controls to standardise grains and limit expensive reprocessing. | ||
Practitioner Guidance
What to prioritise: Treat repeated count distinct use as a modelling signal, not just a query problem. If the same unique-count question appears in many dashboards, move the deduplication boundary upstream so the expensive uniqueness decision is made once.
What to verify: Confirm that the summary grain exactly matches the business question. If analysts still need multiple uniqueness dimensions, keep the model flexible enough to avoid rebuilding the table every time the definition changes.
What good looks like: The pipeline should read a stable user-level table for common uniqueness metrics, with predictable scan size, bounded refresh windows, and no dependence on full raw-event rescans for every report.
Practitioner takeaway: The real optimisation is not a faster count distinct, it is designing the data model so the engine does not need to rediscover uniqueness on every query.
Related resources from NHI Mgmt Group
- Why do logs become unreliable when syslog pipelines scale poorly?
- Why do manual identity processes become a bottleneck as organisations scale across SaaS and remote work?
- Why does AppSec visibility become harder as cloud applications and delivery pipelines scale?
- How should security teams design SIEM architecture for cloud-scale telemetry without turning analytics into a data bottleneck?