Skip to content
Tech Interview Prep
Technical interview guide

Indexing & Query Performance

Why some queries are instant and others scan the whole table, and how an index (usually a B-tree) changes that.

Minutes to read
3
Practice MCQs
25
Interview Q&A
25
Edition
v4
Editorial status
Reviewed

Scope: SQL principles with PostgreSQL 18 examples; vendor-specific behavior must be verified.

Overview

Curated: · Written: · Reviewed:

Indexing & Query Performance

In one line

A B-tree is a sorted map from key to row location, so it serves equality, range and prefix predicates written against a bare indexed column. Reach for one when a selective predicate is slow, and only after reading the plan.

Key definitions

  • Sargable: a predicate the engine can turn into a range over the index's sorted keys.
  • Selectivity: the fraction of rows a predicate matches. An index wins only when that fraction is small.
  • Index-only scan: answered from the index alone, valid only where the visibility map marks heap pages all-visible.
  • Partial index: covers the rows matching its WHERE clause. The planner uses it only when the query predicate implies that clause.
  • Expression index: indexes a computed value. The query must use the same immutable expression.
  • Estimate/actual gap: the planner's row estimate against the real count from EXPLAIN ANALYZE. Check it first.

Formulas and complexity

seek cost=O(log⁡n) comparisons, touching about 3 to 4 pages at 8 kB pages\text{seek cost} = O(\log n) \text{ comparisons, touching about 3 to 4 pages at 8 kB pages} planner cost of a random page=4.0  (random_page_cost) vs 1.0  (seq_page_cost)\text{planner cost of a random page} = 4.0 \;(\text{random\_page\_cost}) \text{ vs } 1.0 \;(\text{seq\_page\_cost})

How it works

The planner checks whether the predicate is sargable on the bare column, then estimates how many rows it matches. A seek into the sorted keys beats a sequential scan only when the match is small. Each matching entry then costs a heap fetch unless the scan is index-only. Composite indexes follow the same rule: equality columns narrow the seek, and a range column ends it.

no yes no yes Query predicate Sargable on bare column? Sequential scan Selective enough? Index seek on sorted keys Heap fetch unless index-only

When to use and when not to

SituationIndex?Why
Equality or range on a selective columnYes, B-treeSeek reads few pages
LIKE '%term%'No B-treeNeeds trigram or full-text index
Boolean matching half the tableNoSequential scan wins
Rare value of a booleanPartial indexSmall, and seekable
Write-heavy table, rare readsMinimalEach index taxes every insert
Predicate matches most rowsNoSequential reads beat random lookups

Traps interviewers probe

  1. Blaming the planner. Fix: compare estimate to actual. The scan may have won the arithmetic, or the statistics are stale or correlated.
  2. Wrapping the column in a function or arithmetic. Fix: rewrite as a half-open range on the bare column, or create an expression index that matches the predicate exactly.
  3. Putting a range column first in a composite. Fix: equality columns first, then the range or sort column, for example (tenant_id, created_at).
  4. Building a plain CREATE INDEX on a live table. Fix: CREATE INDEX CONCURRENTLY. A plain build takes a SHARE lock that blocks writes for the whole build. After a failed concurrent build, check pg_index.indisvalid and drop the invalid index.
  5. Trusting an index-only scan on a churned table. Fix: vacuum. Heap fetches in the plan show the visibility map is stale, and a new index will not fix that.

Interview tips

Open with the method: "I'd read the plan first, check estimate against actual, confirm the predicate is sargable on that index, and only then decide whether an index is the fix and what it costs."

Expect these follow-ups:

  • Why didn't the planner use my index? Estimate gap, then selectivity, then sargability and operator class.
  • What does it cost on writes? One entry per insert, one per update of an indexed column, plus WAL, storage and vacuum.
  • What happens at 10x rows? Height grows by about one level, the selectivity crossover moves, and CONCURRENTLY becomes mandatory.
  • How do you verify the fix? Re-run EXPLAIN ANALYZE and compare actual rows and time, not only the node type.
  • How do you find unused indexes? pg_stat_user_indexes where idx_scan = 0, over a full workload cycle, ordered by size.

30-second outline: name the mechanism (sorted keys, seek versus scan), state the plan check (estimate versus actual, sargability), give the fix (composite order, expression index, or rewrite), and name the cost (write amplification and vacuum) with the verification step. Confirm the plan after the change, and report the actual rows and time it produced.

Feedback on this overview

Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.