Interview concept

What is database indexing?

A database index is an auxiliary structure, often a B-tree, that lets the database find rows without scanning the whole table, speeding reads at the cost of storage and slower writes.

Interview concept

How it works

How indexes help

They turn full-table scans into fast lookups for filters, joins, and sorts on indexed columns.

Costs

Each index adds storage and must be updated on writes, so unnecessary indexes slow inserts and updates.

Design choices

Composite and covering indexes match query patterns; column order and selectivity matter.

Interview concept

Worked example: speeding up an orders query

Query: the 20 most recent orders for customer 42 in a table of 50 million orders, filtered on customer_id and sorted by created_at.

Without an index

The database scans every row to find customer 42, then sorts the matches before returning 20.

Add a composite index

An index on (customer_id, created_at) lets the database seek straight to customer 42 and read entries already in date order, stopping after 20.

Column order matters

An index on (created_at, customer_id) helps far less, because the equality filter is not its leading column.

Pay the write cost

Every insert now updates the index too. Check the query plan, for example with EXPLAIN, to confirm the planner uses it.

Interview concept

Responsible use

Use any live AI assistant only where interview, workplace, school, and platform rules allow it. Do not use generated answers to misrepresent your skills, experience, or authorship.

FAQ

Common questions.

When should I add an index?

Add indexes for columns frequently used in filters, joins, and sorts, guided by query plans and selectivity.

What is the downside of indexing?

Indexes consume storage and slow writes, so avoid indexing columns that queries rarely use.