# I wonder why docvalues are fast for sorting

**URL:** <https://discuss.elastic.co/t/i-wonder-why-docvalues-are-fast-for-sorting/297039>\
**Category:** Elasticsearch\
**Created:** [February 12, 2022, 6:44am UTC](https://discuss.elastic.co/t/i-wonder-why-docvalues-are-fast-for-sorting/297039 "2022-02-12T06:44:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![slowup](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/slowup/32/99097_2.png) [@slowup](https://discuss.elastic.co/u/slowup)\
**Post date:** [February 12, 2022, 6:44am UTC](https://discuss.elastic.co/t/i-wonder-why-docvalues-are-fast-for-sorting/297039/1 "2022-02-12T06:44:33Z")

</div>

docvalues ​​are sorted by docid, so they are not values, but why can they be fast for sorting? I want to know deeply about the internal structure, please help!

---

<div class="post-metadata">

**Author:** ![slowup](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/slowup/32/99097_2.png) [@slowup](https://discuss.elastic.co/u/slowup)\
**Post date:** [February 20, 2022, 9:40am UTC](https://discuss.elastic.co/t/i-wonder-why-docvalues-are-fast-for-sorting/297039/2 "2022-02-20T09:40:14Z")

</div>

> **[Query Planning for Range Queries in Elasticsearch](https://www.elastic.co/kr/blog/better-query-planning-for-range-queries-in-elasticsearch)**
>
> Taking our efforts to make range queries in Elasticsearch faster to a new level and exploring when they're used in conjunctions.

---

<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:** [February 20, 2022, 10:42am UTC](https://discuss.elastic.co/t/i-wonder-why-docvalues-are-fast-for-sorting/297039/3 "2022-02-20T10:42:36Z")

</div>

Indeed, doc values are sorted by docIds, so they are not super fast if you need to sort a lot of documents. But, you can:

1. provide a filter that will limit the number of documents for which you need to lookup their doc values. In the [article](https://www.elastic.co/kr/blog/better-query-planning-for-range-queries-in-elasticsearch) you mentioned, this filter is a term query that matches 10,000 documents (0.1% of the index). Thus even if we have an index of 10M documents, we need to look up doc values only of 10K documents that satisfy our filter, which makes sorting operation much faster.

2. You can [sort your index](https://www.elastic.co/guide/en/elasticsearch/reference/current/index-modules-index-sorting.html) by a field on which you expect to run a lot of sort queries. In this case, your doc Ids will be reorganized to to match the index sort criteria, and this type of sort queries will be super fast.

3. We have recently done [sort optimization for numeric fields](https://www.elastic.co/blog/optimizing-sort-queries-in-elasticsearch-for-faster-results), where internally we would use points data structure instead of doc values for some cases, which would make sorting for those cases much faster.

---

<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:** [March 20, 2022, 10:43am UTC](https://discuss.elastic.co/t/i-wonder-why-docvalues-are-fast-for-sorting/297039/4 "2022-03-20T10:43:02Z")

</div>

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