Indexing JSON Documents
This lesson explains CREATE JSON INDEX, the predicates it supports and its requirements, compared with indexes on computed columns.
Learning outcomes
After completing this lesson, you will be able to:
- Describe the options and requirements of a JSON index.
- Identify which JSON predicates can use a JSON index.
- Choose between a JSON index and a computed column index.
Creating a JSON index
CREATE JSON INDEX indexes the keys and values of a json column, either the whole document (the default path $) or only the paths listed in the FOR clause. Each listed path indexes everything below it, so paths cannot overlap: $.a and $.a.b in the same index raise an error. OPTIMIZE_FOR_ARRAY_SEARCH = ON improves searches inside arrays, such as tag lists.
CREATE JSON INDEX JX_Product_Specs
ON Catalog.Product (Specs)
FOR ('$.material', '$.season', '$.tags')
WITH (OPTIMIZE_FOR_ARRAY_SEARCH = ON);Queries that use the index
The optimizer uses a JSON index for equality predicates built from JSON functions: JSON_VALUE(Specs, '$.material') = 'nylon', also with a RETURNING type such as RETURNING int; JSON_PATH_EXISTS(Specs, '$.season') = 1; and JSON_CONTAINS(Specs, 'winter', '$.tags[*]') = 1. LIKE and IS NULL predicates on JSON values are not currently supported by the index, and JSON indexes cannot be used in index hints.
SELECT ProductID, Name
FROM Catalog.Product
WHERE JSON_CONTAINS(Specs, 'winter', '$.tags[*]') = 1
AND JSON_VALUE(Specs, '$.material') = 'nylon';Can the JSON index help?
Requirements and limits
The table must have a clustered primary key (the clustering key is limited to 31 columns and under 128 bytes). Each json column can have only one JSON index, and a table can have up to 249 JSON indexes across columns. JSON indexes cannot be created on computed json columns, views, table variables or memory-optimized tables. They are built offline, so creating one locks the table for the duration, and changing the indexed paths means recreating the index with DROP_EXISTING = ON. The following comparison helps choose between a JSON index and a computed column index.
JSON index requirements
Searching tags
Products have a json Specs column with a tags array, and the storefront filters products by any of about 200 tags. Queries scan the 2-million-row table. The table has a clustered primary key. What should the developer do?
Create a JSON index on Specs for the path $.tags with OPTIMIZE_FOR_ARRAY_SEARCH = ON, and filter with JSON_CONTAINS(Specs, @tag, '$.tags[*]') = 1. A computed column can expose only one scalar value, so it cannot index membership in an array, and a LIKE predicate on the JSON text cannot use either kind of index.
Can one JSON index list both $.a and $.a.b?
Show answer
No. Paths cannot overlap, because $.a already indexes everything below it.
Exam Tips
Remember for the exam
- A JSON index needs a json column and a clustered primary key.
- One JSON index per json column; listed paths cannot overlap.
- Supported: JSON_VALUE equality, JSON_PATH_EXISTS and JSON_CONTAINS; not LIKE.
- Array membership searches: OPTIMIZE_FOR_ARRAY_SEARCH = ON.