Azure Cosmos DB Indexes

We know that Azure Cosmos DB is multi model data store i.e. it supports multiple ways of modelling and storing data. Also, Cosmos DB has transparent index management, which means that the users do not have to worry about managing indexes and Cosmos DB takes care of indexing the data internally. In this post, we will discuss how data is indexed within Cosmos DB and how those indexes are used to boost query performance.

For every item stored in a Cosmos DB container, a JSON document is generated and then converted into a tree representation i.e. every property of the item gets represented as node in a tree data structure.

Let’s have a look at the example JSON document below:

{
        "locations": [
                      { "country": "Germany", "city": "Berlin" },
                      { "country": "France", "city": "Paris" }
                    ],
        "headquarters": { "country": "Belgium", "employees": 250 },
        "exports": [
                    { "city": "Moscow" },
                    { "city": "Athens" }
                   ]
 }

After conversion to tree, the JSON document above, looks like this:

The important thing to note here, is that each element of array is converted to an intermediate node in the tree structure. e.g. the locations array with two elements….

"locations": [
                      { "country": "Germany", "city": "Berlin" },
                      { "country": "France", "city": "Paris" }
             ]

….gets represented as two intermediate nodes, 0 and 1 in the tree data structure:

Once the tree structure is generated, it is possible to reference every property by their paths in the tree. E.g.

  • /locations/0/country: “Germany”
  • /locations/0/city: “Berlin”
  • /locations/1/country: “France”
  • /locations/1/city: “Paris”
  • /headquarters/country: “Belgium”
  • /headquarters/employees: 250
  • /exports/0/city: “Moscow”
  • /exports/1/city: “Athens”

This allows Cosmos DB to index each property’s path and the corresponding value, irrespective of the underlying data storage model of the item.

Cosmos DB supports three kinds of indexes. This will be discussed in a future post.

Reference: https://docs.microsoft.com/en-us/azure/cosmos-db/index-overview

One thought on “Azure Cosmos DB Indexes

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 )

Facebook photo

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

Connecting to %s

%d bloggers like this: