Optimize SQL Queries with Execution Plans

A query was taking 4 minutes in production. I spent 2 days rewriting the SQL logic. Tried different joins. Tried different filters. Tried breaking it into CTEs. Nothing helped. Then my senior said: "Did you look at the execution plan?" I didn't even know what that was. I looked. Found the problem in 30 seconds. Fixed it in 5 minutes. Query now runs in 12 seconds. 👇 ──────────────── What is an execution plan? It is the database's internal roadmap. It shows you EXACTLY what happens when your query runs — step by step. Which tables it scans. Which indexes it uses. Where it is spending the most time. Stop guessing. Start reading the plan. ──────────────── How to read it — Snowflake and SQL Server: ----- SNOWFLAKE: use EXPLAIN EXPLAIN SELECT c.customer_name, SUM(o.revenue) AS total_revenue FROM customers c JOIN orders o ON c.customer_id = o.customer_id WHERE o.order_date >= '2024-01-01' GROUP BY c.customer_name; ----- SQL SERVER: use SET STATISTICS SET STATISTICS IO ON; SET STATISTICS TIME ON; SELECT c.customer_name, SUM(o.revenue) AS total_revenue FROM customers c JOIN orders o ON c.customer_id = o.customer_id WHERE o.order_date >= '2024-01-01' GROUP BY c.customer_name; ──────────────── 3 things to look for first: → Full table scan — reading ALL rows when you need 100 Fix: add a clustering key or index on filter columns → Wrong row estimates — engine guesses wrong counts Fix: update table statistics so planner works correctly → Nested loops on large tables — wrong join algorithm Fix: ensure join columns are indexed on both sides ──────────────── What I found in my 4 minute query: Full table scan on a 200 million row table. No index on order_date. One clustering key later → 4 minutes became 12 seconds. I had rewritten SQL for 2 days. The fix was one command. Found in 30 seconds. ──────────────── Read the execution plan before you rewrite the query. Always. Without exception. What is the slowest query you have ever fixed? Drop the before and after below 👇 #SQL #DataEngineering #QueryOptimization #DataEngineer #SQLServer #Snowflake #DatabasePerformance #Analytics #DataWarehouse #TechTips #OpenToWork #DataCommunity #HiringDataEngineers #100DaysOfSQL

  • No alternative text description for this image

Open to Data Engineer roles with SQL, Snowflake, PySpark and Azure — 4.5 yrs at GfK NIQ. DM me or tag someone hiring!

Like
Reply

To view or add a comment, sign in

Explore content categories