A walkthrough of diagnosing and fixing a PostgreSQL query that took over 5 minutes to run. The process covers reading the execution plan, identifying two root causes — an expensive self-join on a large table and a non-sargable condition using the ABS() function — and iterating through fixes. Replacing ABS() with a range condition, adding indexes (which had no effect), and comparing findings with Microsoft Copilot are all covered. The breakthrough came from rewriting the query to pre-aggregate track/price counts using CTEs, reducing the cost from ~7.7 million to ~60,000 and runtime from ~6 minutes to under 2 seconds. Key takeaways: always baseline before changing, avoid wrapping columns in functions in join/where clauses, and treat AI suggestions as one input rather than a replacement for understanding the execution plan.

19m watch time
4 Impressions