# How to speed up date\_range aggregation with multiple ranges

**URL:** <https://discuss.elastic.co/t/how-to-speed-up-date-range-aggregation-with-multiple-ranges/159305>\
**Category:** Elasticsearch\
**Created:** [December 4, 2018, 7:39am UTC](https://discuss.elastic.co/t/how-to-speed-up-date-range-aggregation-with-multiple-ranges/159305 "2018-12-04T07:39:44Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ha\_Truong](https://avatars.discourse-cdn.com/v4/letter/h/9fc348/32.png) [@Ha\_Truong](https://discuss.elastic.co/u/Ha_Truong)\
**Post date:** [December 4, 2018, 7:39am UTC](https://discuss.elastic.co/t/how-to-speed-up-date-range-aggregation-with-multiple-ranges/159305/1 "2018-12-04T07:39:45Z")

</div>

We are using date\_range aggregation where we have 30 date ranges in query, and we also need to do aggregation in each range, please see detail of query as below.  
This is taking about 6sec against data set where we have only about 3M docs per range, about 20K uniq value of field2 (which is used in cardinality) and single value in all docs for field1. CPU and Memory usage on ES is very low (\<5%).

Is there a way we can bring this query down to less than 1 sec? (e.g not sure if it helps if we break the search to multiple search with smaller number of ranges and run msearch or es already executes different date ranges in the query in parallel? any tuning in query we can do?)

(Note: if we remove cardinality aggregation, the query took about 900ms).

```
  "size": 0,
  "aggregations": {
    "rolling_date_range": {
      "date_range": {
        "field": "modified_at",
        "ranges": [
          {
            "from": "1527811200000",
            "to": "1530403200000"
          },
          ... There are 30 date ranges here
          {
            "from": "1530316800000",
            "to": "1532908800000"
          }
        ],
        "keyed": false
      },
      "aggregations": {
        "field1": {
          "terms": {
            "field": "field1",
            "size": 30,
            "min_doc_count": 1,
            "shard_min_doc_count": 0,
            "show_term_doc_count_error": false,
            "order": [
              {
                "_count": "desc"
              },
              {
                "_key": "asc"
              }
            ]
          },
          "aggregations": {
            "count_distinct_field2": {
              "cardinality": {
                "field": "field2"
              }
            }
          }
        }
      }
    }
  }
}
```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [January 1, 2019, 7:39am UTC](https://discuss.elastic.co/t/how-to-speed-up-date-range-aggregation-with-multiple-ranges/159305/2 "2019-01-01T07:39:48Z")

</div>

Closed on January 1, 2019, 7:39am UTC

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
