# What's the best performance, to execute two different queries or a single?

**URL:** <https://discuss.elastic.co/t/whats-the-best-performance-to-execute-two-different-queries-or-a-single/125382>\
**Category:** Elasticsearch\
**Created:** [March 23, 2018, 2:47pm UTC](https://discuss.elastic.co/t/whats-the-best-performance-to-execute-two-different-queries-or-a-single/125382 "2018-03-23T14:47:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tberne](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tberne/32/648_2.png) [@tberne](https://discuss.elastic.co/u/tberne)\
**Post date:** [March 23, 2018, 2:47pm UTC](https://discuss.elastic.co/t/whats-the-best-performance-to-execute-two-different-queries-or-a-single/125382/1 "2018-03-23T14:47:33Z")

</div>

Hey, guys.  
I'm planning a new feature and I would like to know what performs better.  
Explaining a bit the scenario:

I have two different filters to be executed and generate different aggregations.

I can either:

- Have a single search with no query filters and two filter aggregations that will split the data and perform inner aggregations.
- Have two different searches, each with its query filter and aggregation to generate results.

Example:  
Suppose that I have the following structure:

```
PUT /x
PUT /x/x/_mapping
{
  "properties": {
    "x":{
      "type": "integer"
    }
  }
}

PUT /x/x/1
{
  "x": 1
}

PUT /x/x/2
{
  "x": 2
}

PUT /x/x/3
{
  "x": 3
}

```

I could do:

```
GET /x/_search
{
  "size": 0,
  "aggs": {
    "f1": {
      "filter": {
        "range": {
          "x": {
            "gt": 2
          }
        }
      },
      "aggs": {
        "max": {
          "max": {
            "field": "x"
          }
        }
      }
    },
    "f2": {
      "filter": {
        "range": {
          "x": {
            "lt": 2
          }
        }
      },
      "aggs": {
        "max": {
          "max": {
            "field": "x"
          }
        }
      }
    }
  }
}

```

Or two different searches:

```
GET /x/_search
{
  "query": {
    "range": {
      "x": {
        "gt": 2
      }
    }
  }, 
  "size": 0,
  "aggs": {
    "max": {
      "max": {
        "field": "x"
      }
    }
  }
}

GET /x/_search
{
  "query": {
    "range": {
      "x": {
        "lt": 2
      }
    }
  }, 
  "size": 0,
  "aggs": {
    "max": {
      "max": {
        "field": "x"
      }
    }
  }
}

```

Considering a large data universe and complex filters, which one would perform better?

---

<div class="post-metadata">

**Author:** ![ddorian43](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ddorian43/32/36093_2.png) [@ddorian43](https://discuss.elastic.co/u/ddorian43)\
**Post date:** [March 23, 2018, 6:36pm UTC](https://discuss.elastic.co/t/whats-the-best-performance-to-execute-two-different-queries-or-a-single/125382/2 "2018-03-23T18:36:25Z")

</div>

A faster query would be, using the 1st one, but changing "gt:2" and "lt:2" to `must\_not: {term:2}}" in the body.

Makes sense ? So "x!=2" is a faster filter compared to 2 range filters.

Try that.

---

<div class="post-metadata">

**Author:** ![tberne](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tberne/32/648_2.png) [@tberne](https://discuss.elastic.co/u/tberne)\
**Post date:** [March 23, 2018, 7:14pm UTC](https://discuss.elastic.co/t/whats-the-best-performance-to-execute-two-different-queries-or-a-single/125382/3 "2018-03-23T19:14:41Z")

</div>

The "\< 2" and "\> 2" filters were only an example. My actual filters will be way more complex.  
Can I ask how did you infer the first query would be more efficient (remember that I'm talking about large databases with complex filters)?

---

<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 20, 2018, 7:14pm UTC](https://discuss.elastic.co/t/whats-the-best-performance-to-execute-two-different-queries-or-a-single/125382/4 "2018-04-20T19:14:48Z")

</div>

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