Day 4 of 5
⏱ ~60 minutes
Database Internals in 5 Days — Day 4

Query Planning & Optimization

Parse/plan/execute pipeline, cost model, join strategies, EXPLAIN output

What You'll Cover Today

Day 4 of Database Internals in 5 Days pushes into advanced territory. You have enough foundation now to tackle real-world complexity. Today's exercise is more open-ended than earlier days — that's intentional.

ℹ️
Topics today: EXPLAIN, cost model, join types. Each section has code you can copy and run immediately.

EXPLAIN

Understanding EXPLAIN is the core goal of Day 4. The concept is straightforward once you see it in practice — most confusion comes from skipping the mental model and jumping straight to implementation. Start with the model, then write the code.

EXPLAIN
# EXPLAIN — Working Example
# Study this pattern carefully before writing your own version

class EXPLAINExample:
    """
    Demonstrates core EXPLAIN concepts.
    Replace placeholder values with your real implementation.
    """
    
    def __init__(self, config: dict):
        self.config = config
        self._validate()
    
    def _validate(self):
        required = ['name', 'type']
        for field in required:
            if field not in self.config:
                raise ValueError(f"Missing required field: {field}")
    
    def process(self) -> dict:
        # Core logic goes here
        result = {
            'status': 'success',
            'topic': 'EXPLAIN',
            'data': self.config
        }
        return result


# Usage
example = EXPLAINExample({
    'name': 'my-implementation',
    'type': 'explain'
})
output = example.process()
print(output)
💡
Key insight: When working with EXPLAIN, always start with the simplest possible case that works end-to-end. Complexity is easier to add than simplicity is to recover.

cost model

cost model is the practical application of EXPLAIN in real projects. Once you understand the underlying model, cost model becomes the natural next step.

💡
Pro tip: When working with cost model, always read the official documentation for the exact version you're using. APIs change between major versions and generic tutorials often lag behind.

join types

join types rounds out today's lesson. It connects EXPLAIN and cost model into a complete picture. You'll use all three concepts together in the exercise below.

Common Mistakes on Day 4

📝 Day 4 Exercise
Query Planning & Optimization — Hands-On
  1. Set up your environment for today's topic: install required tools and verify the basics work before writing any logic.
  2. Implement a minimal working version of EXPLAIN using the code example in this lesson as your starting point.
  3. Extend your implementation to incorporate cost model — this is where the two concepts connect.
  4. Test your implementation with both valid and invalid inputs. What happens at the boundaries?
  5. Review your code: is there anything you'd name differently? Any function doing more than one thing? Refactor one thing.

Day 4 Summary

  • EXPLAIN is the foundation of today's lesson — understand it before moving on.
  • cost model is how you apply it in real projects.
  • join types ties the day's concepts together into a complete pattern.
  • Error handling and input validation belong in the first version, not as an afterthought.
  • Read error messages carefully — they usually tell you exactly what's wrong.
Challenge

Extend today's exercise by adding one feature that wasn't in the instructions. Document what you built in a comment at the top of the file. This habit of going one step further is what separates engineers who grow fast from those who stay stuck.

Finished this lesson?