# Performance of Term Query vs Terms Query with 1 value

**URL:** https://discuss.elastic.co/t/performance-of-term-query-vs-terms-query-with-1-value/150818
**Category:** Elasticsearch
**Created:** [October 3, 2018, 5:51am UTC](https://discuss.elastic.co/t/performance-of-term-query-vs-terms-query-with-1-value/150818 "2018-10-03T05:51:13Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Mohit\_Kathuria](https://avatars.discourse-cdn.com/v4/letter/m/e8c25b/32.png) [@Mohit\_Kathuria](https://discuss.elastic.co/u/Mohit_Kathuria)
#### Post date: [October 3, 2018, 5:51am UTC](https://discuss.elastic.co/t/performance-of-term-query-vs-terms-query-with-1-value/150818/1 "2018-10-03T05:51:13Z")

</div>

We have an Elasticsearch cluster setup of 5 data nodes, 3 master with version 5.2.2. The cluster has 10 indices, namely txn\_0, txn\_1 ... txn\_9 where Ith index has all transactions of a user with modulus of 10 as I. For example: All data of user Id 101 will be in index txn\_1. Also, we are using shard routing to further optimise the routing of data to a particular shard.

Our query pattern includes search on data for a particular user. Recently, we were trying to optimise our query latency and we figured out that performance of terms query with single value was worse than the performance of term query. Be default, all our queries were terms query and when we changed to term, we saw massive improvement in query latency. We were not still quite sure of this behaviour after research, hence I am writing to the community to decipher this behaviour.

In this respect, I understand that term queries are not cached by default and terms query are (depending on frequency of queries & segment size).

Below is our query:  
Old Query - [https://gist.github.com/mkathuria/f54c8228876ee4891850ee1e529fdaee](https://gist.github.com/mkathuria/f54c8228876ee4891850ee1e529fdaee)  
New Query - [https://gist.github.com/mkathuria/749df8d6e3b2560bd26b4f953a82231a](https://gist.github.com/mkathuria/749df8d6e3b2560bd26b4f953a82231a)

The only change in query was to change a particular terms query to term. This particular field txnType is low cardinality field with cardinality of around 100, but each value of txnType will have few millions to 1 billion hits each.

---

<div class="post-metadata">

### Author: ![Mohit\_Kathuria](https://avatars.discourse-cdn.com/v4/letter/m/e8c25b/32.png) [@Mohit\_Kathuria](https://discuss.elastic.co/u/Mohit_Kathuria)
#### Post date: [October 11, 2018, 11:23am UTC](https://discuss.elastic.co/t/performance-of-term-query-vs-terms-query-with-1-value/150818/2 "2018-10-11T11:23:43Z")

</div>

Anyone else also seeing similar behaviour?

---

<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: [November 8, 2018, 11:23am UTC](https://discuss.elastic.co/t/performance-of-term-query-vs-terms-query-with-1-value/150818/3 "2018-11-08T11:23:46Z")

</div>

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