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.-
Create the
api_requestshypertable -
Insert sample data
Generate API requests from different users:
-
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 thedistinct_count()accessor: -
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.-
Create a continuous aggregate with HyperLogLog
-
Query daily unique users by rolling up hourly aggregates
-
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
- For more information about the HyperLogLog algorithm, see the HyperLogLog Wikipedia article.