approx_count_distinct
Description
Returns an approximate count of unique, non-null values for a given expression.
- A document is counted if it contains a unique, non-null value.
- The result is approximate, optimized for performance rather than precision.
Note
approx_count_distinct is an aggregation function and must be used with a grouping keyword such as groupby.
Syntax
Arguments
| Name | Type | Required | Description |
|---|---|---|---|
| expression | any | true | Expression whose unique, non-null values are counted approximately |
Example
Use case: Estimate the number of active users per application
Suppose you want to know how many unique users have been active in a given timeframe. Since you only care whether the number is above a threshold, an approximate count is sufficient.
Example data
{ "applicationname": "checkout-service", "username": "alice" },
{ "applicationname": "checkout-service", "username": "bob" },
{ "applicationname": "checkout-service", "username": "charlie" },
{ "applicationname": "checkout-service", "username": "alice" }
Example query
Example output
| applicationname | active_users |
|---|---|
| checkout-service | 3 |
Theme
Light