Hash, GIN, GiST and BRIN: Choosing the Right Index Type
Beyond B-trees: when to use hash, GIN, GiST, SP-GiST and BRIN indexes in PostgreSQL, plus MySQL full-text and spatial indexes, with examples.
Published November 27, 2026
B-tree is the right default for most columns, but it cannot efficiently answer "which documents contain this word", "which JSON objects have this key", "which shops are within 2 km" or "rows from last Tuesday in a 5 TB append-only log". Other index types exist precisely for those questions. PostgreSQL has the richest selection, so it is the main focus here, with MySQL equivalents noted.
The concept
Each index type is a different data structure optimised for different operators. Hash indexes map values to buckets for pure equality lookups. GIN (Generalized Inverted Index) maps each element inside a value (a word, an array item, a JSON key) to the rows containing it, like the index at the back of a book. GiST (Generalized Search Tree) and SP-GiST support overlapping and spatial data such as ranges, geometries and nearest-neighbour searches.
BRIN (Block Range Index) is the odd one out: instead of indexing every row, it stores a summary (min and max) for each range of table pages. On huge tables where values correlate with physical position, such as timestamps in an append-only log, a BRIN index can be thousands of times smaller than a B-tree and still skip almost all irrelevant pages.
PostgreSQL index types at a glance
| Type | Best for | Operators | Example |
|---|---|---|---|
| B-tree | Most scalar columns | = < > BETWEEN, ORDER BY, LIKE 'abc%' | email, created_at |
| Hash | Equality only | = | Long opaque tokens |
| GIN | Many values per row | @> ? && @@ | jsonb, arrays, full-text tsvector, trigrams |
| GiST | Overlap, geometry, nearest neighbour | && <-> @> | PostGIS, tsrange, exclusion constraints |
| SP-GiST | Space-partitioned data | Inclusion, prefix | IP addresses (inet), quadtrees |
| BRIN | Huge, naturally ordered tables | = < > on correlated columns | Logs, IoT time series |
How GIN works
BRIN: tiny indexes for huge tables
MySQL equivalents
InnoDB supports B-tree indexes for regular columns, FULLTEXT indexes for word search with MATCH ... AGAINST, SPATIAL (R-tree) indexes for geometry types, and multi-valued indexes on JSON arrays (MySQL 8.0.17+). The MEMORY engine supports hash indexes, and InnoDB maintains an internal adaptive hash index automatically.
Practical examples
JSONB containment with GIN
jsonb_path_ops is smaller and faster for @> queries.
CREATE INDEX idx_products_attrs ON products USING gin (attrs jsonb_path_ops);
SELECT id, name FROM products WHERE attrs @> '{"color": "red", "size": "M"}';Full-text search
A generated tsvector column plus GIN makes word search fast.
ALTER TABLE articles ADD COLUMN search tsvector
GENERATED ALWAYS AS (to_tsvector('english', title || ' ' || body)) STORED;
CREATE INDEX idx_articles_search ON articles USING gin (search);
SELECT title FROM articles WHERE search @@ websearch_to_tsquery('english', 'kernel module -rust');Fuzzy LIKE '%term%' with trigrams
pg_trgm lets GIN accelerate infix searches that B-trees cannot.
CREATE EXTENSION IF NOT EXISTS pg_trgm;
CREATE INDEX idx_users_name_trgm ON users USING gin (name gin_trgm_ops);
SELECT * FROM users WHERE name ILIKE '%sharm%';BRIN for time series
Works because rows are inserted in time order.
CREATE INDEX idx_events_created_brin ON events USING brin (created_at) WITH (pages_per_range = 64);
SELECT count(*) FROM events WHERE created_at >= now() - interval '1 day';Common mistakes
- Using a B-tree on a jsonb column and expecting containment queries to use it.
- BRIN on a column with no physical correlation (random UUIDs), which skips nothing.
- GIN on very write-heavy tables without considering fastupdate and pending-list behaviour.
- Reaching for an external search engine when PostgreSQL full-text search would be enough.
- Hash indexes on PostgreSQL versions before 10, where they were not crash-safe.
Key takeaways
- B-tree for scalars; GIN for multi-valued data like jsonb, arrays and text search.
- GiST for geometry, ranges and nearest-neighbour queries.
- BRIN gives tiny indexes on huge, naturally ordered tables.
- Match the index type to the operator your query uses.
FAQ
Are hash indexes faster than B-trees for equality?
Slightly, and they can be smaller for long keys, but they do not support ranges or ordering. Most teams stick with B-trees.
Can I index only part of a JSON document?
Yes, with an expression index such as CREATE INDEX ON orders ((data->>'customer_id')).
Does GIN slow down inserts?
Yes, more than B-tree, because one row can create many index entries. The fastupdate option batches them at the cost of slightly slower searches until the pending list is merged.