# Terms query takes lots of cpu usage

**URL:** <https://discuss.elastic.co/t/terms-query-takes-lots-of-cpu-usage/86765>\
**Category:** Elasticsearch\
**Created:** [May 23, 2017, 6:40am UTC](https://discuss.elastic.co/t/terms-query-takes-lots-of-cpu-usage/86765 "2017-05-23T06:40:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tamizh](https://avatars.discourse-cdn.com/v4/letter/t/8c91f0/32.png) [@Tamizh](https://discuss.elastic.co/u/Tamizh)\
**Post date:** [May 23, 2017, 6:40am UTC](https://discuss.elastic.co/t/terms-query-takes-lots-of-cpu-usage/86765/1 "2017-05-23T06:40:18Z")

</div>

I am using terms query to find the followers of a user.  
For that, I fetch the follower ids of that user and use terms query to get the user result from elasticsearch. But when I run the query it took lots of CPU. I guess it because of more terms per query.  
But I couldn't find any other solution for this. Any suggestions will be helpful.

---

<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:** [May 23, 2017, 12:29pm UTC](https://discuss.elastic.co/t/terms-query-takes-lots-of-cpu-usage/86765/2 "2017-05-23T12:29:58Z")

</div>

If you denormalize the data such that each user has a "follows" array you can just do this:

```
 GET /users/_search
 {
     "query": {
          "match" : { 
              "follows" : "userX"
         }
    }
 }
```

---

<div class="post-metadata">

**Author:** ![Tamizh](https://avatars.discourse-cdn.com/v4/letter/t/8c91f0/32.png) [@Tamizh](https://discuss.elastic.co/u/Tamizh)\
**Post date:** [May 23, 2017, 2:58pm UTC](https://discuss.elastic.co/t/terms-query-takes-lots-of-cpu-usage/86765/3 "2017-05-23T14:58:16Z")

</div>

Really thanks for the response. I thought the same way too. But I have 70 million users. So it will take too much time to add follows array. Is there any other option to overcome this issue ?.

---

<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:** [May 23, 2017, 4:03pm UTC](https://discuss.elastic.co/t/terms-query-takes-lots-of-cpu-usage/86765/4 "2017-05-23T16:03:59Z")

</div>

It comes down to physics. Random disk seeks for lots of unique IDs are slow. SSDs will help but there's still a cost with big numbers.  
Using a graph database will remove the index lookups at query time by chasing around connections using pointers but:

1. This means using a single-server solution with lots of RAM
2. All your index-lookups are shifted to write-time when the database has to convert user IDs to pointers.

No easy answers here.

---

<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:** [June 20, 2017, 4:04pm UTC](https://discuss.elastic.co/t/terms-query-takes-lots-of-cpu-usage/86765/5 "2017-06-20T16:04:40Z")

</div>

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