<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/a-guide-to-query-performance-q5avubs2p" -->

---
title: A Guide to Query Performance | daily.dev
description: A step-by-step walkthrough of query performance tuning in Azure DocumentDB, starting from identifying slow queries via Azure Log Analytics and KQL, through...
canonical: https://daily.dev/posts/a-guide-to-query-performance-q5avubs2p
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: A Guide to Query Performance | daily.dev
og:description: A step-by-step walkthrough of query performance tuning in Azure DocumentDB, starting from identifying slow queries via Azure Log Analytics and KQL, through...
og:url: https://daily.dev/posts/a-guide-to-query-performance-q5avubs2p
og:image: https://api.daily.dev/og/posts/q5aVUBS2P.png
og:image:alt: A Guide to Query Performance
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# A Guide to Query Performance

**[DevBlogs](https://daily.dev/sources/devblogs)** · 10 min read · 1 upvotes · 0 comments

## Summary

A step-by-step walkthrough of query performance tuning in Azure DocumentDB, starting from identifying slow queries via Azure Log Analytics and KQL, through reading explain() output, to designing optimal compound indexes using the ESR (Equality, Sort, Range) rule. The guide demonstrates how a poorly indexed query scanning 333K documents can be reduced to examining exactly 18 keys in 0.053ms. It also covers index-backed sorts (eliminating in-memory SORT stages), covered queries (eliminating FETCH stages), common indexing mistakes like putting range fields first or mismatching sort directions, and a practical ESR checklist.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://devblogs.microsoft.com/documentdb/query-performance-tuning-guide>

## Questions this post answers

### What is the ESR rule for designing MongoDB compound indexes?

ESR stands for Equality, Sort, Range: place fields used in exact-match filters first, followed by the field used in sort(), and finally fields used with range operators like $gt, $lt, $gte, or $lte. This ordering lets the index narrow results before sorting or scanning ranges, avoiding both full collection scans and in-memory sorts.

_Developers tuning MongoDB or DocumentDB indexes can find ESR examples and diagnostics workflows on daily.dev._

### How do I check if a MongoDB or DocumentDB query is using an index-backed sort instead of an in-memory sort?

Run explain("executionStats") and check the winning plan for the absence of a SORT stage above the IXSCAN. If FETCH sits directly on top of IXSCAN with no SORT stage, the sort is index-backed. If a SORT stage appears between FETCH and IXSCAN, the engine is sorting results in memory, which is expensive at scale.

_Track index-backed sort verification techniques for MongoDB-style databases through daily.dev._

### Why is a single-field index like createIndex({status:1}) not enough to speed up a query filtering on status, customerId, and createdAt with a sort?

A single-field index only narrows results by that one field, leaving the other filters and the sort unsupported by the index. In one measured case, indexing only status still required scanning 249,800 index keys to return 18 documents, and the sort still executed in memory. A compound index following the ESR rule across all three fields eliminated both problems, cutting execution to 18 keys examined and 0.058ms.

_Compare single-field versus compound indexing trade-offs for query performance on daily.dev._

## Similar posts on daily.dev

- [Aggregation Optimization in MongoDB: Sorting With Indexes \(Part 5\)](https://daily.dev/posts/aggregation-optimization-in-mongodb-sorting-with-indexes-part-5--ubdqt9zdr) · Foojay.io · 1 upvotes · 0 comments
- [How to Fix Slow EF Core Queries: Practical Performance Tuning](https://daily.dev/posts/how-to-fix-slow-ef-core-queries-practical-performance-tuning-effrdqtga) · Syncfusion · 38 upvotes · 0 comments
- [How to Optimize Enterprise Application Performance with T-SQL Query Tuning and Indexing Strategies](https://daily.dev/posts/how-to-optimize-enterprise-application-performance-with-t-sql-query-tuning-and-indexing-strategies-167jhd7xe) · freeCodeCamp · 2 upvotes · 0 comments
- [Improve query performance with EXPLAIN plans in Amazon Aurora DSQL](https://daily.dev/posts/improve-query-performance-with-explain-plans-in-amazon-aurora-dsql-n9nu4ezma) · AWS Database Blog · 0 upvotes · 0 comments
- [Top 10 best practices tips for ClickHouse](https://daily.dev/posts/top-10-best-practices-tips-for-clickhouse-a5ciehuus) · ClickHouse · 1 upvotes · 0 comments

---

Tags: [#backend](https://daily.dev/tags/backend), [#mongodb](https://daily.dev/tags/mongodb)

[View this post on daily.dev](https://daily.dev/posts/a-guide-to-query-performance-q5avubs2p)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"A Guide to Query Performance","url":"https://daily.dev/posts/a-guide-to-query-performance-q5avubs2p","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/a-guide-to-query-performance-q5avubs2p"},"datePublished":"2026-07-27T20:17:18.768Z","dateModified":"2026-09-14T06:27:25.910Z","description":"A step-by-step walkthrough of query performance tuning in Azure DocumentDB, starting from identifying slow queries via Azure Log Analytics and KQL, through...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/0a39b2c6f494256b8978d6e9b8b391bb?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/0a39b2c6f494256b8978d6e9b8b391bb?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"DevBlogs","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"DevBlogs","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/8f68b453325f482ebeb73fb780092713","url":"https://daily.dev/sources/devblogs"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/a-guide-to-query-performance-q5avubs2p","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":1},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"backend,mongodb","timeRequired":"PT10M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"DevBlogs","item":"https://daily.dev/sources/devblogs"},{"@type":"ListItem","position":3,"name":"A Guide to Query Performance"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/a-guide-to-query-performance-q5avubs2p#faq","mainEntity":[{"@type":"Question","name":"What is the ESR rule for designing MongoDB compound indexes?","acceptedAnswer":{"@type":"Answer","text":"ESR stands for Equality, Sort, Range: place fields used in exact-match filters first, followed by the field used in sort(), and finally fields used with range operators like $gt, $lt, $gte, or $lte. This ordering lets the index narrow results before sorting or scanning ranges, avoiding both full collection scans and in-memory sorts. Developers tuning MongoDB or DocumentDB indexes can find ESR examples and diagnostics workflows on daily.dev."}},{"@type":"Question","name":"How do I check if a MongoDB or DocumentDB query is using an index-backed sort instead of an in-memory sort?","acceptedAnswer":{"@type":"Answer","text":"Run explain(\"executionStats\") and check the winning plan for the absence of a SORT stage above the IXSCAN. If FETCH sits directly on top of IXSCAN with no SORT stage, the sort is index-backed. If a SORT stage appears between FETCH and IXSCAN, the engine is sorting results in memory, which is expensive at scale. Track index-backed sort verification techniques for MongoDB-style databases through daily.dev."}},{"@type":"Question","name":"Why is a single-field index like createIndex({status:1}) not enough to speed up a query filtering on status, customerId, and createdAt with a sort?","acceptedAnswer":{"@type":"Answer","text":"A single-field index only narrows results by that one field, leaving the other filters and the sort unsupported by the index. In one measured case, indexing only status still required scanning 249,800 index keys to return 18 documents, and the sort still executed in memory. A compound index following the ESR rule across all three fields eliminated both problems, cutting execution to 18 keys examined and 0.058ms. Compare single-field versus compound indexing trade-offs for query performance on daily.dev."}}]}
```

