Pick the fix
Queries filter events with WHERE payload @> '{"type": "signup"}' on a jsonb column. Which index helps?
Hint 1 of 1
Which index type looks inside a value?
Choose an answer, then open Show answer below to check it.
Show answer
Answer: A CREATE INDEX ON events USING gin (payload jsonb_path_ops);
Correct. GIN indexes jsonb contents; jsonb_path_ops is compact and supports @>.
@> asks whether a document contains a fragment, which a B-tree can't answer. GIN indexes each key and value inside the document, and jsonb_path_ops makes a smaller index aimed at containment.
Why not the others?
- B
CREATE INDEX ON events (payload);. A B-tree on whole jsonb values only helps equality on the entire document. - C
CREATE INDEX ON events USING brin (payload);. BRIN summarises ranges of ordered values; jsonb containment isn't that. - D
CREATE INDEX ON events USING hash (payload);. Hash is equality on the whole value only.