JSON indexes let you query specific fields inside JSON columns without scanning the entire table or parsing every JSON document. AnalyticDB for MySQL supports two index types for JSON columns: JSON indexes for key/value lookups, and JSON array indexes for array membership queries using JSON_CONTAINS and JSON_OVERLAPS.
Version requirements
| Feature | Minimum version |
|---|---|
| JSON index (manual creation required) | V3.1.5.10 |
JSON index on a property key (column->'$.path') | V3.1.6.8 |
| JSON array index | V3.1.10.6 |
To view or update your cluster's minor version, go to the AnalyticDB for MySQL console, open the Cluster Information page, and find the Configuration Information section.
Choose the right index type
| Index type | Best for | Supported functions |
|---|---|---|
| JSON index | Exact key/value lookups on object properties | — |
| JSON array index | Membership queries on JSON arrays | JSON_CONTAINS, JSON_OVERLAPS |
Each JSON index or JSON array index covers a single JSON column. To index multiple columns, create a separate index for each.
Usage notes
JSON indexes and JSON array indexes can only be created on columns of the JSON type.
JSON array indexes support only numeric and string elements. Nested arrays and objects cannot be indexed.
For clusters running V3.1.5.10 or later, JSON indexes are not created automatically when you create a table. Create them manually using the syntax below.
For clusters running versions earlier than V3.1.5.10, JSON indexes are created automatically for JSON columns after table creation.
Index activation after creation (existing tables)
When you add a JSON index or JSON array index to an existing table, the activation behavior depends on the storage engine:
| Table type | Activation |
|---|---|
| Partitioned and non-partitioned XUANWU_V2 tables | Takes effect immediately. No BUILD job required. |
| Non-partitioned XUANWU tables | Takes effect after a BUILD job completes. |
| Partitioned XUANWU tables | Takes effect after a BUILD job run on the entire table completes. |
Create a JSON index
All examples in this section use a devices table that stores device telemetry as JSON:
CREATE TABLE devices(
id int,
info json,
...
)
DISTRIBUTED BY HASH(id);A typical info value looks like:
{"device_id": "d-001", "status": "online", "tags": ["prod", "us-west"]}Create a JSON index when creating a table
If you specify one or more columns to create an index when you create a table, AnalyticDB for MySQL does not automatically create indexes for other columns in the table.
Syntax
CREATE TABLE table_name(
column_name column_type,
{INDEX|KEY} [index_name](column_name|column_name->'$.json_path')
)
DISTRIBUTED BY HASH(column_name)The column_name->'$.json_path' syntax uses JSONPath notation: $ refers to the root of the JSON document, and .key accesses an object property. This parameter requires V3.1.6.8 or later.
Parameters
| Parameter | Description |
|---|---|
index_name | Name of the index. Must be unique within the table. |
column_name | Name of the JSON column to index. Creates an index on the entire JSON document. |
column_name->'$.json_path' | JSON column and a specific property key. Each index covers one property key. |
For other CREATE TABLE parameters, see CREATE TABLE.
If a JSON column already has an index, delete it before creating a property-key index on the same column.
Examples
Index the entire info JSON column:
CREATE TABLE devices(
id int,
info json,
index idx_info(info)
)
DISTRIBUTED BY HASH(id);Index only the status property key (requires V3.1.6.8 or later):
CREATE TABLE devices(
id int,
info json,
index idx_info_status(info->'$.status')
)
DISTRIBUTED BY HASH(id);Create a JSON index on an existing table
Syntax
ALTER TABLE db_name.table_name ADD {INDEX|KEY} [index_name] (column_name|column_name->'$.json_path',...)Parameters
| Parameter | Description |
|---|---|
db_name | Name of the database. |
table_name | Name of the table. |
index_name | Name of the index. Must be unique within the table. |
column_name | Name of the JSON column to index. |
column_name->'$.json_path' | JSON column and a specific property key. Requires V3.1.6.8 or later. |
If a JSON column already has an index, delete it before creating a property-key index on the same column.
Examples
Index the entire info JSON column:
ALTER TABLE devices ADD KEY idx_info(info);Index only the status property key:
ALTER TABLE devices ADD KEY idx_info_status(info->'$.status');Create a JSON array index
JSON array indexes accelerate membership queries on JSON arrays using JSON_CONTAINS and JSON_OVERLAPS. This index type requires V3.1.10.6 or later.
The $[*] JSONPath expression matches all elements in a JSON array.
JSON array indexes support only numeric and string array elements. Nested arrays and objects cannot be indexed.
Create a JSON array index when creating a table
Syntax
CREATE TABLE table_name(
column_name column_type,
{INDEX|KEY} [index_name](column_name->'$[*]')
)
DISTRIBUTED BY HASH(column_name);Parameters
| Parameter | Description |
|---|---|
index_name | Name of the index. Must be unique within the table. |
column_name->'$[*]' | JSON column to index. $[*] selects all array elements. For example, info->'$[*]' creates an array index on the info column. |
Example
Index the info array column in the devices table:
CREATE TABLE devices(
id int,
info json,
index idx_info_tags(info->'$[*]')
)
DISTRIBUTED BY HASH(id);Create a JSON array index on an existing table
Syntax
ALTER TABLE db_name.table_name ADD {INDEX|KEY} [index_name] (column_name->'$[*]')Parameters
| Parameter | Description |
|---|---|
db_name | Name of the database. |
table_name | Name of the table. |
index_name | Name of the index. Must be unique within the table. |
column_name->'$[*]' | JSON column to index. $[*] selects all array elements. |
Example
Add a JSON array index on the info column of the devices table:
ALTER TABLE devices ADD KEY idx_info_tags(info->'$[*]');Delete an index
Syntax
ALTER TABLE db_name.table_name DROP KEY index_nameTo find the index name, run:
SHOW INDEX FROM db_name.table_name;Examples
Delete the idx_info index from the devices table:
ALTER TABLE mydb.devices DROP KEY idx_info;Delete the idx_info_tags JSON array index from the devices table:
ALTER TABLE mydb.devices DROP KEY idx_info_tags;What's next
JSON: JSON data type reference for AnalyticDB for MySQL.
JSON functions: JSON functions supported by AnalyticDB for MySQL, including
JSON_CONTAINSandJSON_OVERLAPS.