Fixify Iconfixify Journal Back to Journal
Performance TuningDatabase Internals

A Deep Dive Into Database Indexing: B-Trees, Hash Key Indexes, and GIN Tables

June 09, 2026 12 min read 1,610 words

Slow queries are the bane of any application scaling process. While adding memory might delay the problem, lasting resolution requires mastering how relational databases store and traverse disk blocks. This guide explores the computer science behind B-Trees, Hash Indexes, Generalized Inverted Indexes (GIN), and the read/write write trade-offs of query optimization.

1. The Naive Search: Sequential Scans and Disk Overhead

By default, without an index, a relational database (like PostgreSQL or MySQL) must scan every single row on disk to resolve a query like SELECT * FROM users WHERE email = 'test@domain.com'. This is known as a **Sequential Scan**.

On a table containing 10,000 rows, this process takes milliseconds. But for tables with multi-million rows, sequentially reading gigabytes of data from disk into system memory creates massive, blocking CPU queues, causing immediate connections timeouts.

2. The Workhorse: How B-Tree Indexes Work

To bypass sequential scans, databases default to the **B-Tree (Balanced Tree)** data structure. A B-Tree organizes keys hierarchically, maintaining balance so that search operations resolve in logarithmic time (O(log N)).

Rather than scanning every page, the database traverses from the root node, down through branches, directly to the leaf node referencing the target disk block. For a table with 1,000,000 rows, a B-Tree can locate a single record in fewer than 4 disk reads.

3. Specialized Structures: Hash Indexes and GIN Tables

While B-Trees handle range queries (e.g. WHERE created_at > ...) perfectly, specialized data models require alternative indexing structures:

  • Hash Indexes: Optimize exact-match queries with constant time lookup complexity (O(1)), but are unable to support range operations.
  • Generalized Inverted Indexes (GIN): Essential for multi-valued data fields like array columns or JSONB tables. A GIN index maps nested keys to their containing records, allowing rapid, sub-millisecond filtering of JSON blobs.

Beware of the Write Penalty

Every index is an additional data structure maintained on disk. While indexes make read queries incredibly fast, they add overhead to write queries (INSERT, UPDATE, DELETE) because the database must update the index on every change. Refrain from over-indexing columns with high write frequency.

4. Conclusion: High Performance Begins with indexing

Mastering the underlying mechanics of database indexes allows developers to design highly performant schemas capable of resolving queries under heavy production workloads. Evaluate query plans regularly using the EXPLAIN ANALYZE command to identify blockages, structure indexes thoughtfully, and scale database systems with confidence.

Fixify Icon

Written by the fixify Systems Team

Advanced Database Internals Advisory

Back to Articles list