# Terms performance vs many should clauses

**URL:** https://discuss.elastic.co/t/terms-performance-vs-many-should-clauses/108371
**Category:** Elasticsearch
**Created:** [November 20, 2017, 11:20am UTC](https://discuss.elastic.co/t/terms-performance-vs-many-should-clauses/108371 "2017-11-20T11:20:35Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Aharon\_Levine](https://avatars.discourse-cdn.com/v4/letter/a/b5ac83/32.png) [@Aharon\_Levine](https://discuss.elastic.co/u/Aharon_Levine)
#### Post date: [November 20, 2017, 11:20am UTC](https://discuss.elastic.co/t/terms-performance-vs-many-should-clauses/108371/1 "2017-11-20T11:20:35Z")

</div>

Hi

Can someone please help me understand the performance difference between these 2 queries:  
#1 Query with 'terms'

> {  
> "query": {  
> "bool": {  
> "filter": [  
> {  
> "term": {  
> "service\_id": "12345"  
> }  
> },  
> {  
> "term": {  
> "name": "hello123"  
> }  
> },  
> {  
> "terms": {  
> "date": [1501056000000,1500192000000,1501228800000,1500969600000,1499414400000,1499241600000,1500883200000,1501142400000,1502092800000,1500364800000,1503561600000]  
> }  
> }  
> ]  
> }  
> },  
> "size":50  
> }

#2 Query with 'should'

> {  
> "query": {  
> "bool": {  
> "filter": [  
> {  
> "term": {  
> "service\_id": "12345"  
> }  
> },  
> {  
> "term": {  
> "name": "hello123"  
> }  
> },{  
> "bool":{  
> "should":[  
> {  
> "term": {  
> "date": 1501315200000  
> }  
> },  
> {  
> "term": {  
> "date": 1499846400000  
> }  
> },  
> {  
> "term": {  
> "date": 1499328000000  
> }  
> },  
> {  
> "term": {  
> "date": 1500710400000  
> }  
> },  
> {  
> "term": {  
> "date": 1500796800000  
> }  
> },  
> {  
> "term": {  
> "date": 1498982400000  
> }  
> },  
> {  
> "term": {  
> "date": 1500883200000  
> }  
> },  
> {  
> "term": {  
> "date": 1500624000000  
> }  
> },  
> {  
> "term": {  
> "date": 1499155200000  
> }  
> }  
> ]  
> }  
> }  
> ]
> 
> ```
> }
> },
> 
> ```
> 
> "size":50  
> }

We're running on Elasticsearch 5.4 with a relatively large index,  
The 'terms' approach (#1) takes around 4 seconds while the 'should' approach (#2) returns in 40ms!  
Obviously I'm randomizing the requested date fields each time so that the results are not cached.  
I intuitively thought that #1 should be faster (its alot cleaner to write) ?

---

<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 21, 2017, 2:18pm UTC](https://discuss.elastic.co/t/terms-performance-vs-many-should-clauses/108371/2 "2017-11-21T14:18:48Z")

</div>

Can you share the `profile` (run the query with profile:true) in both cases?

---

<div class="post-metadata">

### Author: ![Aharon\_Levine](https://avatars.discourse-cdn.com/v4/letter/a/b5ac83/32.png) [@Aharon\_Levine](https://discuss.elastic.co/u/Aharon_Levine)
#### Post date: [November 21, 2017, 4:53pm UTC](https://discuss.elastic.co/t/terms-performance-vs-many-should-clauses/108371/3 "2017-11-21T16:53:10Z")

</div>

Well, this is embarrassing ...  
I'm rerunning the queries now and I can't replicate the behavior , they both perform very similar to each-other now 😳

Really not sure what happened, I'm sure I wasn't hallucinating before,  
Thanks for the concern

---

<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: [December 19, 2017, 4:53pm UTC](https://discuss.elastic.co/t/terms-performance-vs-many-should-clauses/108371/4 "2017-12-19T16:53:38Z")

</div>

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