|
1 | 1 | ---
|
2 |
| -title: "Index JSON data" |
| 2 | +title: "Index JSON Data" |
3 | 3 | description: "Index JSON data"
|
4 | 4 | author: WilliamDAssafMSFT
|
5 | 5 | ms.author: wiassaf
|
6 |
| -ms.reviewer: jroth, jovanpop |
7 |
| -ms.date: 08/20/2024 |
| 6 | +ms.reviewer: jroth, jovanpop, randolphwest |
| 7 | +ms.date: 06/19/2025 |
8 | 8 | ms.service: sql
|
| 9 | +ms.topic: how-to |
9 | 10 | ms.custom:
|
10 | 11 | - build-2024
|
11 |
| -ms.topic: how-to |
12 | 12 | helpviewer_keywords:
|
13 | 13 | - "JSON, indexing JSON data"
|
14 | 14 | - "indexing JSON data"
|
15 |
| -monikerRange: "=azuresqldb-current||>=sql-server-2016||>=sql-server-linux-2017||=azuresqldb-mi-current" |
| 15 | +monikerRange: "=azuresqldb-current || >=sql-server-2016 || >=sql-server-linux-2017 || =azuresqldb-mi-current" |
16 | 16 | ---
|
17 | 17 | # Index JSON data
|
| 18 | + |
18 | 19 | [!INCLUDE [SQL Server Azure SQL Database Azure SQL Managed Instance](../../includes/applies-to-version/sqlserver2016-asdb-asdbmi.md)]
|
19 | 20 |
|
20 |
| -You can optimize your queries over JSON documents using standard indexes. SQL Server does not have custom JSON indexes. |
| 21 | +You can optimize your queries over JSON documents using standard indexes. |
21 | 22 |
|
22 |
| -- Currently, in SQL Server **json** is not a built-in data type. |
23 |
| -- The [JSON data type](../../t-sql/data-types/json-data-type.md) is currently in preview for Azure SQL Database and Azure SQL Managed Instance (configured with the [**Always-up-to-date** update policy](/azure/azure-sql/managed-instance/update-policy#always-up-to-date-update-policy)). |
| 23 | +The [JSON data type](../../t-sql/data-types/json-data-type.md): |
| 24 | + |
| 25 | +- is generally available for Azure SQL Database and Azure SQL Managed Instance configured with the **[Always-up-to-date update policy](/azure/azure-sql/managed-instance/update-policy#always-up-to-date-update-policy)**. |
| 26 | +- is in preview for [!INCLUDE [sssql25-md](../../includes/sssql25-md.md)]. |
| 27 | + |
| 28 | +> [!NOTE] |
| 29 | +> In [!INCLUDE [sssql25-md](../../includes/sssql25-md.md)], you can use the [CREATE JSON INDEX](../../t-sql/statements/create-json-index-transact-sql.md) feature. |
24 | 30 |
|
25 | 31 | Indexes work the same way on JSON data in **varchar**/**nvarchar** or the [native **json** data type](../../t-sql/data-types/json-data-type.md).
|
26 | 32 |
|
27 |
| -Database indexes improve the performance of filter and sort operations. Without indexes, SQL Server has to perform a full table scan every time you query data. |
28 |
| - |
| 33 | +Database indexes improve the performance of filter and sort operations. Without indexes, SQL Server has to perform a full table scan every time you query data. |
| 34 | + |
29 | 35 | ## Index JSON properties by using computed columns
|
30 |
| -When you store JSON data in SQL Server, typically you want to filter or sort query results by one or more *properties* of the JSON documents. |
| 36 | + |
| 37 | +When you store JSON data in SQL Server, typically you want to filter or sort query results by one or more *properties* of the JSON documents. |
31 | 38 |
|
32 | 39 | ### Example
|
33 |
| -In this example, assume that the `AdventureWorks.SalesOrderHeader` table has an `Info` column that contains various information in JSON format about sales orders. For example, it contains unstructured data about customer, sales person, shipping and billing addresses, and so forth. You could use values from the `Info` column to filter sales orders for a customer. |
34 | 40 |
|
35 |
| -By default, the column `Info` used does not exist, it can be created in the `AdventureWorks` database with the following code. The following examples do not apply to the `AdventureWorksLT` series of sample databases. |
| 41 | +In this example, assume that the `AdventureWorks.SalesOrderHeader` table has an `Info` column that contains various information in JSON format about sales orders. For example, it contains unstructured data about customer, sales person, shipping and billing addresses, and so forth. You could use values from the `Info` column to filter sales orders for a customer. |
| 42 | + |
| 43 | +By default, the column `Info` used doesn't exist, it can be created in the `AdventureWorks` database with the following code. The following examples don't apply to the `AdventureWorksLT` series of sample databases. |
36 | 44 |
|
37 |
| -```sql |
38 |
| -IF NOT EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID('[Sales].[SalesOrderHeader]') AND name = 'Info') |
39 |
| - ALTER TABLE [Sales].[SalesOrderHeader] ADD [Info] NVARCHAR(MAX) NULL |
| 45 | +```sql |
| 46 | +IF NOT EXISTS (SELECT * |
| 47 | + FROM sys.columns |
| 48 | + WHERE object_id = OBJECT_ID('[Sales].[SalesOrderHeader]') |
| 49 | + AND name = 'Info') |
| 50 | + ALTER TABLE [Sales].[SalesOrderHeader] |
| 51 | + ADD [Info] NVARCHAR (MAX) NULL; |
40 | 52 | GO
|
41 |
| -UPDATE h |
| 53 | + |
| 54 | +UPDATE h |
42 | 55 | SET [Info] =
|
43 | 56 | (
|
44 |
| - SELECT [Customer.Name] = concat(p.FirstName, N' ', p.LastName), |
45 |
| - [Customer.ID] = p.BusinessEntityID, |
46 |
| - [Customer.Type] = p.[PersonType], |
47 |
| - [Order.ID] = soh.SalesOrderID, |
48 |
| - [Order.Number] = soh.SalesOrderNumber, |
| 57 | + SELECT [Customer.Name] = concat(p.FirstName, N' ', p.LastName), |
| 58 | + [Customer.ID] = p.BusinessEntityID, |
| 59 | + [Customer.Type] = p.[PersonType], |
| 60 | + [Order.ID] = soh.SalesOrderID, |
| 61 | + [Order.Number] = soh.SalesOrderNumber, |
49 | 62 | [Order.CreationData] = soh.OrderDate,
|
50 | 63 | [Order.TotalDue] = soh.TotalDue
|
51 | 64 | FROM [Sales].SalesOrderHeader AS soh
|
52 |
| - INNER JOIN [Sales].[Customer] AS c ON c.CustomerID = soh.CustomerID |
53 |
| - INNER JOIN [Person].[Person] AS p ON p.BusinessEntityID = c.CustomerID |
54 |
| - WHERE soh.SalesOrderID = h.SalesOrderID FOR JSON PATH, WITHOUT_ARRAY_WRAPPER |
| 65 | + INNER JOIN [Sales].[Customer] AS c |
| 66 | + ON c.CustomerID = soh.CustomerID |
| 67 | + INNER JOIN [Person].[Person] AS p |
| 68 | + ON p.BusinessEntityID = c.CustomerID |
| 69 | + WHERE soh.SalesOrderID = h.SalesOrderID |
| 70 | + FOR JSON PATH, WITHOUT_ARRAY_WRAPPER |
55 | 71 | )
|
56 |
| -FROM [Sales].SalesOrderHeader AS h; |
57 |
| -``` |
| 72 | +FROM [Sales].SalesOrderHeader AS h; |
| 73 | +``` |
58 | 74 |
|
59 | 75 | ### Query to optimize
|
60 |
| -Here's an example of the type of query that you want to optimize by using an index. |
61 |
| - |
62 |
| -```sql |
| 76 | + |
| 77 | +Here's an example of the type of query that you want to optimize by using an index. |
| 78 | + |
| 79 | +```sql |
63 | 80 | SELECT SalesOrderNumber,
|
64 |
| - OrderDate, |
65 |
| - JSON_VALUE(Info, '$.Customer.Name') AS CustomerName |
| 81 | + OrderDate, |
| 82 | + JSON_VALUE(Info, '$.Customer.Name') AS CustomerName |
66 | 83 | FROM Sales.SalesOrderHeader
|
67 |
| -WHERE JSON_VALUE(Info, '$.Customer.Name') = N'Aaron Campbell' |
68 |
| -``` |
| 84 | +WHERE JSON_VALUE(Info, '$.Customer.Name') = N'Aaron Campbell'; |
| 85 | +``` |
69 | 86 |
|
70 | 87 | ### Example index
|
| 88 | + |
71 | 89 | If you want to speed up your filters or `ORDER BY` clauses over a property in a JSON document, you can use the same indexes that you're already using on other columns. However, you can't *directly* reference properties in the JSON documents.
|
72 | 90 |
|
73 | 91 | 1. First, create a "virtual column" that returns the values that you want to use for filtering.
|
74 |
| -1. Then, create an index on that virtual column. |
75 |
| - |
76 |
| -The following example creates a computed column that can be used for indexing. Then it creates an index on the new computed column. This example creates a column that exposes the customer name, which is stored in the `$.Customer.Name` path in the JSON data. |
77 |
| - |
78 |
| -```sql |
| 92 | +1. Then, create an index on that virtual column. |
| 93 | + |
| 94 | +The following example creates a computed column that can be used for indexing. Then it creates an index on the new computed column. This example creates a column that exposes the customer name, which is stored in the `$.Customer.Name` path in the JSON data. |
| 95 | + |
| 96 | +```sql |
79 | 97 | ALTER TABLE Sales.SalesOrderHeader
|
80 |
| -ADD vCustomerName AS JSON_VALUE(Info,'$.Customer.Name') |
| 98 | + ADD vCustomerName AS JSON_VALUE(Info, '$.Customer.Name'); |
81 | 99 |
|
82 | 100 | CREATE INDEX idx_soh_json_CustomerName
|
83 |
| -ON Sales.SalesOrderHeader(vCustomerName) |
84 |
| -``` |
| 101 | + ON Sales.SalesOrderHeader(vCustomerName); |
| 102 | +``` |
85 | 103 |
|
86 |
| -This statement will return the following warning: |
| 104 | +This statement returns the following warning: |
87 | 105 |
|
88 | 106 | ```output
|
89 | 107 | Warning! The maximum key length for a nonclustered index is 1700 bytes.
|
90 | 108 | The index 'vCustomerName' has maximum length of 8000 bytes.
|
91 | 109 | For some combination of large values, the insert/update operation will fail.
|
92 | 110 | ```
|
93 | 111 |
|
94 |
| -The `JSON_VALUE` function might return text values up to 8000 bytes (for example, as the **nvarchar(4000)** type). However, the values that are longer than 1700 bytes cannot be indexed. If you try to enter the value in the indexed computed column that is longer than 1700 bytes, the data manipulation language (DML) operation will fail. |
| 112 | +The `JSON_VALUE` function might return text values up to 8000 bytes (for example, as the **nvarchar(4000)** type). However, the values that are longer than 1700 bytes can't be indexed. If you try to enter the value in the indexed computed column that is longer than 1700 bytes, the data manipulation language (DML) operation fails. |
95 | 113 |
|
96 | 114 | For better performance, try to cast the value that you expose using a computed column into the smallest applicable data type. Use **int** and **datetime2** types instead of string types.
|
97 | 115 |
|
98 | 116 | ### More info about the computed column
|
99 |
| -A computed column is not persisted. A computer column computed only when the index needs to be rebuilt. It does not occupy additional space in the table. |
100 |
| - |
101 |
| -It's important that you create the computed column with the same expression that you plan to use in your queries - in this example, the expression is `JSON_VALUE(Info, '$.Customer.Name')`. |
102 |
| - |
| 117 | + |
| 118 | +A computed column isn't persisted. A computed column is only computed when the index needs to be rebuilt. It doesn't occupy additional space in the table. |
| 119 | + |
| 120 | +It's important that you create the computed column with the same expression that you plan to use in your queries - in this example, the expression is `JSON_VALUE(Info, '$.Customer.Name')`. |
| 121 | + |
103 | 122 | You don't have to rewrite your queries. If you use expressions with the `JSON_VALUE` function, as shown in the preceding example query, SQL Server sees that there's an equivalent computed column with the same expression and applies an index if possible.
|
104 | 123 |
|
105 | 124 | ### Execution plan for this example
|
106 |
| -Here's the execution plan for the query in this example. |
107 |
| - |
| 125 | + |
| 126 | +Here's the execution plan for the query in this example. |
| 127 | + |
108 | 128 | :::image type="content" source="media/index-json-data/json-index-seek.png" alt-text="Screenshot showing the execution plan for this example.":::
|
109 |
| - |
110 |
| -Instead of a full table scan, SQL Server uses an index seek into the nonclustered index and finds the rows that satisfy the specified conditions. Then it uses a key lookup in the `SalesOrderHeader` table to fetch the other columns that are referenced in the query - in this example, `SalesOrderNumber` and `OrderDate`. |
| 129 | + |
| 130 | +Instead of a full table scan, SQL Server uses an index seek into the nonclustered index and finds the rows that satisfy the specified conditions. Then it uses a key lookup in the `SalesOrderHeader` table to fetch the other columns that are referenced in the query - in this example, `SalesOrderNumber` and `OrderDate`. |
111 | 131 |
|
112 | 132 | ### Optimize the index further with included columns
|
113 |
| -If you add required columns in the index, you can avoid this additional lookup in the table. You can add these columns as standard included columns, as shown in the following example, which extends the preceding `CREATE INDEX` example. |
114 |
| - |
115 |
| -```sql |
| 133 | + |
| 134 | +If you add required columns in the index, you can avoid this extra lookup in the table. You can add these columns as standard included columns, as shown in the following example, which extends the preceding `CREATE INDEX` example. |
| 135 | + |
| 136 | +```sql |
116 | 137 | CREATE INDEX idx_soh_json_CustomerName
|
117 |
| -ON Sales.SalesOrderHeader(vCustomerName) |
118 |
| -INCLUDE(SalesOrderNumber,OrderDate) |
119 |
| -``` |
120 |
| - |
121 |
| -In this case, SQL Server doesn't have to read additional data from the `SalesOrderHeader` table because everything it needs is included in the nonclustered JSON index. This type of index is a good way to combine JSON and column data in queries and to create optimal indexes for your workload. |
122 |
| - |
| 138 | + ON Sales.SalesOrderHeader(vCustomerName) |
| 139 | + INCLUDE(SalesOrderNumber, OrderDate); |
| 140 | +``` |
| 141 | + |
| 142 | +In this case, SQL Server doesn't have to read more data from the `SalesOrderHeader` table because everything it needs is included in the nonclustered JSON index. This type of index is a good way to combine JSON and column data in queries and to create optimal indexes for your workload. |
| 143 | + |
123 | 144 | ## JSON indexes are collation-aware indexes
|
124 |
| -An important feature of indexes over JSON data is that the indexes are collation-aware. The result of the `JSON_VALUE` function that you use when you create the computed column is a text value that inherits its collation from the input expression. Therefore, values in the index are ordered using the collation rules defined in the source columns. |
125 |
| - |
126 |
| -To demonstrate that the indexes are collation-aware, the following example creates a simple collection table with a primary key and JSON content. |
127 |
| - |
128 |
| -```sql |
| 145 | + |
| 146 | +An important feature of indexes over JSON data is that the indexes are collation-aware. The result of the `JSON_VALUE` function that you use when you create the computed column is a text value that inherits its collation from the input expression. Therefore, values in the index are ordered using the collation rules defined in the source columns. |
| 147 | + |
| 148 | +To demonstrate that the indexes are collation-aware, the following example creates a simple collection table with a primary key and JSON content. |
| 149 | + |
| 150 | +```sql |
129 | 151 | CREATE TABLE JsonCollection
|
130 |
| - ( |
131 |
| - id INT IDENTITY CONSTRAINT PK_JSON_ID PRIMARY KEY, |
132 |
| - [json] NVARCHAR(MAX) COLLATE SERBIAN_CYRILLIC_100_CI_AI |
133 |
| - CONSTRAINT [Content should be formatted as JSON] |
134 |
| - CHECK(ISJSON(json)>0) |
135 |
| - ) |
136 |
| -``` |
137 |
| - |
138 |
| -The preceding command specifies the Serbian Cyrillic collation for the `json` column. The following example populates the table and creates an index on the name property. |
139 |
| - |
140 |
| -```sql |
| 152 | +( |
| 153 | + id INT IDENTITY CONSTRAINT PK_JSON_ID PRIMARY KEY, |
| 154 | + [json] NVARCHAR (MAX) COLLATE SERBIAN_CYRILLIC_100_CI_AI |
| 155 | + CONSTRAINT [Content should be formatted as JSON] CHECK (ISJSON(json) > 0) |
| 156 | +); |
| 157 | +``` |
| 158 | + |
| 159 | +The preceding command specifies the Serbian Cyrillic collation for the `json` column. The following example populates the table and creates an index on the name property. |
| 160 | + |
| 161 | +```sql |
141 | 162 | INSERT INTO JsonCollection
|
142 | 163 | VALUES
|
143 |
| -(N'{"name":"Иво","surname":"Андрић"}'), |
144 |
| -(N'{"name":"Андрија","surname":"Герић"}'), |
145 |
| -(N'{"name":"Владе","surname":"Дивац"}'), |
146 |
| -(N'{"name":"Новак","surname":"Ђоковић"}'), |
147 |
| -(N'{"name":"Предраг","surname":"Стојаковић"}'), |
148 |
| -(N'{"name":"Михајло","surname":"Пупин"}'), |
149 |
| -(N'{"name":"Борислав","surname":"Станковић"}'), |
150 |
| -(N'{"name":"Владимир","surname":"Грбић"}'), |
151 |
| -(N'{"name":"Жарко","surname":"Паспаљ"}'), |
152 |
| -(N'{"name":"Дејан","surname":"Бодирога"}'), |
153 |
| -(N'{"name":"Ђорђе","surname":"Вајферт"}'), |
154 |
| -(N'{"name":"Горан","surname":"Бреговић"}'), |
155 |
| -(N'{"name":"Милутин","surname":"Миланковић"}'), |
156 |
| -(N'{"name":"Никола","surname":"Тесла"}') |
| 164 | + (N'{"name":"Иво","surname":"Андрић"}'), |
| 165 | + (N'{"name":"Андрија","surname":"Герић"}'), |
| 166 | + (N'{"name":"Владе","surname":"Дивац"}'), |
| 167 | + (N'{"name":"Новак","surname":"Ђоковић"}'), |
| 168 | + (N'{"name":"Предраг","surname":"Стојаковић"}'), |
| 169 | + (N'{"name":"Михајло","surname":"Пупин"}'), |
| 170 | + (N'{"name":"Борислав","surname":"Станковић"}'), |
| 171 | + (N'{"name":"Владимир","surname":"Грбић"}'), |
| 172 | + (N'{"name":"Жарко","surname":"Паспаљ"}'), |
| 173 | + (N'{"name":"Дејан","surname":"Бодирога"}'), |
| 174 | + (N'{"name":"Ђорђе","surname":"Вајферт"}'), |
| 175 | + (N'{"name":"Горан","surname":"Бреговић"}'), |
| 176 | + (N'{"name":"Милутин","surname":"Миланковић"}'), |
| 177 | + (N'{"name":"Никола","surname":"Тесла"}'); |
157 | 178 | GO
|
158 |
| - |
| 179 | + |
159 | 180 | ALTER TABLE JsonCollection
|
160 |
| -ADD vName AS JSON_VALUE(json,'$.name') |
| 181 | + ADD vName AS JSON_VALUE(json, '$.name'); |
161 | 182 |
|
162 | 183 | CREATE INDEX idx_name
|
163 |
| -ON JsonCollection(vName) |
164 |
| -``` |
165 |
| - |
166 |
| -The preceding commands create a standard index on the computed column `vName`, which represents the value from the JSON `$.name` property. In the Serbian Cyrillic code page, the order of the letters is `А`, `Б`, `В`, `Г`, `Д`, `Ђ`, `Е`, etc. The order of items in the index is compliant with Serbian Cyrillic rules because the result of the `JSON_VALUE` function inherits its collation from the source column. The following example queries this collection and sorts the results by name. |
167 |
| - |
168 |
| -```sql |
169 |
| -SELECT JSON_VALUE(json,'$.name'),* |
| 184 | + ON JsonCollection(vName); |
| 185 | +``` |
| 186 | + |
| 187 | +The preceding commands create a standard index on the computed column `vName`, which represents the value from the JSON `$.name` property. In the Serbian Cyrillic code page, the order of the letters is `А`, `Б`, `В`, `Г`, `Д`, `Ђ`, `Е`, etc. The order of items in the index is compliant with Serbian Cyrillic rules because the result of the `JSON_VALUE` function inherits its collation from the source column. The following example queries this collection and sorts the results by name. |
| 188 | + |
| 189 | +```sql |
| 190 | +SELECT JSON_VALUE(json, '$.name'), |
| 191 | + * |
170 | 192 | FROM JsonCollection
|
171 |
| -ORDER BY JSON_VALUE(json,'$.name') |
172 |
| -``` |
173 |
| - |
174 |
| - If you look at the actual execution plan, you see that it uses sorted values from the nonclustered index. |
175 |
| - |
176 |
| - :::image type="content" source="media/index-json-data/json-index-scan.png" alt-text="Screenshot showing an execution plan that uses sorted values from the non-clustered index." lightbox="media/index-json-data/json-index-scan.png"::: |
177 |
| - |
178 |
| - Although the query has an `ORDER BY` clause, the execution plan doesn't use a Sort operator. The JSON index is already ordered according to Serbian Cyrillic rules. Therefore SQL Server can use the nonclustered index where results are already sorted. |
179 |
| - |
180 |
| - However, if you change the collation of the `ORDER BY` expression - for example, if you add `COLLATE French_100_CI_AS_SC` after the `JSON_VALUE` function - you get a different query execution plan. |
181 |
| - |
182 |
| - :::image type="content" source="media/index-json-data/json-index-execution-plan.png" alt-text="Screenshot showing a different execution plan." lightbox="media/index-json-data/json-index-execution-plan.png"::: |
183 |
| - |
184 |
| - Since the order of values in the index is not compliant with French collation rules, SQL Server can't use the index to order results. Therefore, it adds a Sort operator that sorts results using French collation rules. |
| 193 | +ORDER BY JSON_VALUE(json, '$.name'); |
| 194 | +``` |
185 | 195 |
|
186 |
| -### Microsoft videos |
| 196 | +If you look at the actual execution plan, you see that it uses sorted values from the nonclustered index. |
| 197 | + |
| 198 | +:::image type="content" source="media/index-json-data/json-index-scan.png" alt-text="Screenshot showing an execution plan that uses sorted values from the nonclustered index." lightbox="media/index-json-data/json-index-scan.png"::: |
187 | 199 |
|
188 |
| -> [!NOTE] |
189 |
| -> Some of the video links in this section might not work at this time. Microsoft is migrating content formerly on Channel 9 to a new platform. We will update the links as the videos are migrated to the new platform. |
| 200 | +Although the query has an `ORDER BY` clause, the execution plan doesn't use a Sort operator. The JSON index is already ordered according to Serbian Cyrillic rules. Therefore SQL Server can use the nonclustered index where results are already sorted. |
| 201 | + |
| 202 | +However, if you change the collation of the `ORDER BY` expression - for example, if you add `COLLATE French_100_CI_AS_SC` after the `JSON_VALUE` function - you get a different query execution plan. |
| 203 | + |
| 204 | +:::image type="content" source="media/index-json-data/json-index-execution-plan.png" alt-text="Screenshot showing a different execution plan." lightbox="media/index-json-data/json-index-execution-plan.png"::: |
| 205 | + |
| 206 | +Since the order of values in the index isn't compliant with French collation rules, SQL Server can't use the index to order results. Therefore, it adds a Sort operator that sorts results using French collation rules. |
| 207 | + |
| 208 | +### Microsoft videos |
190 | 209 |
|
191 |
| -For a visual introduction to the built-in JSON support in SQL Server and Azure SQL Database, see the following videos: |
| 210 | +For a visual introduction to the built-in JSON support in SQL Server and Azure SQL Database, see the following video: |
192 | 211 |
|
193 |
| -- [JSON as a bridge between NoSQL and relational worlds](https://channel9.msdn.com/events/DataDriven-SQLServer2016/JSON-as-bridge-betwen-NoSQL-relational-worlds) |
| 212 | +- [JSON as a bridge between NoSQL and relational worlds](/shows/datadriven-sqlserver2016/json-as-bridge-betwen-nosql-relational-worlds) |
194 | 213 |
|
195 | 214 | ## Related content
|
196 | 215 |
|
|
0 commit comments