Slow queries are rarely solved by one trick. In production, performance comes from a sequence of improvements: index strategy, query shape, and sensible caching.
1) Start with the execution plan
Use EXPLAIN to find full table scans, incorrect join order, and missing indexes. Optimizing blindly wastes time.
2) Index for actual filter paths
Composite indexes should match real WHERE and ORDER BY patterns. Add indexes based on usage, not assumptions.
3) Reduce query payload
Select only required columns, paginate long result sets, and avoid loading expensive joins when summary data is enough.
4) Cache expensive but stable responses
For report endpoints with repeated requests, short-lived caching can remove pressure from database hotspots.
The biggest result is user trust: when dashboards respond quickly, teams use them more and rely on data-driven workflows.