CodeOath
← All posts
SQL65 min total · 16 parts

Understanding SQL Indexes and Query Performance

Contents — Part 2 of 16: What Problem Indexes Actually Solve
Part 2 of 16 · ~1 min

What Problem Indexes Actually Solve

Without an index, the only way a database can answer WHERE dept = 'Sales' is to check every single row, one at a time — a full table scan. That's O(n) work regardless of how selective the condition is, and on a table with millions of rows, it's the difference between a query returning instantly and one that takes seconds. An index trades a small amount of extra storage and write overhead for the ability to jump directly to matching rows instead of inspecting every one.

Diagram comparing a full table scan checking every row against an index seek jumping straight to the matching B-tree branch

CREATE INDEX idx_employees_dept ON Employees (dept);

This creates a sorted structure of dept values, each pointing back to its corresponding row, so WHERE dept = 'Sales' can jump straight to the matching entries instead of scanning the whole table — the same reason a book's index lets you jump to a page instead of reading cover to cover.