PostgreSQL JSON Index
Summary: in this tutorial, you will learn how to create a PostgreSQL JSON index for a JSONB column to improve query performance.
Introduction to PostgreSQL JSON index
JSONB (binary JSON) is a data type that allows you to store JSON data and query it efficiently.
When a JSONB column has a complex JSON structure, utilizing an index can significantly improve query performance.
PostgreSQL uses the GIN
index type for indexing a column with JSONB data type. GIN
stands for Generalized Inverted Index.
Note that you can utilize the GIN index for tsvector or array columns.
To create a GIN
index for a JSONB column, you can use the following CREATE INDEX
statement:
This statement creates a GIN
index on the jsonb_column
. This GIN
index is suitable for general-purpose queries on JSONB data.
When creating a GIN
index on a JSONB column, you can use a specific GIN
operator class.
The operator class determines how PostgreSQL builds the index and how it optimizes the queries on the indexed column.
For example, The following CREATE INDEX
statement creates a GIN
index on the jsonb_coumn
with jsonb_path_ops
operator class:
This index is optimized for the queries that use the @> (contains), ? (exists), and @@ JSONB operators. It can be useful for searches involving keys or values within JSONB documents.
The following table displays the GIN
operator classes:
Name | Indexable Operators |
---|---|
array_ops | && (anyarray,anyarray) |
@> (anyarray,anyarray) | |
<@ (anyarray,anyarray) | |
= (anyarray,anyarray) | |
jsonb_ops | @> (jsonb,jsonb) |
@? (jsonb,jsonpath) | |
@@ (jsonb,jsonpath) | |
? (jsonb,text) | |
`? | (jsonb,text[])` |
?& (jsonb,text[]) | |
jsonb_path_ops | @> (jsonb,jsonb) |
@? (jsonb,jsonpath) | |
@@ (jsonb,jsonpath) | |
tsvector_ops | @@ (tsvector,tsquery) |
@@@ (tsvector,tsquery) |
Note that if you don’t explicitly specify a GIN
operator class, the statement will use the jsonb_ops
operator by default, which is suitable for most cases.
Additionally, PostgreSQL allows you to create a GIN
index for a specific field in JSON documents as follows:
This index can improve the queries that involve searching values within the field_name
of JSON documents stored in the JSONB column (data).
PostgreSQL JSON index examples
We’ll use the tables in the sample database.
1) Setting up a sample table
First, create a new table called customer_json
that stores the customer information in JSON format:
Second, insert data from the customer
, address
, city
, and country
tables into the customer_json
table:
Third, retrieve the email of the customer whose first name is John
:
Output:
Finally, explain and analyze the above query:
Output:
The output indicates that PostgreSQL has to scan the entire customer_json
table to search for the customer.
To improve the performance of the query, you can create a GIN
index on the data column of the customer_json
table.
2) Creating an index on the JSONB column
First, create an index on the data
column of the customer_json
table:
Second, execute the query that searches for the customer whose first name is John
:
Output:
The query plan indicates that PostgreSQL uses the customer_json_index
to improve the performance.
This time, the execution time is significantly smaller 0.045ms
vs. 0.128
ms, about 2 – 3 times faster than a query without using the GIN
index.
3) Creating an index on the JSONB column with the GIN operator class
First, drop the customer_json_index
index:
Second, create a GIN
index on the data column of the customer_json
table with a GIN
operator class:
Third, explain the query that finds the customer whose first name is John
:
Output:
The query plan shows that the query does use the customer_json_index
for improved performance.
Finally, explain the query that searches for the customer where the value in the first_name
field within the data column is John:
Output:
In this plan, the query cannot fully utilize the GIN
index customer_json_index
. The reason is that the query does not use the JSONB operator (@
, @?
, @@
) that the jsonb_path_ops
operator class is optimized for.
4) Creating an index on a specific field of a JSONB column
First, drop the customer_json_index
index:
Second, create a GIN
index on the first_name
field of the customer_json
table using the GIN
operator class:
Third, explain the query that finds the rows where the “first_name
” field in the data
JSONB column contains the value "John"
:
Output:
The output indicates that the query uses the customer_json_index
index.
Summary
- Use the
GIN
index to create an index for a JSONB column of a table to improve query performance.