# Boolean must\_not Query is having slow performance

**URL:** https://discuss.elastic.co/t/boolean-must-not-query-is-having-slow-performance/64781
**Category:** Elasticsearch
**Created:** [November 2, 2016, 10:57pm UTC](https://discuss.elastic.co/t/boolean-must-not-query-is-having-slow-performance/64781 "2016-11-02T22:57:22Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![lubp123](https://avatars.discourse-cdn.com/v4/letter/l/58956e/32.png) [@lubp123](https://discuss.elastic.co/u/lubp123)
#### Post date: [November 2, 2016, 10:57pm UTC](https://discuss.elastic.co/t/boolean-must-not-query-is-having-slow-performance/64781/1 "2016-11-02T22:57:22Z")

</div>

**Elasticsearch version** :  
version : {  
"number" : "2.4.1",  
"build\_hash" : "c67dc32e24162035d18d6fe1e952c4cbcbe79d16",  
"build\_timestamp" : "2016-09-27T18:57:55Z",  
"build\_snapshot" : false,  
"lucene\_version" : "5.5.2"  
}

**Plugins installed** : []

**JVM version** : "1.8.0\_102"

**OS version** : OSX El Capitan 10.11.6

**Description of the problem including expected versus actual behavior** :

when we do a NOT based query on a large dataset of 300 million rows the time it takes to get data is very slow.  
time taken: 12 minutes  
number of rows: 300 million  
returning roughly 80% of the data.  
Index fields are all analyzed

```auto
curl -XGET 'http://localhost:9200/cars/item/_search?size=200' -d '
{ "profile": true, "query": { "bool": { "must_not": [{ "match": { "color": "red" } }, { "match": { "description": "car" } }] } }, "aggs": { "description": { "terms": { "field": "description", "size": 100 } } }} ' 

```

**Steps to reproduce** :

1. Do a simple curl command of a query in ES
2. Observe the time it takes from the profile information

---

<div class="post-metadata">

### Author: ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)
#### Post date: [November 3, 2016, 10:12am UTC](https://discuss.elastic.co/t/boolean-must-not-query-is-having-slow-performance/64781/2 "2016-11-03T10:12:44Z")

</div>

Phohibited clauses (MUST\_NOT) are indeed more costly than required clauses (MUST, FILTER) since the inverted index can barely help.

---

<div class="post-metadata">

### Author: ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)
#### Post date: [November 3, 2016, 10:13am UTC](https://discuss.elastic.co/t/boolean-must-not-query-is-having-slow-performance/64781/3 "2016-11-03T10:13:47Z")

</div>

In your case, I suspect the time it takes to compute the response is mainly due to the amount of data: how long does it take to run a simple match\_all query?

---

<div class="post-metadata">

### Author: ![lubp123](https://avatars.discourse-cdn.com/v4/letter/l/58956e/32.png) [@lubp123](https://discuss.elastic.co/u/lubp123)
#### Post date: [November 3, 2016, 8:51pm UTC](https://discuss.elastic.co/t/boolean-must-not-query-is-having-slow-performance/64781/4 "2016-11-03T20:51:35Z")

</div>

hello Adrain

Thank you for your insight.  
I did a Match All query on the data and it returns in 2.5 minutes. But this is still significantly less than the must\_not

---

<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: [July 5, 2017, 10:06pm UTC](https://discuss.elastic.co/t/boolean-must-not-query-is-having-slow-performance/64781/5 "2017-07-05T22:06:47Z")

</div>


