EXPLAIN
SELECT *
FROM orders
WHERE customer_id = 101;
If supported by your database:
EXPLAIN ANALYZE
SELECT *
FROM orders
WHERE customer_id = 101;
🔥 Mini Challenge
You have an "orders" table containing 50 million
rows:
• order_id
• customer_id
• order_date
• status
• region
• order_amount
The following query is running slowly:
SELECT
order_id,
order_date,
order_amount
FROM orders
WHERE customer_id = 5001
AND order_date >= DATE '2026-01-01'
AND status = 'Completed';
Think about:
1. Which columns are being filtered?
2. Would a composite index be worth investigating?
3. Which column should come first?
4. Should you use "SELECT *"?
5. How would you inspect the execution plan?
6. Would the index always be used?
7. What happens to performance when the table receives millions of new rows?
Possible candidate to investigate:
CREATE INDEX idx_orders_customer_date
ON orders(customer_id, order_date);
Then inspect the query:
EXPLAIN
SELECT
order_id,
order_date,
order_amount
FROM orders
WHERE customer_id = 5001
AND order_date >= DATE '2026-01-01'
AND status = 'Completed';
Don't automatically assume this is the optimal index.
Use the execution plan, data distribution, workload, and database-specific behavior to determine whether it actually helps.
🎯 Key Takeaway
Remember:
INDEX → Helps the database find data efficiently
COMPOSITE INDEX → Index on multiple columns
EXPLAIN → Inspect how the database plans to execute a query
SELECTIVITY → How much a filter narrows the data
And the most important principle:
More indexes ≠ Always faster
Good indexing + Good query design + Execution-plan analysis = Better SQL performance
A strong Data Analyst doesn't just write SQL that produces the correct answer.
They also understand how that SQL behaves when the data grows from thousands of rows to millions or billions. 🚀
🎯 Double Tap ❤️ For More