Interview's MateData Engineer
Interview's Mate

Data Engineer

SQL · Spark · Lakehouse · Streaming

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
Data Engineer topics
Lesson 32/ 160
02. Advanced SQL & Relational Databases

B-Tree, Hash, GIN, GiST & BRIN Indexes

Match index types to equality, range, full-text, geospatial, and append-only table access patterns.

AdvancedB-Tree · GIN/GiST · BRIN3 quiz questions
Practice Generative AI Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

In plain words

Equality, range, containment, or spatial. Next, match an index access method. Finally, plan use, size, update cost, and read time.

Think of it like…

A B-Tree is a sorted catalog, hash is a direct locker map, GIN is an inverted word index, GiST is a shape-aware organizer, and BRIN is a row of box labels for sequential shelves.

Where you’ve seen it

CREATE INDEX ON orders (tenant_id, created_at DESC) WHERE status='open' serves tenant status queries compactly; GIN serves JSONB @> while BRIN on append-only event_time stores tiny min/max ranges per block.

Words you’ll hear

B-Tree
A balanced ordered index suited to equality, prefix, range, and ordering operations with logarithmic lookup depth.
GIN/GiST
Indexes for multi-component searchable values and extensible tree classes, commonly JSON/full-text arrays and spatial/range operators.
BRIN
A block range index storing min/max summaries that is tiny and fast when physical table order matches the query range.
Operational boundary
Each index adds write amplification, WAL, and storage; a GIN index can be expensive to maintain, while BRIN gives little pruning if rows are physically scattered by the indexed key. Start from actual predicates and EXPLAIN, use composite B-Trees for selective leading prefixes, specialized indexes only for their operators, and BRIN only where table physical order correlates strongly with the indexed column.

Visual diagram

Loading interactive diagram

Theory & deep dive

B-Tree: the working mechanism

B-Tree supports equality and ordered ranges, hash targets equality, GIN indexes searchable composite values, GiST supports many operator classes, and BRIN summarizes ordered ranges for very large append-only tables.

Equality, range, containment, or spatial. Match an index access method, then row ordering and correlation; this exposes the state governed by BRIN.

B-Tree, Hash, GIN, GiST & BRIN Indexes in one concrete design

CREATE INDEX ON orders (tenant_id, created_at DESC) WHERE status='open' serves tenant status queries compactly; GIN serves JSONB @> while BRIN on append-only event_time stores tiny min/max ranges per block.

Key takeaway: B-Tree supports equality and ordered ranges, hash targets equality, GIN indexes searchable composite values, GiST supports many operator classes, and BRIN summarizes ordered ranges for very large append-only tables

B-Tree, Hash, GIN, GiST & BRIN Indexes: failure mode and production response

Each index adds write amplification, WAL, and storage; a GIN index can be expensive to maintain, while BRIN gives little pruning if rows are physically scattered by the indexed key.

Start from actual predicates and EXPLAIN, use composite B-Trees for selective leading prefixes, specialized indexes only for their operators, and BRIN only where table physical order correlates strongly with the indexed column. Track B-Tree prefix as "only a leftmost prefix is independently searchable", and make BRIN correlation visible to the dataset owner.

Key takeaway: Start from actual predicates and EXPLAIN, use composite B-Trees for selective leading prefixes, specialized indexes only for their operators, and BRIN only where table physical order correlates strongly with the indexed column.

How it works, step by step

Step 1 of 4
B-Tree

1. Classify predicate

Equality, range, containment, or spatial. B-Tree supports equality and ordered ranges, hash targets equality, GIN indexes searchable composite values, GiST supports many operator classes, and BRIN summarizes ordered ranges for very large append-only tables.

equality, range, containment, or spatial
Key numbers at this step
B-Tree prefix
only a leftmost prefix is independently searchable
GIN/GiST
Indexes for multi-component searchable values and extensible tree classes, commonly JSON/full-text arrays and spatial/range operators.

When to use it & trade-offs

What it is

B-Tree supports equality and ordered ranges, hash targets equality, GIN indexes searchable composite values, GiST supports many operator classes, and BRIN summarizes ordered ranges for very large append-only tables. CREATE INDEX ON orders (tenant_id, created_at DESC) WHERE status='open' serves tenant status queries compactly; GIN serves JSONB @> while BRIN on append-only event_time stores tiny min/max ranges per block.

Why it matters: Each index adds write amplification, WAL, and storage; a GIN index can be expensive to maintain, while BRIN gives little pruning if rows are physically scattered by the indexed key. Start from actual predicates and EXPLAIN, use composite B-Trees for selective leading prefixes, specialized indexes only for their operators, and BRIN only where table physical order correlates strongly with the indexed column.

When to reach for it

  • Match index types to equality, range, full-text, geospatial, and append-only table access patterns.
  • Choose B-Tree because it provides this capability: A balanced ordered index suited to equality, prefix, range, and ordering operations with logarithmic lookup depth.
  • Keep evidence for B-Tree prefix at "only a leftmost prefix is independently searchable" so the last production stage-Measure - plan use, size, update cost, and read time-remains testable.

When to avoid or be careful

  • Each index adds write amplification, WAL, and storage; a GIN index can be expensive to maintain, while BRIN gives little pruning if rows are physically scattered by the indexed key.
  • Do not conflate GIN/GiST with B-Tree. The second concept contributes different behavior: Indexes for multi-component searchable values and extensible tree classes, commonly JSON/full-text arrays and spatial/range operators.
  • Release only after this guardrail is in place: Start from actual predicates and EXPLAIN, use composite B-Trees for selective leading prefixes, specialized indexes only for their operators, and BRIN only where table physical order correlates strongly with the indexed column.

B-Tree / GIN/GiST / BRIN - roles, limits, and operating cost

B-Tree
Best for: A balanced ordered index suited to equality, prefix, range, and ordering operations with logarithmic lookup depth.

Strengths: A balanced ordered index suited to equality, prefix, range, and ordering operations with logarithmic lookup depth. B-Tree supports equality and ordered ranges, hash targets equality, GIN indexes searchable composite values, GiST supports many operator classes, and BRIN summarizes ordered ranges for very large append-only tables.

Watch out: Each index adds write amplification, WAL, and storage; a GIN index can be expensive to maintain, while BRIN gives little pruning if rows are physically scattered by the indexed key.

GIN/GiST
Best for: Indexes for multi-component searchable values and extensible tree classes, commonly JSON/full-text arrays and spatial/range operators.

Strengths: Indexes for multi-component searchable values and extensible tree classes, commonly JSON/full-text arrays and spatial/range operators. In this workflow it supports: Model locality - row ordering and correlation.

Watch out: The operating cost is real: high table-value order makes range summaries useful.

BRIN
Best for: A block range index storing min/max summaries that is tiny and fast when physical table order matches the query range.

Strengths: A block range index storing min/max summaries that is tiny and fast when physical table order matches the query range. Its output can be checked with Index cost.

Watch out: Operating guardrail: Start from actual predicates and EXPLAIN, use composite B-Trees for selective leading prefixes, specialized indexes only for their operators, and BRIN only where table physical order correlates strongly with the indexed column.

Interview signals & numbers to know

When this topic comes up

  • “Where is the failure boundary for B-Tree, and how would B-Tree prefix reveal it?”
  • “Why is GIN/GiST distinct from this behavior: a block range index storing min/max summaries that is tiny and fast when physical table order matches the query range?”
  • Production clue: Each index adds write amplification, WAL, and storage; a GIN index can be expensive to maintain, while BRIN gives little pruning if rows are physically scattered by the indexed key.
  • Design probe: Start from actual predicates and EXPLAIN, use composite B-Trees for selective leading prefixes, specialized indexes only for their operators, and BRIN only where table physical order correlates strongly with the indexed column.

Benchmarks & rules of thumb

B-Tree prefix -
only a leftmost prefix is independently searchable
BRIN correlation -
high table-value order makes range summaries useful
Index cost -
measure bytes written and maintenance on every UPDATE

Common mistakes & follow-ups

Common interview mistakes

  • Each index adds write amplification, WAL, and storage; a GIN index can be expensive to maintain, while BRIN gives little pruning if rows are physically scattered by the indexed key.
  • Reporting success from a job exit code without checking B-Tree prefix and BRIN correlation.
  • Treating this example as complete without replay, rollback, or access boundaries: CREATE INDEX ON orders (tenant_id, created_at DESC) WHERE status='open' serves tenant status queries compactly; GIN serves JSONB @> while BRIN on append-only event_time stores tiny min/max ranges per block.

Likely interviewer follow-ups

  • How would you prove this contract instead of only checking that the command ran: A balanced ordered index suited to equality, prefix, range, and ordering operations with logarithmic lookup depth.
  • What does the recovery plan do when this occurs? Each index adds write amplification, WAL, and storage; a GIN index can be expensive to maintain, while BRIN gives little pruning if rows are physically scattered by the indexed key.
  • When is B-Tree insufficient for BRIN correlation, and what does adding GIN/GiST cost in B-Tree prefix?
Interview tip: Lead with the data path and failure boundary: B-Tree supports equality and ordered ranges, hash targets equality, GIN indexes searchable composite values, GiST supports many operator classes, and BRIN summarizes ordered ranges for very large append-only tables. Then make the answer operational: Start from actual predicates and EXPLAIN, use composite B-Trees for selective leading prefixes, specialized indexes only for their operators, and BRIN only where table physical order correlates strongly with the indexed column.

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply B-Tree, Hash, GIN, GiST & BRIN Indexes with data engineer interview Q&A and hands-on practice.

Open Data Engineer Q&A →Machine Learning Q&A
Previous topic
Stored Procedures, User-Defined Functions (UDFs) & Triggers
Next topic
Reading EXPLAIN ANALYZE & Query Execution Plans