Indexing is important and can save significant costs and improve dramatically performance.

Test Setup

Four keys scenarios were tested:

  • Container with indexing policy to set to None
  • Container with indexing policy to set to Automatic where all properties has been explicit excluded
  • Container with indexing policy to set to Automatic where all properties except partition key has been explicit excluded
  • Container with indexing policy to set to Automatic

Container may consume 400 RU/s max for each test and it store 1000 or 10000 documents in 5 or 50 partitions, distributed equally across all. The document has three properties id, orderNumber and partitionKey.

{
    "id": "f72841ec-6ca5-4630-b61c-fb16f22c6336",
    "orderNumber": "1",
    "partitionKey": "1"
}

Test Results

The cost of all database operations is normalized by Azure Cosmos DB and is expressed by Request Units (or RUs, for short). You can think of RUs per second as the currency for throughput. RUs per second is a rate-based currency. It abstracts the system resources such as CPU, IOPS, and memory that are required to perform the database operations supported by Azure Cosmos DB.

https://docs.microsoft.com/en-us/azure/cosmos-db/request-units
Indexing policyCount of DocumentsCount of PartitionsWrite timeWrite RU/sAverage Write RU/s
none1000500:00:34.8254950.00142.14
automatic (excluded all)1000500:00:34.8725520.00158.29
automatic (only partition key index)1000500:00:34.9805900.00168.67
automatic1000500:00:34.7226290.00181.15
Indexing policyCount of DocumentsCount of PartitionsRead timeRead RU/sAverage Read RU/s
none1000500:00:01.812131.3072.46
automatic (excluded all)1000500:00:01.949131.3067.37
automatic (only partition key index)1000500:00:01.40252.5537.48
automatic1000500:00:01.40252.5537.48
Indexing policyCount of DocumentsCount of PartitionsWrite timeWrite RU/sAverage Write RU/s
none100005000:06:18.00049500.00130.95
automatic (excluded all)100005000:05:50.32655200.00157.57
automatic (only partition key index)100005000:05:39.67459000.00173.70
automatic100005000:05:40.00062900.00185.00
Indexing policyCount of DocumentsCount of PartitionsRead timeRead RU/sAverage Read RU/s
none100005000:00:36.61211433.00312.27
automatic (excluded all)100005000:00:35.16411433.00325.13
automatic (only partition key index)100005000:00:06.546525.0080.20
automatic100005000:00:03.190525.00164.58

How indexing important is can be seen by working with high load. By reading 1000 documents hasn’t index much impact on read time. But already here was visible that not indexed containers consumed as much as double of RU/s more towards indexed. But the real drama was seen by not indexed containers by reading 10000 documents. Not indexed time containers consumed as much as 21 times more RU/s as indexed, the read time increased by twelvefold.

The last one thing: you have already seen this but this is reminder. When you set indexing policy to automatic and you exclude all properties it means really everything. Not even partition key is indexed.

Source Code

https://github.com/kalcik/blog-samples/tree/master/CosmosDbPerformanceMeasurement

About the Author Anton Kalcik

Most of the time, I assist people in the creation of valuable software. I’m a software engineer and entrepreneur specializing in .NET and Microsoft Azure. I offer Code Katas, Coding Dojos, workshops and talks about .NET, Microsoft Azure, DevOps, Agile Methodologies and Clean Code. I'm founder of CoderDojo Wien and president of digital.austria association.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.