Performance

Introduction to Indexes (B-Trees)

SQL Mastery Team
April 18, 2026
6 min read

It's **Day 87**. Today we learn about the most powerful tool for speed: the **Index**.

What is an Index?

Think of a massive 1,000-page book. If you want to find the chapter on "JOINS," you don't read every page. You go to the **Index** at the back, find the page number, and jump straight there.

In SQL, a B-Tree (Balanced Tree) index works the same way. It organizes your data so the database can find any value in just a few "Jumps."

Creating an Index

CREATE INDEX idx_users_email ON users (email);

How much faster is it?

  • **Without Index**: $O(N)$ time. (Searching 1M rows takes 1M comparisons).
  • **With Index**: $O(\log N)$ time. (Searching 1M rows takes roughly **20** comparisons).
  • That is a **50,000x** speed improvement for a single line of code!

    The Catch: There's No Free Lunch

    Indexes don't make *everything* faster.

    1. **Writes are slower**: Every time you `INSERT` or `UPDATE`, the database also has to update the index.

    2. **Disk Space**: Indexes take up extra space on your server.

    Your Task for Today

    Find a column you frequently filter in your `WHERE` clause. Create an index for it and see if your query feels faster.

    *Day 88: Composite Indexes—When one column isn't enough.*

    Ready to put your knowledge into practice?

    Join SQL Mastery and learn through interactive exercises.