# Slow queries: profiler to index

Symptom: an endpoint or job is slow and you suspect the database.

## Diagnose

1. Atlas UI: Performance Advisor. It lists slow query shapes grouped by pattern, with suggested indexes. This is only on M10+ clusters.
2. Cross-check with the Query Profiler (same area): it shows slow operations with key stats for the last few days, no extra cost.
3. Pick the worst shape by total time (frequency x avg duration), not the single slowest one-off.

## Confirm

Run the query shape with `explain("executionStats")` in mongosh. Look at `totalDocsExamined` vs `nReturned`. A big ratio means the query scans far more than it returns: missing or wrong index.

## Fix

Create the suggested index. The Performance Advisor can create it directly from the UI (one at a time), or run the `createIndex` in mongosh. Keep read/write ratio in mind: every index taxes writes.

## Verify

Re-run `explain`: `totalDocsExamined` should collapse toward `nReturned`, and the stage should be IXSCAN. Then watch the Performance Advisor over the next day to confirm the shape dropped off the slow list.