You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/cosmos-db/index-policy.md
+27-26Lines changed: 27 additions & 26 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -26,12 +26,12 @@ In some situations, you may want to override this automatic behavior to better s
26
26
Azure Cosmos DB supports two indexing modes:
27
27
28
28
-**Consistent**: The index is updated synchronously as you create, update or delete items. This means that the consistency of your read queries will be the [consistency configured for the account](consistency-levels.md).
29
-
-**None**: Indexing is disabled on the container. This is commonly used when a container is used as a pure key-value store without the need for secondary indexes. It can also be used to improve the performance of bulk operations. After the bulk operations are complete, the index mode can be set to Consistent and then monitored using the [IndexTransformationProgress](how-to-manage-indexing-policy.md#dotnet-sdk) until complete.
29
+
-**None**: Indexing is disabled on the container. This mode is commonly used when a container is used as a pure key-value store without the need for secondary indexes. It can also be used to improve the performance of bulk operations. After the bulk operations are complete, the index mode can be set to Consistent and then monitored using the [IndexTransformationProgress](how-to-manage-indexing-policy.md#dotnet-sdk) until complete.
30
30
31
31
> [!NOTE]
32
32
> Azure Cosmos DB also supports a Lazy indexing mode. Lazy indexing performs updates to the index at a much lower priority level when the engine is not doing any other work. This can result in **inconsistent or incomplete** query results. If you plan to query an Azure Cosmos DB container, you should not select lazy indexing. New containers cannot select lazy indexing. You can request an exemption by contacting [email protected] (except if you are using an Azure Cosmos DB account in [serverless](serverless.md) mode which doesn't support lazy indexing).
33
33
34
-
By default, indexing policy is set to `automatic`. It's achieved by setting the `automatic` property in the indexing policy to `true`. Setting this property to `true` allows Azure Cosmos DB to automatically index documents as they are written.
34
+
By default, indexing policy is set to `automatic`. It's achieved by setting the `automatic` property in the indexing policy to `true`. Setting this property to `true` allows Azure Cosmos DB to automatically index documents as they're written.
35
35
36
36
## <aid="index-size"></a>Index size
37
37
@@ -71,16 +71,17 @@ Taking the same example again:
71
71
72
72
- the path to anything under `headquarters` is `/headquarters/*`
73
73
74
-
For example, we could include the `/headquarters/employees/?` path. This path would ensure that we index the employees property but would not index additional nested JSON within this property.
74
+
For example, we could include the `/headquarters/employees/?` path. This path would ensure that we index the employees property but wouldn't index additional nested JSON within this property.
75
75
76
76
## Include/exclude strategy
77
77
78
78
Any indexing policy has to include the root path `/*` as either an included or an excluded path.
79
79
80
-
- Include the root path to selectively exclude paths that don't need to be indexed. This is the recommended approach as it lets Azure Cosmos DB proactively index any new property that may be added to your model.
81
-
- Exclude the root path to selectively include paths that need to be indexed.
80
+
- Include the root path to selectively exclude paths that don't need to be indexed. This approach is recommended as it lets Azure Cosmos DB proactively index any new property that may be added to your model.
82
81
83
-
- For paths with regular characters that include: alphanumeric characters and _ (underscore), you don't have to escape the path string around double quotes (for example, "/path/?"). For paths with other special characters, you need to escape the path string around double quotes (for example, "/\"path-abc\"/?"). If you expect special characters in your path, you can escape every path for safety. Functionally, it doesn't make any difference if you escape every path Vs just the ones that have special characters.
82
+
- Exclude the root path to selectively include paths that need to be indexed. The partition key property path isn't indexed by default with the exclude strategy and should be explicitly included if needed.
83
+
84
+
- For paths with regular characters that include: alphanumeric characters and _ (underscore), you don't have to escape the path string around double quotes (for example, "/path/?"). For paths with other special characters, you need to escape the path string around double quotes (for example, "/\"path-abc\"/?"). If you expect special characters in your path, you can escape every path for safety. Functionally, it doesn't make any difference if you escape every path or just the ones that have special characters.
84
85
85
86
- The system property `_etag` is excluded from indexing by default, unless the etag is added to the included path for indexing.
86
87
@@ -92,9 +93,9 @@ When including and excluding paths, you may encounter the following attributes:
92
93
93
94
-`precision` is a number defined at the index level for included paths. A value of `-1` indicates maximum precision. We recommend always setting this value to `-1`.
94
95
95
-
-`dataType` can be either `String` or `Number`. This indicates the types of JSON properties which will be indexed.
96
+
-`dataType` can be either `String` or `Number`. This indicates the types of JSON properties that will be indexed.
96
97
97
-
It is no longer necessary to set these properties. When not specified, these properties will have the following default values:
98
+
It's no longer necessary to set these properties. When not specified, these properties will have the following default values:
In this case, the included path takes precedence over the excluded path because it is more precise. Based on these paths, any data in the `food/ingredients` path or nested within would be excluded from the index. The exception would be data within the included path: `/food/ingredients/nutrition/*`, which would be indexed.
118
+
In this case, the included path takes precedence over the excluded path because it's more precise. Based on these paths, any data in the `food/ingredients` path or nested within would be excluded from the index. The exception would be data within the included path: `/food/ingredients/nutrition/*`, which would be indexed.
118
119
119
120
Here are some rules for included and excluded paths precedence in Azure Cosmos DB:
120
121
@@ -136,13 +137,13 @@ When you define a spatial path in the indexing policy, you should define which i
136
137
137
138
* LineString
138
139
139
-
Azure Cosmos DB, by default, will not create any spatial indexes. If you would like to use spatial SQL built-in functions, you should create a spatial index on the required properties. See [this section](sql-query-geospatial-index.md) for indexing policy examples for adding spatial indexes.
140
+
Azure Cosmos DB, by default, won't create any spatial indexes. If you would like to use spatial SQL built-in functions, you should create a spatial index on the required properties. See [this section](sql-query-geospatial-index.md) for indexing policy examples for adding spatial indexes.
140
141
141
142
## Composite indexes
142
143
143
144
Queries that have an `ORDER BY` clause with two or more properties require a composite index. You can also define a composite index to improve the performance of many equality and range queries. By default, no composite indexes are defined so you should [add composite indexes](how-to-manage-indexing-policy.md#composite-index) as needed.
144
145
145
-
Unlike with included or excluded paths, you can't create a path with the `/*` wildcard. Every composite path has an implicit `/?` at the end of the path that you don't need to specify. Composite paths lead to a scalar value and this is the only value that is included in the composite index.
146
+
Unlike with included or excluded paths, you can't create a path with the `/*` wildcard. Every composite path has an implicit `/?` at the end of the path that you don't need to specify. Composite paths lead to a scalar value that is the only value included in the composite index.
146
147
147
148
When defining a composite index, you specify:
148
149
@@ -157,7 +158,7 @@ When defining a composite index, you specify:
157
158
158
159
The following considerations are used when using composite indexes for queries with an `ORDER BY` clause with two or more properties:
159
160
160
-
- If the composite index paths do not match the sequence of the properties in the `ORDER BY` clause, then the composite index can't support the query.
161
+
- If the composite index paths don't match the sequence of the properties in the `ORDER BY` clause, then the composite index can't support the query.
161
162
162
163
- The order of composite index paths (ascending or descending) should also match the `order` in the `ORDER BY` clause.
163
164
@@ -180,15 +181,15 @@ You should customize your indexing policy so you can serve all necessary `ORDER
180
181
181
182
If a query has filters on two or more properties, it may be helpful to create a composite index for these properties.
182
183
183
-
For example, consider the following query which has both an equality and range filter:
184
+
For example, consider the following query that has both an equality and range filter:
184
185
185
186
```sql
186
187
SELECT*
187
188
FROM c
188
189
WHEREc.name="John"ANDc.age>18
189
190
```
190
191
191
-
This query will be more efficient, taking less time and consuming fewer RU's, if it is able to leverage a composite index on `(name ASC, age ASC)`.
192
+
This query will be more efficient, taking less time and consuming fewer RUs, if it's able to leverage a composite index on `(name ASC, age ASC)`.
192
193
193
194
Queries with multiple range filters can also be optimized with a composite index. However, each individual composite index can only optimize a single range filter. Range filters include `>`, `<`, `<=`, `>=`, and `!=`. The range filter should be defined last in the composite index.
194
195
@@ -200,13 +201,13 @@ FROM c
200
201
WHEREc.name="John"ANDc.age>18ANDc._ts>1612212188
201
202
```
202
203
203
-
This query will be more efficient with a composite index on `(name ASC, age ASC)` and `(name ASC, _ts ASC)`. However, the query would not utilize a composite index on `(age ASC, name ASC)` because the properties with equality filters must be defined first in the composite index. Two separate composite indexes are required instead of a single composite index on `(name ASC, age ASC, _ts ASC)` since each composite index can only optimize a single range filter.
204
+
This query will be more efficient with a composite index on `(name ASC, age ASC)` and `(name ASC, _ts ASC)`. However, the query wouldn't utilize a composite index on `(age ASC, name ASC)` because the properties with equality filters must be defined first in the composite index. Two separate composite indexes are required instead of a single composite index on `(name ASC, age ASC, _ts ASC)` since each composite index can only optimize a single range filter.
204
205
205
206
The following considerations are used when creating composite indexes for queries with filters on multiple properties
206
207
207
208
- Filter expressions can use multiple composite indexes.
208
-
- The properties in the query's filter should match those in composite index. If a property is in the composite index but is not included in the query as a filter, the query will not utilize the composite index.
209
-
- If a query has additional properties in the filter that were not defined in a composite index, then a combination of composite and range indexes will be used to evaluate the query. This will require fewer RU's than exclusively using range indexes.
209
+
- The properties in the query's filter should match those in composite index. If a property is in the composite index but isn't included in the query as a filter, the query won't utilize the composite index.
210
+
- If a query has other properties in the filter that aren't defined in a composite index, then a combination of composite and range indexes will be used to evaluate the query. This will require fewer RUs than exclusively using range indexes.
210
211
- If a property has a range filter (`>`, `<`, `<=`, `>=`, or `!=`), then this property should be defined last in the composite index. If a query has more than one range filter, it may benefit from multiple composite indexes.
211
212
- When creating a composite index to optimize queries with multiple filters, the `ORDER` of the composite index will have no impact on the results. This property is optional.
212
213
@@ -289,8 +290,8 @@ ORDER BY c.firstName, c.lastName
289
290
290
291
The following considerations apply when creating composite indexes to optimize a query with a filter and `ORDER BY` clause:
291
292
292
-
* If you do not define a composite index on a query with a filter on one property and a separate `ORDER BY` clause using a different property, the query will still succeed. However, the RU cost of the query can be reduced with a composite index, particularly if the property in the `ORDER BY` clause has a high cardinality.
293
-
* If the query filters on properties, these should be included first in the `ORDER BY` clause.
293
+
* If you don't define a composite index on a query with a filter on one property and a separate `ORDER BY` clause using a different property, the query will still succeed. However, the RU cost of the query can be reduced with a composite index, particularly if the property in the `ORDER BY` clause has a high cardinality.
294
+
* If the query filters on properties, these properties should be included first in the `ORDER BY` clause.
294
295
* If the query filters on multiple properties, the equality filters must be the first properties in the `ORDER BY` clause.
295
296
* If the query filters on multiple properties, you can have a maximum of one range filter or system function utilized per composite index. The property used in the range filter or system function should be defined last in the composite index.
296
297
* All considerations for creating composite indexes for `ORDER BY` queries with multiple properties as well as queries with filters on multiple properties still apply.
@@ -316,7 +317,7 @@ The following considerations apply when creating composite indexes to optimize a
316
317
* If the query filters on multiple properties, the equality filters must be the first properties in the composite index.
317
318
* You can have a maximum of one range filter per composite index and it must be on the property in the aggregate system function.
318
319
* The property in the aggregate system function should be defined last in the composite index.
319
-
* The `order` (`ASC` or `DESC`) does not matter.
320
+
* The `order` (`ASC` or `DESC`) doesn't matter.
320
321
321
322
|**Composite Index**|**Sample Query**|**Supported by Composite Index?**|
@@ -336,13 +337,13 @@ A container's indexing policy can be updated at any time [by using the Azure por
336
337
> [!NOTE]
337
338
> You can track the progress of index transformation in the Azure portal or [by using one of the SDKs](how-to-manage-indexing-policy.md).
338
339
339
-
There is no impact to write availability during any index transformations. The index transformation uses your provisioned RUs but at a lower priority than your CRUD operations or queries.
340
+
There's no impact to write availability during any index transformations. The index transformation uses your provisioned RUs but at a lower priority than your CRUD operations or queries.
340
341
341
-
There is no impact to read availability when adding new indexed paths. Queries will only utilize new indexed paths once an index transformation is complete. In other words, when adding a new indexed paths, queries that benefit from that indexed path will have the same performance before and during the index transformation. After the index transformation is complete, the query engine will begin to use the new indexed paths.
342
+
There's no impact to read availability when adding new indexed paths. Queries will only utilize new indexed paths once an index transformation is complete. In other words, when adding a new indexed path, queries that benefit from that indexed path will have the same performance before and during the index transformation. After the index transformation is complete, the query engine will begin to use the new indexed paths.
342
343
343
-
When removing indexed paths, you should group all your changes into one indexing policy transformation. If you remove multiple indexes and do so in one single indexing policy change, the query engine provides consistent and complete results throughout the index transformation. However, if you remove indexes through multiple indexing policy changes, the query engine will not provide consistent or complete results until all index transformations complete. Most developers do not drop indexes and then immediately try to run queries that utilize these indexes so, in practice, this situation is unlikely.
344
+
When removing indexed paths, you should group all your changes into one indexing policy transformation. If you remove multiple indexes and do so in one single indexing policy change, the query engine provides consistent and complete results throughout the index transformation. However, if you remove indexes through multiple indexing policy changes, the query engine won't provide consistent or complete results until all index transformations complete. Most developers don't drop indexes and then immediately try to run queries that utilize these indexes so, in practice, this situation is unlikely.
344
345
345
-
When you drop an indexed path, the query engine will immediately stop using it and instead do a full scan.
346
+
When you drop an indexed path, the query engine will immediately stop using it, and will do a full scan instead.
346
347
347
348
> [!NOTE]
348
349
> Where possible, you should always try to group multiple indexing changes into one single indexing policy modification
@@ -351,8 +352,8 @@ When you drop an indexed path, the query engine will immediately stop using it a
351
352
352
353
Using the [Time-to-Live (TTL) feature](time-to-live.md) requires indexing. This means that:
353
354
354
-
- it is not possible to activate TTL on a container where the indexing mode is set to `none`,
355
-
- it is not possible to set the indexing mode to None on a container where TTL is activated.
355
+
- it isn't possible to activate TTL on a container where the indexing mode is set to `none`,
356
+
- it isn't possible to set the indexing mode to None on a container where TTL is activated.
356
357
357
358
For scenarios where no property path needs to be indexed, but TTL is required, you can use an indexing policy with an indexing mode set to `consistent`, no included paths, and `/*` as the only excluded path.
0 commit comments