PostgreSQL Enterprise (50 Qs)•Part 2: Core Architecture & Implementation (Mid-Level)
How do GIN Indexes accelerate JSONB queries?
senior LevelMost AskedCode ExampleSystem Design
In Simple Words: A GIN (Generalized Inverted Index) decomposes a JSONB document into individual key-value paths:
- Creates an inverted index entry for every key and element.
- Index Definition:
sql
-- GIN with jsonb_path_ops (optimized for containment queries `@>`):
CREATE INDEX idx_users_meta_gin ON users USING GIN (metadata jsonb_path_ops);
-- Lightning-fast query:
SELECT * FROM users WHERE metadata @> '{"status": "premium"}';
- Can speed up JSON search by 100x to 1000x compared to full table scans.