4 Hours

5. Indexing Strategies for Performance

Performance

Indexing Strategies: Making Queries Fast

Indexes are the most powerful tool for database performance. They make queries fast by providing efficient access to data. But indexes come with costs — they take space and slow down writes. Understanding indexing strategies is essential for building high-performance applications.

An index is like a book index. It stores a small subset of data in a sorted structure, allowing the database to find rows quickly without scanning the entire table. Without indexes, queries are slow. With indexes, queries are fast. But indexes are not free — each index adds overhead to INSERT, UPDATE, and DELETE operations. This module covers index types, design strategies, and trade-offs.

Understanding B-Tree Indexes

B-Tree indexes are the default and most common index type. They store values in a balanced tree structure, enabling O(log n) lookups. This means finding a row in a million-row table requires only about 20 comparisons. B-Tree indexes support equality (=), range (> <), and ordering (ORDER BY) operations.

B-Tree indexes are versatile and work well for most queries. They are the first index type to consider. The key is to create indexes for your most common queries. Analyze your query patterns and create indexes that support them.

SQL
-- Creating a simple B-Tree index
CREATE INDEX idx_users_email ON users(email);

-- Creating a compound index (multiple columns)
CREATE INDEX idx_orders_user_status ON orders(user_id, status, created_at DESC);

-- Creating a unique index
CREATE UNIQUE INDEX idx_users_email_unique ON users(email);

-- Creating a partial index (indexes only some rows)
CREATE INDEX idx_active_users ON users(created_at) WHERE status = 'active';

-- Creating an expression index (indexes a computed value)
CREATE INDEX idx_users_lower_email ON users(LOWER(email));

-- Creating a covering index (includes extra columns)
CREATE INDEX idx_users_covering ON users(email) INCLUDE (name, created_at);

Index Design Strategies

Index design is about trade-offs. The left-prefix rule for compound indexes means that the order of columns matters. An index on (a, b, c) can be used for queries filtering on a, a+b, or a+b+c, but not on b or c alone. Design indexes starting with the most selective column.

Index TypeSyntaxBest ForExample Query
Single-ColumnCREATE INDEX ON users(email)Simple equality queriesWHERE email = alice@...
CompoundCREATE INDEX ON orders(user_id, status)Multiple conditionsWHERE user_id=1 AND status=pending
PartialCREATE INDEX ON orders(created_at) WHERE status=activeSubset of rowsWHERE status=active AND created_at > ...
CoveringCREATE INDEX ON users(email) INCLUDE (name, age)Queries needing only indexed columnsSELECT name, age FROM users WHERE email=...
  • Single-column indexes: Good for simple equality and range queries.
  • Compound indexes: Good for queries with multiple conditions. Order matters (left-prefix rule).
  • Partial indexes: Good for queries that only need a subset of data.
  • Expression indexes: Good for queries with transformations like LOWER(email).
  • Covering indexes: Good for queries that only need indexed columns (no table access needed).

Knowledge Check

Ready to test your understanding of 5. Indexing Strategies for Performance?

Back