Skill 1.1.3
Design and implement JSON columns and indexes
Key Concepts
The json data type stores documents in an optimized binary format and validates them on insert, unlike nvarchar(max), which accepts any text unless a CHECK (ISJSON(col) = 1) constraint is added. JSON suits attributes that vary between rows, such as product specifications, while values that are filtered, joined or constrained in most queries belong in ordinary columns. A computed column such as JSON_VALUE(Specs, '$.weight' RETURNING decimal(6,2)) can expose one property for a regular B-tree index.
CREATE JSON INDEX indexes all paths of a json column, or only the paths listed in a FOR clause, and the optimizer uses it for JSON_VALUE equality, JSON_PATH_EXISTS and JSON_CONTAINS predicates. The table needs a clustered primary key, one JSON index is allowed per json column, listed paths cannot overlap, and OPTIMIZE_FOR_ARRAY_SEARCH = ON improves searches inside arrays. The index is built offline, and changing its paths means recreating it.