Skip to content

Commit e2657d4

Browse files
Merge pull request #215080 from jcocchi/index-pk-for-exclude-policy
update indexing docs for including partition key with exclude strategy
2 parents ebe690f + b45cbc1 commit e2657d4

File tree

1 file changed

+27
-26
lines changed

1 file changed

+27
-26
lines changed

articles/cosmos-db/index-policy.md

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ In some situations, you may want to override this automatic behavior to better s
2626
Azure Cosmos DB supports two indexing modes:
2727

2828
- **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.
3030

3131
> [!NOTE]
3232
> 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).
3333
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.
3535

3636
## <a id="index-size"></a>Index size
3737

@@ -71,16 +71,17 @@ Taking the same example again:
7171

7272
- the path to anything under `headquarters` is `/headquarters/*`
7373

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.
7575

7676
## Include/exclude strategy
7777

7878
Any indexing policy has to include the root path `/*` as either an included or an excluded path.
7979

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.
8281

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.
8485

8586
- The system property `_etag` is excluded from indexing by default, unless the etag is added to the included path for indexing.
8687

@@ -92,9 +93,9 @@ When including and excluding paths, you may encounter the following attributes:
9293

9394
- `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`.
9495

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.
9697

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:
9899

99100
| **Property Name** | **Default Value** |
100101
| ----------------------- | -------------------------------- |
@@ -114,7 +115,7 @@ Here's an example:
114115

115116
**Excluded Path**: `/food/ingredients/*`
116117

117-
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.
118119

119120
Here are some rules for included and excluded paths precedence in Azure Cosmos DB:
120121

@@ -136,13 +137,13 @@ When you define a spatial path in the indexing policy, you should define which i
136137

137138
* LineString
138139

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.
140141

141142
## Composite indexes
142143

143144
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.
144145

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.
146147

147148
When defining a composite index, you specify:
148149

@@ -157,7 +158,7 @@ When defining a composite index, you specify:
157158

158159
The following considerations are used when using composite indexes for queries with an `ORDER BY` clause with two or more properties:
159160

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.
161162

162163
- The order of composite index paths (ascending or descending) should also match the `order` in the `ORDER BY` clause.
163164

@@ -180,15 +181,15 @@ You should customize your indexing policy so you can serve all necessary `ORDER
180181

181182
If a query has filters on two or more properties, it may be helpful to create a composite index for these properties.
182183

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:
184185

185186
```sql
186187
SELECT *
187188
FROM c
188189
WHERE c.name = "John" AND c.age > 18
189190
```
190191

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)`.
192193

193194
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.
194195

@@ -200,13 +201,13 @@ FROM c
200201
WHERE c.name = "John" AND c.age > 18 AND c._ts > 1612212188
201202
```
202203

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.
204205

205206
The following considerations are used when creating composite indexes for queries with filters on multiple properties
206207

207208
- 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.
210211
- 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.
211212
- 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.
212213

@@ -289,8 +290,8 @@ ORDER BY c.firstName, c.lastName
289290

290291
The following considerations apply when creating composite indexes to optimize a query with a filter and `ORDER BY` clause:
291292

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.
294295
* If the query filters on multiple properties, the equality filters must be the first properties in the `ORDER BY` clause.
295296
* 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.
296297
* 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
316317
* If the query filters on multiple properties, the equality filters must be the first properties in the composite index.
317318
* You can have a maximum of one range filter per composite index and it must be on the property in the aggregate system function.
318319
* 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.
320321

321322
| **Composite Index** | **Sample Query** | **Supported by Composite Index?** |
322323
| ---------------------------------------- | ------------------------------------------------------------ | --------------------------------- |
@@ -336,13 +337,13 @@ A container's indexing policy can be updated at any time [by using the Azure por
336337
> [!NOTE]
337338
> 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).
338339
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.
340341

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.
342343

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.
344345

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.
346347

347348
> [!NOTE]
348349
> 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
351352

352353
Using the [Time-to-Live (TTL) feature](time-to-live.md) requires indexing. This means that:
353354

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.
356357

357358
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.
358359

0 commit comments

Comments
 (0)