# Slow response times query first couple of times

**URL:** https://discuss.elastic.co/t/slow-response-times-query-first-couple-of-times/285018
**Category:** Elasticsearch
**Created:** [September 23, 2021, 4:17pm UTC](https://discuss.elastic.co/t/slow-response-times-query-first-couple-of-times/285018 "2021-09-23T16:17:20Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Mark\_Schenk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_schenk/32/95000_2.png) [@Mark\_Schenk](https://discuss.elastic.co/u/Mark_Schenk)
#### Post date: [September 23, 2021, 4:17pm UTC](https://discuss.elastic.co/t/slow-response-times-query-first-couple-of-times/285018/1 "2021-09-23T16:17:20Z")

</div>

We have a service which is using ES sporadically, (once every few hours). We noted that the first couple of times we query ES takes a lot longer than subsequent calls. For example if we perform the following query:

```auto
{
    "query": {
        "bool": {
            "must": [
                
                    {"term": {
                        "organization.id": 1234
                    }},
                    {"match": {
                        "name": "BEST HITS"
                    }}
                
            ]
        }
    }
}

```

the first call can take up to 5 seconds. Subsequently calls decrease rapidly (2 seconds, 1100 ms, 100 ms). We have done some experimenting with the property `index.search.idle.after` and are now testing changing `index.refresh_interval` but I'm wondering whether we are on the right track. All help will be appreciated.

---

<div class="post-metadata">

### Author: ![mayya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mayya/32/83147_2.png) [@mayya](https://discuss.elastic.co/u/mayya)
#### Post date: [September 23, 2021, 8:32pm UTC](https://discuss.elastic.co/t/slow-response-times-query-first-couple-of-times/285018/2 "2021-09-23T20:32:13Z")

</div>

If you are querying ES rarely, but actively indexing, Elasticsearch moves shards to "idle" state for search which means that search indices are not actively built. And once after some time you hit a search request, for all these new indexed data new search indices will be built, which may take some time. This is the best optimization for indexing speed.

If you want your searches to be always faster even after some period of inactivity, you can set up an explicit "index.refresh\_interval", which ensures that new search indices are built after this interval.

Another factor that comes into play for repeated queries are caching. If you run the same query multiple times, its results may be cached, and subsequent calls for the same query can return much faster, as they are returned from cache. This is provided that your index doesn't change.

---

<div class="post-metadata">

### Author: ![Mark\_Schenk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_schenk/32/95000_2.png) [@Mark\_Schenk](https://discuss.elastic.co/u/Mark_Schenk)
#### Post date: [September 24, 2021, 4:37am UTC](https://discuss.elastic.co/t/slow-response-times-query-first-couple-of-times/285018/3 "2021-09-24T04:37:59Z")

</div>

Thanks for the quick response and your confirmation that we are on the right track. We are today doing some more tests and I will put the results of them 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: [October 22, 2021, 4:38am UTC](https://discuss.elastic.co/t/slow-response-times-query-first-couple-of-times/285018/4 "2021-10-22T04:38:50Z")

</div>

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