Lab 06: EXPLAIN & Query Optimization
Step 1 — MySQL: Basic EXPLAIN
USE labdb;
-- Create a meaningful dataset
CREATE INDEX idx_customer ON orders(customer);
CREATE INDEX idx_product ON orders(product);
-- Basic EXPLAIN
EXPLAIN
SELECT customer, COUNT(*) AS cnt, SUM(total) AS total_spent
FROM orders
WHERE customer = 'Alice'
GROUP BY customer;id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 SIMPLE orders NULL ref idx_customer idx_customer 403 const 2 100.00 NULLColumn
Meaning
Step 2 — MySQL: EXPLAIN FORMAT=JSON
Step 3 — PostgreSQL: EXPLAIN ANALYZE
Step 4 — Reading PostgreSQL Execution Plans
Node
Meaning
Step 5 — Cost Model and Statistics
Step 6 — Identifying Slow Queries
Step 7 — N+1 Pattern Detection
Step 8 — Capstone: Query Tuning Workflow
Summary
Tool
Purpose
Last updated
