Real-World Performance Tuning Cases: Performance
Module: Query Optimization & Performance
**Prioritization Framework:**
Impact = Speedup × Frequency
Example:
- Query A: 10x speedup, 1000 calls/day = 10,000 impact
- Query B: 100x speedup, 10 calls/day = 1,000 impact
- Fix Query A first (higher total impact)
**Effort Estimation:**
Low effort (minutes):
- Add simple index
- Update statistics
- Adjust work_mem
Medium effort (hours):
- Add covering index
- Rewrite query
- Add materialized view
High effort (days):
- Partition table
- Redesign schema
- Add caching layer
**Risk Assessment:**
Low risk:
- Add index (can drop if not helpful)
- Update statistics (reversible)
- Increase work_mem for session
Medium risk:
- Rewrite query (must validate results)
- Change configuration (affects all queries)
High risk:
- Schema changes (requires migration)
- Partitioning (complex, hard to reverse)
**Measurement Best Practices:**
1. Baseline: Measure before optimization
2. Controlled: Change one thing at a time
3. Validated: Verify results are correct
4. Sustained: Monitor for regressions
5. Documented: Record what, why, results
pg_stat_statements shows total time (execution × calls) - prioritize high total time
EXPLAIN ANALYZE red flags: Seq Scan on large table, estimated ≪ actual, Disk usage
Missing index: 10-1000x speedup, low effort, high impact
Stale statistics: 3-50x speedup, ANALYZE takes seconds