100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

What is Database Indexing at Scale?

Learn how database indexing works at scale: B-trees, write amplification, composite, covering, and partial indexes for interviews.

mediumQ23 of 224 in System Design Est. time: 5 minsLast updated:
Open Code Lab

Expected Interview Answer

Database indexing at scale is the practice of building auxiliary data structures, typically B-trees or hash indexes, that let queries locate rows without scanning an entire table, while managing the real trade-offs indexes introduce once tables reach millions or billions of rows: write amplification, memory pressure, and index-maintenance cost.

At small scale an index is nearly free, but at scale every additional index roughly doubles write cost because each insert or update must also update every index on that table, and large indexes may not fully fit in memory, forcing expensive disk reads. Engineers at scale must be deliberate: choose composite indexes that match actual query patterns, avoid over-indexing tables with heavy write traffic, and consider covering indexes so a query can be satisfied from the index alone without touching the underlying table. At very large scale, indexing decisions also interact with sharding and partitioning, since an index that works well on one shard’s local data does not automatically give a fast answer for a cross-shard query. Techniques like index-only scans, partial indexes (indexing only a relevant subset of rows), and periodically rebuilding fragmented indexes become part of routine operations.

  • Turns O(n) table scans into O(log n) or better lookups
  • Composite and covering indexes can satisfy queries without touching table rows
  • Partial indexes reduce index size and maintenance cost for large tables
  • Well-chosen indexes are often the single biggest lever for read latency at scale

AI Mentor Explanation

A database index at scale is like a stadium’s detailed scorecard index sorted by batsman name across an entire multi-year archive, letting a statistician jump straight to a player’s record instead of flipping through every match ever played. Building that index for a small local club is trivial, but maintaining it for a national archive spanning decades means every new match played must also update the index, slowing down how fast new scorecards can be filed. A well-designed index only covers the fields analysts actually query, like player name and season, rather than indexing every possible statistic, keeping the update cost manageable even as the archive grows into millions of matches.

Step-by-Step Explanation

  1. Step 1

    Match indexes to query patterns

    Build composite indexes that reflect the actual WHERE and ORDER BY clauses used in production queries.

  2. Step 2

    Weigh write amplification

    Recognize that every index adds cost to inserts and updates, so avoid indexing columns nobody queries.

  3. Step 3

    Use covering and partial indexes

    Add covering indexes to satisfy queries from the index alone, and partial indexes to shrink index size on huge tables.

  4. Step 4

    Maintain indexes at scale

    Monitor for fragmentation, rebuild periodically, and ensure large indexes have enough memory to avoid disk-bound lookups.

What Interviewer Expects

  • Explaining how an index turns a table scan into a faster lookup (B-tree/hash)
  • Discussing write amplification as the cost of adding indexes
  • Mentioning composite, covering, and partial index strategies
  • Connecting indexing decisions to memory footprint and query patterns at scale

Common Mistakes

  • Assuming indexes are free and adding one for every column
  • Not matching composite index column order to real query filters
  • Ignoring that large indexes may not fit in memory, causing disk I/O
  • Forgetting indexing interacts with sharding: a local index does not help cross-shard queries

Best Answer (HR Friendly)

Database indexing is about building a structure, usually a B-tree, that lets the database jump straight to the rows a query needs instead of scanning the whole table. At scale it is a balancing act: indexes make reads fast but make every write slightly slower and use more memory, so I would pick indexes carefully based on the queries the application actually runs, and use techniques like covering or partial indexes to keep things efficient as the table grows into the millions or billions of rows.

Code Example

Composite and partial index design for an orders table
# Composite index matching a common query pattern:
#   SELECT * FROM orders WHERE customer_id = ? AND status = ? ORDER BY created_at DESC
index_composite:
  table: orders
  columns: [customer_id, status, created_at]
  type: btree

# Covering index so the query never touches the table heap
index_covering:
  table: orders
  columns: [customer_id, created_at]
  include: [total_amount, status]

# Partial index: only actively-processing orders are indexed,
# keeping index size small even with billions of historical rows
index_partial:
  table: orders
  columns: [status, updated_at]
  where: "status IN ('pending', 'processing')"

Follow-up Questions

  • How does a composite index’s column order affect which queries it can serve?
  • What is a covering index and when does it avoid a table lookup entirely?
  • How does sharding change your approach to indexing?
  • How would you detect and fix index fragmentation on a huge table?

MCQ Practice

1. What is the main cost an index adds to a table, especially with heavy write traffic?

Each index on a table must be updated on every write, so more indexes mean more write overhead.

2. What does a covering index let a query avoid?

A covering index includes every column the query needs, so the database can answer directly from the index.

3. Why is a partial index useful on a very large table?

Partial indexes cover only rows matching a condition (e.g., active records), reducing size versus indexing the whole table.

Flash Cards

What does a database index do?Lets queries locate rows without scanning the whole table, typically via a B-tree or hash structure.

Main downside of adding an index?Write amplification — every insert/update also has to update the index.

What is a covering index?An index containing every column a query needs, so it never touches the underlying table.

What is a partial index?An index built over only a subset of rows matching a condition, reducing size on huge tables.

1 / 4

Continue Learning