SQL Query and performance tuning - Explain and Analyse commands
๐ ️ Understanding SQL Query Performance with EXPLAIN
and ANALYZE
When it comes to SQL optimization, the first and most powerful step is understanding how your queries are actually executed. While SQL is declarative—you tell the database what you want—what happens under the hood is procedural and driven by a query execution plan.
This blog post will walk you through how to use PostgreSQL’s EXPLAIN
and ANALYZE
tools to explore, diagnose, and improve query performance, including how WHERE clauses and indexes influence plans.
๐ Meet EXPLAIN
: Peek Into the Execution Plan
Let’s start simple:
Output:
-
Seq Scan: A full table scan—every row is read.
-
Cost: Estimated computation units. Not real time, but useful for comparing query complexity.
-
Width: Amount of data (in bytes) returned per row.
๐ข For small tables, a Seq Scan is typically fast enough.
๐ด For large tables, it can be inefficient.
๐งช EXPLAIN ANALYZE
: Get Real Execution Metrics
To see what actually happens when the query runs, use:
This outputs real runtime stats:
If you reduce the number of columns:
Output:
๐ฏ Smaller row width = low memory consumption
๐ง Adding a WHERE Clause: Does It Help?
Let’s try filtering data:
This returns fewer rows (e.g., 717 out of 1000), but still uses a Seq Scan:
Compare this with the previous example of not having the where clause.
๐ Even though fewer rows are returned, the execution time is longer because of the extra filtering step.
๐️ Enter Indexes: How Do They Help?
Indexes can avoid full table scans, especially for selective queries.
Let’s create an index:
Now retry:
๐ด Still a Seq Scan?
That’s because too many rows match the condition. The planner estimates that scanning the entire table is faster than jumping between the index and table rows.
✅ Making Queries More Selective
Try a more selective query:
Now the index is likely used:
๐ Success! PostgreSQL uses the index because only a few rows match, making it faster than a full scan.
๐ง Takeaway: When Are Indexes Used?
Query Type | Index Used? |
---|---|
SELECT * FROM staff | ❌ No (Seq Scan) |
WHERE salary > 75000 (many rows) | ❌ No (still Seq Scan) |
WHERE salary > 150000 (few rows) | ✅ Yes (Index Scan) |
Indexes are used when the WHERE clause is highly selective, meaning it narrows the result set significantly.
๐ Metrics to Pay Attention To
Metric | What It Tells You |
---|---|
Cost | Estimated effort (useful for comparisons) |
Execution Time | Actual time the query took (ms) |
Rows | How many rows were returned |
Width | Avg. bytes per row (lower = faster I/O) |
Operation Type | Seq Scan vs Index Scan vs Filter, etc. |
๐งฉ Visual Metaphor: Zooming In with a Magnifying Glass
You can think of EXPLAIN ANALYZE
like using a magnifying glass on your data query:
-
๐ Without WHERE clause: You're looking at the whole pizza (table).
-
๐ With a WHERE clause: You're trying to find slices with only mushrooms.
-
๐️ With an index: You’ve already labeled slices by topping and can go directly to the mushrooms!
๐ง Final Thoughts: How to Use This in Practice
-
Use
EXPLAIN
to understand execution paths. -
Use
ANALYZE
to measure real-world performance. -
Index only columns used in selective WHERE clauses.
-
Watch row width to reduce data transfer time.
-
Remember: PostgreSQL is smart—it chooses plans based on cost and data distribution.
Comments
Post a Comment