# More and more Nested query is progressively getting slower and slower

**URL:** <https://discuss.elastic.co/t/more-and-more-nested-query-is-progressively-getting-slower-and-slower/222855>\
**Category:** Elasticsearch\
**Created:** [March 10, 2020, 4:59am UTC](https://discuss.elastic.co/t/more-and-more-nested-query-is-progressively-getting-slower-and-slower/222855 "2020-03-10T04:59:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![NewHere](https://avatars.discourse-cdn.com/v4/letter/n/5e9695/32.png) [@NewHere](https://discuss.elastic.co/u/NewHere)\
**Post date:** [March 10, 2020, 4:59am UTC](https://discuss.elastic.co/t/more-and-more-nested-query-is-progressively-getting-slower-and-slower/222855/1 "2020-03-10T04:59:57Z")

</div>

We have a few millions of documents, each can have zero to hundred of nested objects (thankfully its just one level of nesting, the rest are just text fields).

When issuing a boolean should match query with one nested query for the nested objects, it took 3sec to return 30 results paginated from more than 10k results.

However, as we add more and more nested query, e.g.

```
"bool": {
  "should": [{
    "dis_max": {
      "queries": [{
        "nested": {...},
        "nested": {...},
        "nested": {...},

```

the time needed to return results is getting slower and slower to a point it takes over a minute to return for a query with 25 nested queries.

Here are some stats:  
No nested query 1sec  
1 nested query 3sec  
5 nested queries 8sec  
11 nested queries 15sec  
19 nested queries 40sec  
25 nested queries over a minute

Is more than 10 nested queries already considered too much for Elasticsearch?

What other things can we try to reduce the query time?

Is it too unrealistic to try to reduce the time for say 20 nested queries to return within 5sec?

Thank you

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [March 29, 2020, 2:46pm UTC](https://discuss.elastic.co/t/more-and-more-nested-query-is-progressively-getting-slower-and-slower/222855/2 "2020-03-29T14:46:30Z")

</div>

The more query clauses you ahve the more work for Elasticsearch, especially if it is wrapped in a should clause and there are no filters to reduce the number of documents that need to be evaluated. Having 25 match clauses sounds like a quite complex query, so it would be interesting to learn what this does and see if there are any alternatives. It would also be useful to know why you have gone with nested documents in the first place. Sometimes a flattened data model can perform better, but that is hard to judge without knowing the data and use-case.

> [@NewHere](#):
>
> Is more than 10 nested queries already considered too much for Elasticsearch?

Not necessarily - it depends on the use case and latency requirements.

> [@NewHere](#):
>
> Is it too unrealistic to try to reduce the time for say 20 nested queries to return within 5sec?

Given the data you have provided I would say that likely is unrealistic with the current approach.

---

<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:** [April 26, 2020, 2:46pm UTC](https://discuss.elastic.co/t/more-and-more-nested-query-is-progressively-getting-slower-and-slower/222855/3 "2020-04-26T14:46:39Z")

</div>

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