Back to library

🐢SQL Query Optimization

Stop guessing why a query is slow and start reading its plan like a story — see how the planner picks scans, joins, and orders, then take a real production query from minutes to milliseconds by reasoning from the plan, not from superstition.

Advanced14 drops~2-week path · 5–8 min/daytechnology

Phase 1How the Planner Thinks

See how the planner turns your SQL into a physical plan

4 drops
  1. SQL is a wish — the plan is what actually happens

    6 min

    SQL is a wish — the plan is what actually happens

  2. The planner picks the cheapest plan it can imagine

    7 min

    The planner picks the cheapest plan it can imagine

  3. The planner is only as smart as its row estimates

    7 min

    The planner is only as smart as its row estimates

  4. Every plan is a tree of pull-based operators

    6 min

    Every plan is a tree of pull-based operators

Phase 2Reading EXPLAIN ANALYZE in Anger

Read EXPLAIN ANALYZE on progressively gnarlier real queries

5 drops
  1. Seq Scan isn't always wrong — sometimes it's the right answer

    7 min

    Seq Scan isn't always wrong — sometimes it's the right answer

  2. Three join algorithms, three different stories

    7 min

    Three join algorithms, three different stories

  3. Filter and Index Cond mean very different things

    7 min

    Filter and Index Cond mean very different things

  4. Sort spills to disk are silent assassins

    7 min

    Sort spills to disk are silent assassins

  5. BUFFERS counts are the only honest measure of work

    7 min

    BUFFERS counts are the only honest measure of work

Phase 3Stats, Indexes, and Join Order Together

Connect stats, indexes, and join order to plan decisions

4 drops
  1. Your CTO says "this query is slow — fix it"

    7 min

    Your CTO says "this query is slow — fix it"

  2. An on-call page during dinner

    7 min

    An on-call page during dinner

  3. The new index that didn't help

    7 min

    The new index that didn't help

  4. OFFSET 100,000 LIMIT 20

    7 min

    OFFSET 100,000 LIMIT 20

Phase 4Optimize a Real Slow Query End-to-End

Take a real slow query from minutes to milliseconds

1 drop
  1. Take one real slow query from minutes to milliseconds

    8 min

    Take one real slow query from minutes to milliseconds

Frequently asked questions

What's the difference between EXPLAIN and EXPLAIN ANALYZE?
This is covered in the “SQL Query Optimization” learning path. Start with daily 5-minute micro-lessons that build from fundamentals to hands-on application.
Why is my query slow even though I added an index?
This is covered in the “SQL Query Optimization” learning path. Start with daily 5-minute micro-lessons that build from fundamentals to hands-on application.
How do I tell if the planner is choosing a bad join order?
This is covered in the “SQL Query Optimization” learning path. Start with daily 5-minute micro-lessons that build from fundamentals to hands-on application.
What does a high 'rows removed by filter' number mean in a plan?
This is covered in the “SQL Query Optimization” learning path. Start with daily 5-minute micro-lessons that build from fundamentals to hands-on application.
When should I rewrite a query versus add an index?
This is covered in the “SQL Query Optimization” learning path. Start with daily 5-minute micro-lessons that build from fundamentals to hands-on application.