Concepts
In this article, we will explore how to design and implement native applications using Microsoft Azure Cosmos DB by focusing on the implementation of a composite index. A composite index is a powerful feature that allows you to optimize queries by combining multiple properties into a single index. By using composite indexes effectively, you can improve query performance and reduce the amount of data that needs to be scanned.
Enabling Composite Index
To get started, let’s assume that we have a collection in our Cosmos DB database that stores information about exams. Each document in the collection represents an exam and contains properties such as examId, studentId, courseId, and score. Our goal is to create a composite index that can efficiently query exams based on a combination of studentId and courseId.
First, we need to enable the composite index feature for our collection. To do this, follow the steps below:
- Navigate to the Azure portal.
- Open the Cosmos DB account that contains your database.
- Go to the “Data Explorer” and select your database and collection.
- Click on the “Scale & Settings” tab and then click on the “Composite Index” tab.
- Enable the “Composite Indexing” feature if it’s not already enabled.
Creating a Composite Index
Once the feature is enabled, we can create a composite index by defining the paths for the properties that we want to include in the index. In our case, we want to include the studentId and courseId properties. To create the index, we need to specify these paths in the correct order. The order of the paths is important because it determines the order of the index keys.
Here’s an example code snippet that demonstrates how to create a composite index using the Azure Cosmos DB SDK:
using Microsoft.Azure.Cosmos;
using System.Threading.Tasks;
string endpointUri = "your_cosmosdb_endpoint_uri";
string primaryKey = "your_cosmosdb_primary_key";
CosmosClient client = new CosmosClient(endpointUri, primaryKey);
Database database = await client.CreateDatabaseIfNotExistsAsync("your_database_id");
Container container = await database.CreateContainerIfNotExistsAsync("your_container_id", "/partitionKeyPath");
await container.IndexingPolicy.CreateIndexingPolicyAsync(new Cosmos.IndexingPolicy
{
CompositeIndexes = new Collection<>
{
new Collection
{
new Cosmos.CompositePath()
{
Path = "/studentId",
Order = Cosmos.SortOrder.Ascending
},
new Cosmos.CompositePath()
{
Path = "/courseId",
Order = Cosmos.SortOrder.Ascending
}
}
}
});
Make sure to replace your_cosmosdb_endpoint_uri
and your_cosmosdb_primary_key
with the appropriate values for your Cosmos DB account. Also, provide the desired IDs for your database and container.
In the code snippet above, we create an instance of CosmosClient
by providing the endpoint URI and primary key. Then, we create or retrieve the database and container using their IDs. Finally, we define the composite index by specifying the paths for the studentId
and courseId
properties and their sort order. In this example, we set the sort order to be ascending for both.
After executing the code, the composite index will be created for the specified paths. Cosmos DB will automatically maintain and update the index as new documents are inserted, updated, or deleted.
Querying Using the Composite Index
Now, we can efficiently query the exams collection using the composite index. For example, to retrieve all exams for a particular student and course, execute the following code snippet:
using Microsoft.Azure.Cosmos;
using System.Linq;
using System.Threading.Tasks;
string endpointUri = "your_cosmosdb_endpoint_uri";
string primaryKey = "your_cosmosdb_primary_key";
CosmosClient client = new CosmosClient(endpointUri, primaryKey);
Database database = await client.CreateDatabaseIfNotExistsAsync("your_database_id");
Container container = database.GetContainer("your_container_id");
string studentId = "your_student_id";
string courseId = "your_course_id";
IQueryable query = container.GetItemLinqQueryable()
.Where(e => e.studentId == studentId && e.courseId == courseId);
FeedIterator iterator = query.ToFeedIterator();
while (iterator.HasMoreResults)
{
FeedResponse
foreach (Exam exam in response)
{
// Process each exam
}
}
Again, ensure that you provide the correct values for your_cosmosdb_endpoint_uri
, your_cosmosdb_primary_key
, your_database_id
, and your_container_id
. Also, replace your_student_id
and your_course_id
with the desired values for your query.
In the code snippet, we create a LINQ query that filters exams based on the studentId
and courseId
properties. The composite index we created earlier will be utilized to efficiently retrieve the desired results. We then iterate through the results using a feed iterator.
By implementing a composite index in Microsoft Azure Cosmos DB, we can optimize queries involving multiple properties and improve the performance of our native applications. This allows us to efficiently retrieve the data we need while minimizing the amount of data scanned by the database.
Answer the Questions in Comment Section
Which of the following statements is true about composite indexes in Azure Cosmos DB?
a) Composite indexes are created automatically for all collections in Cosmos DB.
b) Composite indexes improve the performance of queries that involve multiple properties.
c) Composite indexes can only be created on string properties.
d) Composite indexes cannot be used for sorting the query results.
Correct answer: b) Composite indexes improve the performance of queries that involve multiple properties.
True or False: Composite indexes can be created on both single-partition and multi-partition collections in Azure Cosmos DB.
Correct answer: True
When defining a composite index, which order should the properties be specified in the index path?
a) Ascending order
b) Descending order
c) Random order
d) Alphabetical order
Correct answer: d) Alphabetical order
Which of the following statements is true about the query performance with composite indexes in Azure Cosmos DB?
a) The performance of queries improves with each additional property added to the composite index.
b) Composite indexes have no impact on query performance.
c) The performance of queries deteriorates with each additional property added to the composite index.
d) The order of properties in the composite index has no impact on query performance.
Correct answer: a) The performance of queries improves with each additional property added to the composite index.
True or False: Composite indexes can be created on nested properties within a document in Azure Cosmos DB.
Correct answer: True
When querying with a composite index in Azure Cosmos DB, which of the following operators can be used to filter the results?
a) Equal
b) Greater than or equal
c) Less than
d) All of the above
Correct answer: d) All of the above
Which API can be used to create a composite index in Azure Cosmos DB?
a) SQL API
b) Cassandra API
c) Gremlin API
d) MongoDB API
Correct answer: a) SQL API
True or False: Composite indexes in Azure Cosmos DB are automatically updated whenever the indexed properties are modified.
Correct answer: True
How many properties can be included in a composite index in Azure Cosmos DB?
a) Up to 5 properties
b) Up to 10 properties
c) Up to 50 properties
d) Unlimited number of properties
Correct answer: b) Up to 10 properties
Which of the following is NOT a recommended practice when working with composite indexes in Azure Cosmos DB?
a) Define composite indexes for every queryable property in a collection.
b) Optimize index paths to match frequently executed queries.
c) Monitor query performance and add or modify composite indexes as needed.
d) Avoid creating composite indexes with a large number of properties.
Correct answer: b) Optimize index paths to match frequently executed queries.
Great post! Clear explanation on implementing a composite index in Cosmos DB.
Thanks for the article. It really helped me understand the performance implications.
Can someone explain how to handle composite indexes when you have frequent schema changes?
Implementing composite indexes drastically reduced my query latency. Highly recommend trying them out.
Do composite indexes increase the cost of write operations in Cosmos DB?
Excellent article! Very helpful in preparation for DP-420 exam.
The details on composite index configuration were spot on. Thanks!
One question: How does implementing a composite index affect the storage usage?