Skip to content
All Notes

Always Profile Before You Optimize

performancepythonsql

Problem

I assumed migrating a synchronous FastAPI service to async would be the biggest performance win. I was ready to rewrite the entire database layer.

Key Insight

Profiling with Datadog APM revealed that SQL query execution was 62% of total latency. The async framework overhead was only 8%. The real bottleneck was N+1 queries and missing composite indexes.

One index addition reduced a 200ms query to 12ms. The SQL rewrites delivered 3x more improvement than the async migration.

Takeaway

Measure first. The bottleneck is rarely where you think it is. Profile, identify the actual hotspot, fix that — then move to the next one. Intuition-driven optimization wastes engineering time on the wrong problems.