Indexing Policy
← Back to DP-420 Terminology Reference
Category: Optimization
Summary: Defines which properties are indexed, trading off query performance vs. write RU cost.
Overview
An indexing policy defines which properties of items in a container are indexed and how, controlling the trade-off between query performance (read efficiency) and write RU cost/storage (since more indexed paths mean more write overhead). By default, Azure Cosmos DB automatically indexes every property, but you can customize the policy to include/exclude specific paths, favoring either a read-heavy or write-heavy workload.
{
"indexingMode": "consistent",
"automatic": true,
"includedPaths": [
{
"path": "/*"
}
],
"excludedPaths": [
{
"path": "/\"_etag\"/?"
}
]
}
var containerDefinition = new ContainerDefinition
{
Id = "Products",
PartitionKeyPath = "/category",
IndexingPolicy = new IndexingPolicy
{
Automatic = true,
IndexingMode = IndexingMode.Consistent,
IncludedPaths =
{
new IncludedPath { Path = "/*" } // Optional when automatic is true
},
ExcludedPaths =
{
new ExcludedPath { Path = "/description/*" } // Exclude description
}
}
};
string sqlQueryText = "SELECT TOP 10 c.id FROM c WHERE c.Item = 'value1234' AND c.Price > 2";
QueryDefinition query = new QueryDefinition(sqlQueryText);
FeedIterator resultSetIterator = container.GetItemQueryIterator(
query, requestOptions: new QueryRequestOptions
{
PopulateIndexMetrics = true
});
FeedResponse response = null;
while (resultSetIterator.HasMoreResults)
{
response = await resultSetIterator.ReadNextAsync();
Console.WriteLine(response.IndexMetrics);
}
More Info & Links
DP-420 Style Practice Questions
Q1. What is indexed by default in a new Azure Cosmos DB for NoSQL container?
- A. Every property of every item, using the default automatic indexing policy
- B. No properties, until explicitly configured
- C. Only the partition key property
- D. Only properties marked as unique keys
Q2. Why might you exclude certain paths from indexing in a write-heavy workload?
- A. To reduce the RU cost and latency of write operations by avoiding unnecessary index maintenance
- B. To increase the maximum logical partition size
- C. To enable multi-region writes
- D. To allow TTL to be configured
Answer Key
Answers are listed at the very end so you can attempt the questions first.
- Q1 (Indexing Policy): A. Every property of every item, using the default automatic indexing policy — By default, Azure Cosmos DB automatically indexes all properties unless a custom indexing policy excludes some paths.
- Q2 (Indexing Policy): A. To reduce the RU cost and latency of write operations by avoiding unnecessary index maintenance — Excluding unused paths from indexing reduces the RU cost of writes, which is valuable for write-heavy workloads.
DP-420: Designing and Implementing Cloud-Native Applications Using Microsoft Azure Cosmos DB — term detail page