NoSQL · Data models beyond tables

How do you diagnose and improve NoSQL query performance?

Mediumquery performanceindexesexplain plansUpdated Oct 2026
Quiz this track

Short answer

Find out whether the query uses an index or scans, then reduce the data read and the partitions touched. Check key design, projection, pagination and caching before adding hardware.

Start with the execution plan and the actual metrics: rows or documents examined, time spent, partitions contacted and bytes returned. A slow query is usually reading far more data than it returns or waiting on a hot partition.

js
db.orders.find({ customerId: "cus_7" })
  .sort({ createdAt: -1 })
  .limit(20)
  .explain("executionStats");

const plan = db.orders.find({ customerId: "cus_7" })
  .sort({ createdAt: -1 })
  .limit(20)
  .explain("queryPlanner");
Look for a COLLSCAN versus an IXSCAN, and compare keys examined with documents returned.

The usual fixes

  • Add or fix an index so the filter and sort are supported
  • Project only the needed fields to reduce network and memory
  • Paginate with a stable cursor instead of large offsets
  • Avoid scans and unbounded ranges on large collections
  • Cache repeated reads when staleness is acceptable
  • Fix the key design if one partition receives most of the traffic

In distributed stores, latency is often dominated by the slowest replica or by cross-partition fan-out. Measure p99, not just the average, and separate query time from queueing and network time. A query that is fast on average can still be unacceptable for a small fraction of users.

Do not add an index for every slow query without checking the write cost and memory footprint. Indexes speed reads and slow writes; the right number is the smallest set that covers the important patterns.

Follow-ups you should expect

Interviewers use these to check whether the first answer was understanding or memorisation.

  1. 1What is the difference between a collection scan and an index scan?
  2. 2Why is offset pagination slow on large datasets?
  3. 3How do you investigate p99 latency?
  4. 4When is caching the right fix?

Say it in the interview

I start with the execution plan and the real metrics: documents examined versus returned, partitions touched, and time breakdown. Usually the query is scanning or reading far more than it needs. Then I fix the index, projection, pagination or key design. In a distributed store I look at the slowest replica and cross-partition fan-out, and I use p99 rather than averages. I add indexes deliberately, because each one costs write throughput and memory.

Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.

Related questions

Reading is step one

Now say it out loud.

Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live NoSQL mock interview and get scored feedback on depth, clarity and confidence.