# How does order-by-field work (performance)?

**URL:** <https://discuss.elastic.co/t/how-does-order-by-field-work-performance/61961>\
**Category:** Elasticsearch\
**Created:** [September 30, 2016, 8:54pm UTC](https://discuss.elastic.co/t/how-does-order-by-field-work-performance/61961 "2016-09-30T20:54:32Z")\
**Posts on this page:** 3\
**Page:** 1

<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:** [September 30, 2016, 8:54pm UTC](https://discuss.elastic.co/t/how-does-order-by-field-work-performance/61961/1 "2016-09-30T20:54:32Z")

</div>

Say I have 2 indexes with 100K documents and 1M documents.  
Then I make a filter which returns 1K documents from each index and sorts by a biginteger field. Which one will complete faster ?  
Meaning, is the sort speed based on the number of filtered documents or total doucments (assuming everything in ram) ?

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [October 1, 2016, 11:35am UTC](https://discuss.elastic.co/t/how-does-order-by-field-work-performance/61961/2 "2016-10-01T11:35:03Z")

</div>

> [@ddorian43](#):
>
> biginteger field

If you mean an infinite precision field then that isn't a thing we have support for. Long is the best we've got.

> [@ddorian43](#):
>
> Meaning, is the sort speed based on the number of filtered documents or total doucments (assuming everything in ram) ?

The dominant factor is going to be the number of hits that who's value for that field is in a disk block that the OS hasn't paged in. If all blocks are paged in then the dominant factor is the number of hits.

Sorting (by a field or by score) works by making a min-heap the size of the number of hits and dumping all the hits into it, discarding hits that "fall out" or "don't fit". If you sort by `_score` then the query has to figure out some score and that is the sort key. If you are sorting by a field the queries skip figuring out a score and instead the sort key is the document's value for that field (or those fields if sorting by more than one field).

Getting the value for a field should be fairly fast [this](https://www.elastic.co/elasticon/2015/sf/elasticsearch-architecture-amusing-algorithms-and-details-on-data-structures) talk went into some detail about it but I can't figure the video at this point, sadly. It is really one of my favorite talks.

---

<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:** [July 5, 2017, 10:15pm UTC](https://discuss.elastic.co/t/how-does-order-by-field-work-performance/61961/3 "2017-07-05T22:15:41Z")

</div>

Closed on July 5, 2017, 10:15pm UTC


