# How to increase performance of query using filter?

**URL:** https://discuss.elastic.co/t/how-to-increase-performance-of-query-using-filter/54743
**Category:** Elasticsearch
**Created:** [July 5, 2016, 2:19pm UTC](https://discuss.elastic.co/t/how-to-increase-performance-of-query-using-filter/54743 "2016-07-05T14:19:49Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![rupal](https://avatars.discourse-cdn.com/v4/letter/r/5f9b8f/32.png) [@rupal](https://discuss.elastic.co/u/rupal)
#### Post date: [July 5, 2016, 2:19pm UTC](https://discuss.elastic.co/t/how-to-increase-performance-of-query-using-filter/54743/1 "2016-07-05T14:19:49Z")

</div>

{  
"size": 0,  
"filter": [  
{  
"term": {  
"type": "type\_data"  
}  
},  
{  
"exists": {  
"field": "ID"  
}  
}  
],  
"aggs": {  
"group\_by\_state": {  
"terms": {  
"field": "name",  
"size": 0,  
"order": {  
"\_term": "asc"  
}  
},  
"aggs": {  
"Average": {  
"avg": {  
"field": "AVG\_FIELD1"  
}  
},  
"99th Percentile": {  
"percentiles": {  
"field": "AVG\_FIELD2",  
"percents": [  
99  
]  
}  
}  
}  
}  
}  
}

I am using above query but it takes too much time to execute. There are about 1900000 events. How can i optimize above query? should I use filter ?

---

<div class="post-metadata">

### Author: ![danielmitterdorfer](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/danielmitterdorfer/32/110510_2.png) [@danielmitterdorfer](https://discuss.elastic.co/u/danielmitterdorfer)
#### Post date: [July 5, 2016, 2:26pm UTC](https://discuss.elastic.co/t/how-to-increase-performance-of-query-using-filter/54743/2 "2016-07-05T14:26:48Z")

</div>

Hi @rupal,

you can use the [profile API](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-profile.html) (available in ES 2.2+) to dig deeper. I **guess** your problem are the aggregations.

Daniel

---

<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:37pm UTC](https://discuss.elastic.co/t/how-to-increase-performance-of-query-using-filter/54743/3 "2017-07-05T22:37:56Z")

</div>


