Speed Up a Slow Report Query
Part of the SQL Roadmap
In short
This project puts the whole phase together. You take one genuinely slow query, prove why it is slow, fix it and show the before and after. It's the kind of story interviewers love to hear.
A project where you take a slow query on a large table, read its plan, apply indexes and rewrites, and document the before and after timings.

Think of it like this
Tuning a slow scooter
A good mechanic in a Lucknow gali doesn't replace random parts. He takes the scooter for a timed test ride, opens it up, finds the clogged carburettor, fixes that one thing and rides again with the stopwatch. Then he writes it in the service book. Query tuning follows exactly the same routine: measure, diagnose, fix one thing, measure again, write it down.
Watch it happen
The March city report, before and after
Before
- DATE_TRUNC on order_date in WHERE
- No index for status or date
- Seq Scan over every order ever placed
- Slow, and slower every month
After
- Plain date range in WHERE
- Index on (status, order_date) INCLUDE (customer_id, amount)
- Index Only Scan on one month's slice
- Stays fast as the table grows
The fix was two small changes, but you only know which two by reading the plan first.
The big ideas
What you need to know
Measure first
Run EXPLAIN ANALYZE a few times and record the time. Without a baseline you can't prove anything improved.
Diagnose from the plan
Find the heaviest node. Is it a Seq Scan, a bad estimate, a huge sort or a join that multiplies rows?
One change at a time
Rewrite the filter, or add an index, then measure. Changing five things at once hides what actually helped.
Document it
Write the original query, plan, fix and timings in a README. That record is the project.
How it works
The tuning loop
- 1
Get a big table
Generate or load a few lakh orders so slowness is visible.
- 2
Baseline
Run EXPLAIN (ANALYZE, BUFFERS) three times and note the typical time.
- 3
Diagnose
Spot the expensive node and the reason: a function on a column, a missing index, stale stats.
- 4
Fix
Make the filter SARGable, add a targeted index, then ANALYZE the table.
- 5
Re-measure and write up
Run the same EXPLAIN again and record before, after and why in your README.
See it in code
Walk through the code
1-- BEFORE: function on the column, no useful index2EXPLAIN (ANALYZE, BUFFERS)3SELECT c.city, COUNT(*) AS orders, SUM(o.amount) AS revenue4FROM orders o5JOIN customers c ON c.customer_id = o.customer_id6WHERE DATE_TRUNC('month', o.order_date) = DATE '2026-03-01'7 AND o.status = 'delivered'8GROUP BY c.city9ORDER BY revenue DESC;10 11-- FIX: an index that matches the filter and covers the columns12CREATE INDEX idx_orders_status_date13 ON orders (status, order_date) INCLUDE (customer_id, amount);14ANALYZE orders;15 16-- AFTER: same report, SARGable date range17EXPLAIN (ANALYZE, BUFFERS)18SELECT c.city, COUNT(*) AS orders, SUM(o.amount) AS revenue19FROM orders o20JOIN customers c ON c.customer_id = o.customer_id21WHERE o.order_date >= DATE '2026-03-01'22 AND o.order_date < DATE '2026-04-01'23 AND o.status = 'delivered'24GROUP BY c.city25ORDER BY revenue DESC;1/3The original report. DATE_TRUNC on order_date forces a full scan, and nothing indexes status.
PostgreSQL syntax. In MySQL, drop INCLUDE and put customer_id and amount at the end of the index key, and use EXPLAIN ANALYZE without BUFFERS.
Save yourself the pain
Common mistakes
Oops
Testing on a 500-row table and declaring the query 'fast'.
Do this instead
Load realistic volume. Problems like a missing index only show up with lakhs of rows.
Oops
Timing only once, right after creating the index.
Do this instead
Run each version a few times. The first run may read from disk while later runs hit the cache.
Oops
Reporting 'it's faster now' with no numbers.
Do this instead
Show the before and after plans and timings side by side, and explain which change caused the gain.
Your mission
Ship the project
Quick check
You added an index and rewrote the WHERE clause at the same time, and the query got faster. What's the problem?
Go deeper
- 1Generate or download a table with at least a million rows.
- 2Record EXPLAIN output and timings before and after each change.
- 3Write a short README explaining what made the biggest difference.
Stuck on this topic? Ask an Insider
Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.