Optimization

Creating and Using Indexes

Senior Data Analyst
March 14, 2026
6 min read

What is an Index?

Think of it like the index in the back of a textbook. Instead of reading every page to find "SQL," you look it up in the index and go directly to page 57.

Creating an Index

CREATE INDEX idx_orders_customer_id ON orders (customer_id);

When to Index

  • Columns in `WHERE` clauses.
  • Columns in `JOIN` conditions.
  • Columns in `ORDER BY` clauses.
  • When NOT to Index

  • Columns with very few unique values (e.g., a boolean `is_active`).
  • Tables that are mostly written to (indexes slow down `INSERT`/`UPDATE`).
  • Pro Tip

    Too many indexes can be as bad as too few. Each index adds overhead to write operations.

    *Day 74: Composite Indexes.*

    Ready to put your knowledge into practice?

    Join SQL Mastery and learn through interactive exercises.