Given a table products with a details column of type JSONB, which query will use the GIN index created as CREATE INDEX idx_gin_details ON products USING GIN (details); to efficiently find products where the color is "red"?
CREATE TABLE products (id SERIAL PRIMARY KEY, details JSONB); CREATE INDEX idx_gin_details ON products USING GIN (details); -- Which query uses the GIN index efficiently?
GIN indexes on JSONB support containment operators like @>.
The @> operator checks if the left JSONB contains the right JSONB. This operator is supported by GIN indexes on JSONB columns, so query A uses the index efficiently. Queries using ->> or -> operators do not use the GIN index for filtering.
What does a GIN index on a JSONB column index internally to speed up queries?
Think about how GIN indexes support containment queries.
GIN indexes on JSONB break down the JSON document into individual keys and values and index them separately. This allows efficient containment queries using operators like @>.
Which of the following is the correct syntax to create a GIN index on a JSONB column data using the jsonb_path_ops operator class?
Operator classes are specified without parentheses.
The correct syntax is CREATE INDEX idx ON table USING GIN (column jsonb_path_ops); without parentheses after the operator class name.
You want to optimize queries that check if a JSONB column contains specific key-value pairs but do not need to query array elements inside JSON. Which GIN operator class should you choose?
One operator class is smaller and faster but supports fewer query types.
jsonb_path_ops indexes only the top-level keys and values, making it smaller and faster for containment queries that do not involve arrays. jsonb_ops supports more query types but is larger.
You created a GIN index on a JSONB column info using CREATE INDEX idx_info ON users USING GIN (info);. However, the query SELECT * FROM users WHERE info->>'status' = 'active'; does not use the index and is slow. Why?
Consider which JSONB operators are supported by GIN indexes.
GIN indexes on JSONB support containment operators like @>. The ->> operator extracts text and does not use the GIN index. To use the index, rewrite the query with @> operator.