0
0
Elasticsearchquery~5 mins

Metric aggregations (avg, sum, min, max) in Elasticsearch - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Metric aggregations (avg, sum, min, max)
O(n)
Understanding Time Complexity

When using metric aggregations like average, sum, minimum, and maximum in Elasticsearch, it's important to know how the time to get results changes as data grows.

We want to understand how the work done grows when there are more documents to process.

Scenario Under Consideration

Analyze the time complexity of the following code snippet.


GET /sales/_search
{
  "size": 0,
  "aggs": {
    "average_price": { "avg": { "field": "price" } },
    "total_revenue": { "sum": { "field": "revenue" } },
    "lowest_price": { "min": { "field": "price" } },
    "highest_price": { "max": { "field": "price" } }
  }
}
    

This query calculates average, sum, minimum, and maximum values for fields across all matching documents.

Identify Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: Scanning each document once to read the field values.
  • How many times: Once per document in the search results.
How Execution Grows With Input

As the number of documents grows, the time to compute these metrics grows roughly in direct proportion.

Input Size (n)Approx. Operations
1010 field reads and updates
100100 field reads and updates
10001000 field reads and updates

Pattern observation: Doubling the number of documents roughly doubles the work done.

Final Time Complexity

Time Complexity: O(n)

This means the time to compute these metrics grows linearly with the number of documents processed.

Common Mistake

[X] Wrong: "Metric aggregations instantly return results regardless of data size."

[OK] Correct: Even though Elasticsearch is fast, it still needs to look at each document's field to calculate metrics, so more data means more work.

Interview Connect

Understanding how metric aggregations scale helps you explain performance considerations clearly, showing you grasp how data size affects query speed.

Self-Check

"What if we added a filter to reduce documents before aggregation? How would the time complexity change?"