Home > Database > Mysql Tutorial > How to Optimize Postgres JSONB Array Queries with GIN Indexes?

How to Optimize Postgres JSONB Array Queries with GIN Indexes?

Patricia Arquette
Release: 2025-01-06 13:25:40
Original
604 people have browsed it

How to Optimize Postgres JSONB Array Queries with GIN Indexes?

How to Use GIN Indexes with JSONB Arrays in Postgres

Problem:
When querying JSONB arrays in Postgres, a GIN index using events_gin_idx ON some_table USING GIN (events); may not optimize queries that search for specific array elements.

Cause:
The query is converting the array elements to JSON array elements using jsonb_array_elements(events) AS e, which disrupts the indexing.

Proper Indexing for Postgres 12 or Later:
To create a GIN index that supports queries with path expressions, use the jsonb_path_ops operator class:

CREATE INDEX locations_events_gin_idx ON locations USING gin (events jsonb_path_ops);
Copy after login

Query Syntax for Postgres 12 or Later:
Use the @? operator to test for array elements that match a condition:

SELECT *
FROM locations
WHERE events @? '$[*] ? (@.event_slug == "test_1")
                      ? (@.end_time.datetime() < "2014-10-13".datetime()'
Copy after login

Query Syntax for Older Versions of Postgres:
For versions prior to Postgres 12, you can use the @> operator with a partial JSON object:

SELECT *
FROM locations
WHERE events @> '[{ "event_slug": "test_1"}]';
Copy after login

Advanced Solution Using Materialized View:
If the basic indexing solutions are not performant enough, consider creating a materialized view:

CREATE MATERIALIZED VIEW loc_event AS
SELECT l.location_id, e.event_slug, e.end_time  -- start_time not needed
FROM locations l, jsonb_populate_recordset(null::event_type, l.events) e;

CREATE INDEX loc_event_idx ON loc_event (event_slug, end_time, location_id);
Copy after login

Materialized View Query:

SELECT *
FROM loc_event
WHERE event_slug = 'test_1'
AND end_time >= '2014-10-30 14:04:06 -0400'::timestamptz;
Copy after login

The above is the detailed content of How to Optimize Postgres JSONB Array Queries with GIN Indexes?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template