# Multiple sorting fields performance issues

**URL:** <https://discuss.elastic.co/t/multiple-sorting-fields-performance-issues/132349>\
**Category:** Elasticsearch\
**Created:** [May 17, 2018, 2:10pm UTC](https://discuss.elastic.co/t/multiple-sorting-fields-performance-issues/132349 "2018-05-17T14:10:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![bogdanjsx](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bogdanjsx/32/108181_2.png) [@bogdanjsx](https://discuss.elastic.co/u/bogdanjsx)\
**Post date:** [May 17, 2018, 2:10pm UTC](https://discuss.elastic.co/t/multiple-sorting-fields-performance-issues/132349/1 "2018-05-17T14:10:06Z")

</div>

Hello,

I'm trying to implement pagination for a service that pulls data out of Elasticsearch by using the search after functionality. Data needs to be sorted by only one field, but, as the docs mention, a field with unique values must be used as a tiebreaker.  
I first tried using `_uid`, as suggested in the docs, but it's a pretty big performance drawback, as it does not have `doc_values` enabled on it.  
I then switched to using another keyword-type field, which made it better, but it still takes a lot more time and memory than a single sorting field.

Does anyone have a suggestion on the second field I should use to minimize the second sort overhead? Or any other way that can make the search\_after paging work.  
Also, is there any way I can measure how much time the second sort takes? Profile API only shows query stats, but no sorting data, as far as I can see.

I'm using Elasticsearch version 5.5, on a cluster with 3 data nodes, 3 coordinating nodes and 3 master nodes. The data is split with an index per time frame approach (index configuration: 9 shards, 2 replicas), each one holding 2 weeks worth of data, with about 30 million docs each.

Thanks!

---

<div class="post-metadata">

**Author:** ![ddorian43](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ddorian43/32/36093_2.png) [@ddorian43](https://discuss.elastic.co/u/ddorian43)\
**Post date:** [May 19, 2018, 8:17pm UTC](https://discuss.elastic.co/t/multiple-sorting-fields-performance-issues/132349/2 "2018-05-19T20:17:36Z")

</div>

Can you upgrade to 6 and try index-sorting: [https://www.elastic.co/guide/en/elasticsearch/reference/6.3/index-modules-index-sorting.html](https://www.elastic.co/guide/en/elasticsearch/reference/6.3/index-modules-index-sorting.html) ?

Can you try joining both values into 1 field (don't know if it will help to be honest)?

---

<div class="post-metadata">

**Author:** ![bogdanjsx](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bogdanjsx/32/108181_2.png) [@bogdanjsx](https://discuss.elastic.co/u/bogdanjsx)\
**Post date:** [May 21, 2018, 7:49am UTC](https://discuss.elastic.co/t/multiple-sorting-fields-performance-issues/132349/3 "2018-05-21T07:49:14Z")

</div>

Hi and thanks for the answer.

Unfortunately, none of these solutions can be done, since the first sorting field is not always the same, but rather sent on the request. Because of that, any field can be a potential sorting field.

---

<div class="post-metadata">

**Author:** ![ddorian43](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ddorian43/32/36093_2.png) [@ddorian43](https://discuss.elastic.co/u/ddorian43)\
**Post date:** [May 21, 2018, 8:18am UTC](https://discuss.elastic.co/t/multiple-sorting-fields-performance-issues/132349/4 "2018-05-21T08:18:48Z")

</div>

How are you filtering the documents ? Maybe you can index-sort by filtered-fields so you don't query some segments at all ?

---

<div class="post-metadata">

**Author:** ![ddorian43](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ddorian43/32/36093_2.png) [@ddorian43](https://discuss.elastic.co/u/ddorian43)\
**Post date:** [May 21, 2018, 8:19am UTC](https://discuss.elastic.co/t/multiple-sorting-fields-performance-issues/132349/5 "2018-05-21T08:19:38Z")

</div>

How are sort fields distributed ? If 80% of sorts are for field `a` you can index-sort by that and have the other slow ones still being slow.

---

<div class="post-metadata">

**Author:** ![bogdanjsx](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bogdanjsx/32/108181_2.png) [@bogdanjsx](https://discuss.elastic.co/u/bogdanjsx)\
**Post date:** [May 21, 2018, 9:57am UTC](https://discuss.elastic.co/t/multiple-sorting-fields-performance-issues/132349/6 "2018-05-21T09:57:18Z")

</div>

Sort field distribution is random, it's user chosen.  
Also, an upgrade to 6 is not really possible at the moment.

---

<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 18, 2018, 9:57am UTC](https://discuss.elastic.co/t/multiple-sorting-fields-performance-issues/132349/7 "2018-06-18T09:57:24Z")

</div>

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