Performance Optimization

Fast applications improve user experience and cut infrastructure cost — but the way to get there is disciplined, not heroic. The cardinal rule: measure before you optimize. Most performance problems concentrate in a few hot spots (very often the database), and intuition about where the time goes is famously unreliable. Profile, find the real bottleneck, fix the one with the biggest impact, then measure again.

There's a natural order of leverage: algorithmic complexity and database access dominate, network round-trips and caching come next, and micro-level code tweaks are a last resort. Spending a day shaving microseconds off a function that runs once is the classic premature-optimization trap.

TL;DR

Quick Example

Profile to find the real hot spot before touching anything:

Core Concepts

Metrics

The optimization hierarchy

Profiling

Python has cProfile (function level, above) and line_profiler (line level). The goal is always the same: let data, not hunches, point you at the hot spot.

Database Optimization

The most common and highest-impact area — add indexes, analyze with EXPLAIN, and kill N+1 queries:

See Database Indexing.

Caching, Batching & Async

Cache repeated work, batch I/O, and parallelize independent calls:

Also reuse expensive resources via connection pooling (pool_size, max_overflow) rather than opening a connection per request. See Caching.

Monitoring & Budgets

Optimization isn't done at deploy — watch production and enforce targets:

Best Practices

Common Mistakes

Optimizing without measuring

The N+1 query

FAQ

Why is "measure before optimizing" so emphasized?

Because developers are reliably bad at guessing where time goes — the bottleneck is often somewhere mundane (an unindexed query, a serial loop of network calls) rather than the clever code you suspect. Profiling shows the truth, so you spend effort where it actually moves p95/p99 latency. Optimizing unmeasured code wastes time and often adds complexity for no real gain.

What's the single highest-impact area to look at first?

The database, in most web applications. Missing indexes, N+1 access patterns, and inefficient queries dominate latency far more often than application code. Run EXPLAIN ANALYZE on slow queries, add the right indexes, and eliminate N+1 with joins or batch loading. After the database, look at reducing network round-trips and adding caching before touching low-level code.

What is the N+1 query problem?

It's fetching a list with one query, then issuing an additional query per item — 1 + N queries instead of 1 or 2. For 100 users with posts, that's 101 queries and 101 round-trips. Fix it with a join, an IN-clause batch load, or your ORM's eager-loading. It's one of the most common and most impactful performance bugs in data-heavy apps.

When is micro-optimizing code worth it?

Rarely, and only after profiling proves a specific function is a genuine hot spot that algorithmic, query, caching, and concurrency improvements haven't resolved. Code-level tweaks (avoiding allocations, tightening loops) are the bottom of the optimization hierarchy because they usually yield small gains relative to fixing an O(n²) algorithm or an N+1 query. Don't start here.

Related Topics

References