# Why does matchAll query so slowly?

**URL:** <https://discuss.elastic.co/t/why-does-matchall-query-so-slowly/69764>\
**Category:** Elasticsearch\
**Created:** [December 22, 2016, 10:10am UTC](https://discuss.elastic.co/t/why-does-matchall-query-so-slowly/69764 "2016-12-22T10:10:50Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![hubo3085632](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hubo3085632/32/23724_2.png) [@hubo3085632](https://discuss.elastic.co/u/hubo3085632)\
**Post date:** [December 22, 2016, 10:10am UTC](https://discuss.elastic.co/t/why-does-matchall-query-so-slowly/69764/1 "2016-12-22T10:10:50Z")

</div>

hi all ,I want to use matchAll to achieve a query which same as the sql 'select top xxx from tab' in rdbms.But i find it does slowly,is there has another appropriate function to achieve my purpose?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [December 22, 2016, 10:38am UTC](https://discuss.elastic.co/t/why-does-matchall-query-so-slowly/69764/2 "2016-12-22T10:38:39Z")

</div>

This is the fastest query.  
Can you share your exact query please? And the full response as well?

---

<div class="post-metadata">

**Author:** ![Lumaraf](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lumaraf/32/50639_2.png) [@Lumaraf](https://discuss.elastic.co/u/Lumaraf)\
**Post date:** [December 22, 2016, 11:19am UTC](https://discuss.elastic.co/t/why-does-matchall-query-so-slowly/69764/3 "2016-12-22T11:19:31Z")

</div>

Your query is probably using a lot of time sorting results.

Have a look at the [Field stats API](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-field-stats.html). It should return the minimum and maximum values for you field much faster than using a matchAll query which would then have to sort all documents first.

---

<div class="post-metadata">

**Author:** ![hubo3085632](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hubo3085632/32/23724_2.png) [@hubo3085632](https://discuss.elastic.co/u/hubo3085632)\
**Post date:** [December 22, 2016, 1:06pm UTC](https://discuss.elastic.co/t/why-does-matchall-query-so-slowly/69764/4 "2016-12-22T13:06:59Z")

</div>

here is my code

QueryBuilder qb =QueryBuilders.matchAllQuery();  
SearchResponse response = Es\_Utils.getClient().prepareSearch(indexName)  
.setTypes(mappingName)  
.setQuery(qb )  
.setSize(100)  
.setFrom(0)  
.execute()  
.actionGet()  
;  
SearchHits hits=response.getHits();

---

<div class="post-metadata">

**Author:** ![hubo3085632](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hubo3085632/32/23724_2.png) [@hubo3085632](https://discuss.elastic.co/u/hubo3085632)\
**Post date:** [December 22, 2016, 1:10pm UTC](https://discuss.elastic.co/t/why-does-matchall-query-so-slowly/69764/5 "2016-12-22T13:10:37Z")

</div>

but i haven't use sorting

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [December 22, 2016, 1:21pm UTC](https://discuss.elastic.co/t/why-does-matchall-query-so-slowly/69764/6 "2016-12-22T13:21:38Z")

</div>

Can you print the response object please?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [December 22, 2016, 1:43pm UTC](https://discuss.elastic.co/t/why-does-matchall-query-so-slowly/69764/8 "2016-12-22T13:43:24Z")

</div>

It's not the response object

---

<div class="post-metadata">

**Author:** ![hubo3085632](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hubo3085632/32/23724_2.png) [@hubo3085632](https://discuss.elastic.co/u/hubo3085632)\
**Post date:** [December 22, 2016, 1:54pm UTC](https://discuss.elastic.co/t/why-does-matchall-query-so-slowly/69764/9 "2016-12-22T13:54:45Z")

</div>

How to print it

---

<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:** [December 22, 2016, 2:04pm UTC](https://discuss.elastic.co/t/why-does-matchall-query-so-slowly/69764/10 "2016-12-22T14:04:36Z")

</div>

> [@hubo3085632](#):
>
> hi all ,I want to use matchAll to achieve a query which same as the sql 'select top xxx from tab' in rdbms

I'm not clear what "xxx" is here. Is it documents? Is it values e.g. top "author names"?  
If the latter then you want to be using aggregations, not retrieving individual documents.  
Can you be more specific about the data you have and the question you want answered?

---

<div class="post-metadata">

**Author:** ![hubo3085632](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hubo3085632/32/23724_2.png) [@hubo3085632](https://discuss.elastic.co/u/hubo3085632)\
**Post date:** [December 22, 2016, 2:28pm UTC](https://discuss.elastic.co/t/why-does-matchall-query-so-slowly/69764/11 "2016-12-22T14:28:48Z")

</div>

This is my document,The xxx is integer value,I just replaced the true values.I use matchall to get data then put them on the homepage.

---

<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:** [December 22, 2016, 2:33pm UTC](https://discuss.elastic.co/t/why-does-matchall-query-so-slowly/69764/12 "2016-12-22T14:33:04Z")

</div>

> [@hubo3085632](#):
>
> The xxx is integer value,

And when you say you want the `top` values do you mean the largest-seen values or the most-frequently used?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [December 22, 2016, 3:04pm UTC](https://discuss.elastic.co/t/why-does-matchall-query-so-slowly/69764/13 "2016-12-22T15:04:59Z")

</div>

```auto
response.toString()

```

---

<div class="post-metadata">

**Author:** ![hubo3085632](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hubo3085632/32/23724_2.png) [@hubo3085632](https://discuss.elastic.co/u/hubo3085632)\
**Post date:** [December 23, 2016, 1:22am UTC](https://discuss.elastic.co/t/why-does-matchall-query-so-slowly/69764/14 "2016-12-23T01:22:27Z")

</div>

{  
"took" : 478,  
"timed\_out" : false,  
"\_shards" : {  
"total" : 12,  
"successful" : 12,  
"failed" : 0  
},  
"hits" : {  
"total" : 196680688,  
"max\_score" : 1.0,  
"hits" : [{  
....  
},  
{  
....

```
} ]

```

}  
}

here is the code print time usage  
"  
startTime=System.currentTimeMillis();  
QueryBuilder qb =QueryBuilders.matchAllQuery();  
SearchResponse response = Es\_Utils.getClient().prepareSearch(indexName)  
.setTypes(mappingName)  
.setQuery(qb )  
.setSize(100)  
.setFrom(0)  
.execute()  
.actionGet();

System.out.println(response.toString());  
endTime =System.currentTimeMillis();  
endTime -startTime=1924ms  
"

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [December 23, 2016, 2:16am UTC](https://discuss.elastic.co/t/why-does-matchall-query-so-slowly/69764/15 "2016-12-23T02:16:14Z")

</div>

Thanks.

2 things here.

It takes around 500ms to elasticsearch to collect all things on the coordinating node.  
Then it takes around 1500ms to send that data over the wire to your client.

What is elasticsearch version BTW?

Can you test the same with size =10 ?

---

<div class="post-metadata">

**Author:** ![hubo3085632](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hubo3085632/32/23724_2.png) [@hubo3085632](https://discuss.elastic.co/u/hubo3085632)\
**Post date:** [December 23, 2016, 3:26am UTC](https://discuss.elastic.co/t/why-does-matchall-query-so-slowly/69764/16 "2016-12-23T03:26:59Z")

</div>

es version is 2.3.5,i set the size =10 ,but the query use the same time usage.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [December 23, 2016, 7:26am UTC](https://discuss.elastic.co/t/why-does-matchall-query-so-slowly/69764/17 "2016-12-23T07:26:00Z")

</div>

Can you print again the full response and your endTime-startTime?

---

<div class="post-metadata">

**Author:** ![hubo3085632](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hubo3085632/32/23724_2.png) [@hubo3085632](https://discuss.elastic.co/u/hubo3085632)\
**Post date:** [December 26, 2016, 4:55am UTC](https://discuss.elastic.co/t/why-does-matchall-query-so-slowly/69764/18 "2016-12-26T04:55:59Z")

</div>

{  
"took" : 472,  
"timed\_out" : false,  
"\_shards" : {  
"total" : 12,  
"successful" : 12,  
"failed" : 0  
},  
"hits" : {  
"total" : 196680688,  
"max\_score" : 1.0,  
"hits" : [  
......  
]  
}  
}

(endTime-startTime)=1887

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [January 9, 2017, 6:17am UTC](https://discuss.elastic.co/t/why-does-matchall-query-so-slowly/69764/19 "2017-01-09T06:17:04Z")

</div>

So you are consuming most of the time (75%) outside elasticsearch.

I know that 5.0 has enhanced the match all query so may you can lower down the 25% part but you will still have to fix the remaining 75%

---

<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:** [February 6, 2017, 6:17am UTC](https://discuss.elastic.co/t/why-does-matchall-query-so-slowly/69764/20 "2017-02-06T06:17:29Z")

</div>

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