How we cut Postgres p95 from 900 ms to 40 ms with one index
A slow dashboard query was scanning a 40M-row events table. A composite partial index matching the filter and sort turned the scan into an index-only read, without new hardware.
Key takeaways
- Run EXPLAIN (ANALYZE, BUFFERS) on the slowest query before adding anything.
- Put equality filters first in a composite index, the time range last.
- A partial index on recent rows kept the index small enough to stay in memory.
