Skip to main content
When you calculate cardinality in a dataset, the time it takes to process the query is proportional to how large the dataset is. Finding the cardinality of a dataset that contains 20 million entries can take a significant amount of time and compute resources. Approximate count distinct does not calculate the exact cardinality of a dataset, but rather estimates the number of unique values. This reduces memory consumption and improves compute time by avoiding spilling intermediate results to secondary storage. uses the HyperLogLog algorithm, which provides estimates typically within a 2% margin of error. The benefit of HyperLogLog on time-series data is that it can continue to calculate the approximate cardinality of a dataset as it changes over time. It does this by adding an entry to the HyperLogLog hash as new data is retrieved, rather than recalculating the result for the entire dataset every time it is needed. This makes it an ideal candidate for using with continuous aggregates.

Prerequisites

To follow the steps on this page:
  • Create a target with Real-time analytics enabled.

    You need your connection details. This procedure also works for .

Calculate approximate distinct counts

This example tracks unique users visiting different API endpoints over time.
  1. Create the api_requests hypertable
  2. Insert sample data Generate API requests from different users:
  3. Count distinct users per endpoint using approx_count_distinct The approx_count_distinct() function uses default settings that work well for most use cases:
    This creates a hyperloglog aggregate for each endpoint. To get the actual distinct count, use the distinct_count() accessor:
  4. Count distinct users per hour using hyperloglog with custom bucket size For more control over accuracy, use hyperloglog() directly:

Use with continuous aggregates

Create a continuous aggregate to efficiently track unique users over time.
  1. Create a continuous aggregate with HyperLogLog
  2. Query daily unique users by rolling up hourly aggregates
  3. Calculate total unique users across all endpoints

Understand accuracy and memory trade-offs

The number of buckets in a HyperLogLog affects both accuracy and memory usage. More buckets provide better accuracy but require more memory.

Approximate relative errors by bucket size

Recommendations:
  • For most use cases, 8,192 buckets (1.15% error) provides a good balance
  • Use fewer buckets (1,024-4,096) when memory is constrained
  • Use more buckets (16,384+) when high accuracy is critical
  • Avoid using less than 1,024 buckets when cardinality is high