Advanced Jsonb Indexing
title: Index JSONB Columns for Efficient Querying impact: MEDIUM impactDescription: 10-100x faster JSONB queries with proper indexing tags: jsonb, gin, indexes, json
Section titled “title: Index JSONB Columns for Efficient Querying impact: MEDIUM impactDescription: 10-100x faster JSONB queries with proper indexing tags: jsonb, gin, indexes, json”Index JSONB Columns for Efficient Querying
Section titled “Index JSONB Columns for Efficient Querying”JSONB queries without indexes scan the entire table. Use GIN indexes for containment queries.
Incorrect (no index on JSONB):
create table products ( id bigint primary key, attributes jsonb);
-- Full table scan for every queryselect * from products where attributes @> '{"color": "red"}';select * from products where attributes->>'brand' = 'Nike';Correct (GIN index for JSONB):
-- GIN index for containment operators (@>, ?, ?&, ?|)create index products_attrs_gin on products using gin (attributes);
-- Now containment queries use the indexselect * from products where attributes @> '{"color": "red"}';
-- For specific key lookups, use expression indexcreate index products_brand_idx on products ((attributes->>'brand'));select * from products where attributes->>'brand' = 'Nike';Choose the right operator class:
-- jsonb_ops (default): supports all operators, larger indexcreate index idx1 on products using gin (attributes);
-- jsonb_path_ops: only @> operator, but 2-3x smaller indexcreate index idx2 on products using gin (attributes jsonb_path_ops);Reference: JSONB Indexes