# Slow Terms Aggregation

**URL:** <https://discuss.elastic.co/t/slow-terms-aggregation/136182>\
**Category:** Elasticsearch\
**Created:** [June 16, 2018, 5:34pm UTC](https://discuss.elastic.co/t/slow-terms-aggregation/136182 "2018-06-16T17:34:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![DumpCat](https://avatars.discourse-cdn.com/v4/letter/d/7bcc69/32.png) [@DumpCat](https://discuss.elastic.co/u/DumpCat)\
**Post date:** [June 16, 2018, 5:34pm UTC](https://discuss.elastic.co/t/slow-terms-aggregation/136182/1 "2018-06-16T17:34:09Z")

</div>

Hi, I hava a problem with es query and aggregation. All es record count is about 0.1 billion, before adding a terms aggregation, my query is fast, only cost 75ms and all hit count is 105. But after adding a aggregation, like this one:  
{  
"query": {  
...  
},  
"aggs": {  
"index": {  
"terms": {  
"field": "index"  
}  
}  
}  
}  
this query will cost 20 second!  
My question is: my query result count is only 105, why the aggregation is so slow?  
Thanks for any reply!

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [June 22, 2018, 9:51am UTC](https://discuss.elastic.co/t/slow-terms-aggregation/136182/2 "2018-06-22T09:51:22Z")

</div>

> [@DumpCat](#):
>
> My question is: my query result count is only 105, why the aggregation is so slow?

When we gather terms in results we can either gather the full-strings in memory or what we call a "global ordinal" which is much more compact. The ordinal is a number which is given to every unique string and it is this process of determining the strings-\>numbers lookup that can be expensive on an index that is still constantly being written to. So given the low number of search results maybe you'd be better served by using raw strings rather than the ordinals which is the `map` [execution hint](https://www.elastic.co/guide/en/elasticsearch/reference/6.3/search-aggregations-bucket-terms-aggregation.html#search-aggregations-bucket-terms-aggregation-execution-hint).

What is the number of unique terms for the field in question, out of interest?

---

<div class="post-metadata">

**Author:** ![DumpCat](https://avatars.discourse-cdn.com/v4/letter/d/7bcc69/32.png) [@DumpCat](https://discuss.elastic.co/u/DumpCat)\
**Post date:** [June 26, 2018, 1:58am UTC](https://discuss.elastic.co/t/slow-terms-aggregation/136182/3 "2018-06-26T01:58:21Z")

</div>

When I use `map execution hint`, query is faster than before, thanks.

---

<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 24, 2018, 1:58am UTC](https://discuss.elastic.co/t/slow-terms-aggregation/136182/4 "2018-07-24T01:58:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Alex\_Marquardt](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alex_marquardt/32/42925_2.png) [@Alex\_Marquardt](https://discuss.elastic.co/u/Alex_Marquardt)\
**Post date:** [May 10, 2019, 7:01pm UTC](https://discuss.elastic.co/t/slow-terms-aggregation/136182/5 "2019-05-10T19:01:55Z")

</div>

As @Mark_Harwood mentioned, this sounds like it is due to computation of global ordinals. A detailed description of this issue and several approaches to improve such performance (including the execution\_hint:map) are given in the following blog post.

> **[Improving the performance of high-cardinality terms aggregations in...](https://www.elastic.co/blog/improving-the-performance-of-high-cardinality-terms-aggregations-in-elasticsearch)**
>
> Learn how to improve the performance of slow terms aggregations on high-cardinality fields. Don't let fields with a high level of uniqueness slow down your Elasticsearch queries.
