SQL Query Optimization Guide
Query analysis techniques, execution plan reading, index strategies, partitioning patterns, and anti-pattern catalog.
📄 Product Preview
Try the interactive reader and demo tools below, or get the full product with all content unlocked.
📖 Interactive Reader (Free Preview) ⚙ Try Demo Tools 📦 Download Free Sample📁 File Structure 23 files
📖 Documentation Preview README excerpt
SQL Query Optimization
A hands-on, vendor-neutral guide to making SQL queries fast and keeping them
fast. Not a reference manual to memorize — a set of runnable .sql files and
field-tested runbooks that teach the one loop every optimization comes down to:
**read the execution plan, find the expensive step, change one thing, re-read the
plan.** Every technique is shown on a real schema with before/after plans and the
reasoning behind each rewrite.
Written for PostgreSQL 12+ and MySQL 8.0 / MariaDB 10.5+, with the dialect
differences (and there are real ones — merge joins, INCLUDE, functional indexes,
descending indexes) called out inline as you meet them. Nothing here is
ORM-specific or framework-specific; it's the SQL underneath all of them.
What's included
sql-query-optimization/
├── README.md
├── LICENSE
├── sql/ # runnable, heavily-commented lessons
│ ├── 01_explain_plan_basics.sql Read a plan in both engines
│ ├── 02_index_design.sql Selectivity, composites, covering
│ ├── 03_sargable_predicates.sql WHERE clauses an index can seek
│ ├── 04_join_strategies.sql Nested loop vs hash vs merge
│ ├── 05_window_functions.sql Replace self-joins & correlated subqueries
│ ├── 06_query_rewrites.sql Equivalent shapes that run 100x faster
│ ├── 07_antipatterns.sql The recurring mistakes + fixes
│ └── 08_pagination.sql Keyset pagination vs deep OFFSET
├── docs/ # the runbooks (the "why")
│ ├── READING-EXECUTION-PLANS.md EXPLAIN/ANALYZE, estimate vs actual
│ ├── INDEX-DESIGN.md The index decision framework
│ ├── JOIN-STRATEGIES.md Which join, when, and how to steer it
│ ├── WINDOW-FUNCTIONS.md Pattern catalogue (ranking, running totals)
│ ├── ANTI-PATTERNS.md Field guide: smell → why → fix
│ └── QUERY-REWRITE-PATTERNS.md Result-preserving rewrites
└── examples/
└── sample_schema.sql Portable e-commerce schema + seed data
Each sql/0X file pairs with a docs/ runbook: the .sql is the runnable
lesson you paste into a session; the .md is the deeper explanation, decision
tables, and FAQ.
Prerequisites
- PostgreSQL 12+ or MySQL 8.0 / MariaDB 10.5+, and its standard CLI
(psql or mysql). Everything runs in a plain SQL session — no extensions
required for the core lessons (a couple of optional notes mention pg_trgm /
FULLTEXT for substring search).
- A scratch database you can create and drop. The examples build and seed
~320,000 rows so plans behave like production, not like a 10-row toy.
- Comfort reading SQL. This is intermediate material: it assumes you know
JOIN/GROUP BY and want to know why one form outruns another.
- Window functions (
sql/05) require MySQL 8.0+ / MariaDB 10.2+ — not MySQL
5.7. Every other lesson works on 5.7 with the inline notes.
... continues with setup instructions, usage examples, and more.