Optimize index creation in Azure Cosmos DB for MongoDB vCore

APPLIES TO: MongoDB vCore

The CreateIndexes Command in Azure Cosmos DB for MongoDB vCore has an option to optimize index creation, especially beneficial for scenarios involving empty collections. This document outlines the usage and expected behavior of this new option.

Advantages in Specific Scenarios

  • Efficiency in Migration Utilities: This option is ideal in migration contexts, reducing the time for index creation by preventing delays caused by waiting for transactions with pre-existing snapshots.
  • Streamlined Index Creation Process: In Cosmos DB for MongoDB vCore, this translates to a simpler process with a single collection scan, enhancing efficiency.
  • Enhanced Control: Users gain more control over the indexing process, crucial in environments balancing read and write operations during index creation.

Prerequisites

Default Setting

The default value of this option is false, ensuring backward compatibility and maintaining the existing non-blocking behavior.

Blocking Option

The CreateIndexes Command includes a { "blocking": true } option, designed to provide more control over the indexing process in an empty collection.

Setting { "blocking": true } blocks all write operations (delete, update, insert) to the collection until index creation is completed. This feature is particularly useful in scenarios such as migration utilities where indexes are created on empty collections before data writes commence.

Create an index using the blocking option

For simplicity, let us consider an example of a blog application with the following setup:

  • Database name: cosmicworks
  • Collection name: products

To demonstrate the use of this new option in the cosmicworks database for an empty collection named products. This code snippet demonstrates how to use the blocking option, which will temporarily block write operations to the specified collection during index creation in an empty collection:

use cosmicworks;
db.runCommand({
  createIndexes: "products",
  indexes: [{"key":{"name":1}, "name":"name_1"}],
  blocking: true
})

Summary

The introduction of the blocking option in the CreateIndexes Command of Azure Cosmos DB for MongoDB (vCore) is a strategic enhancement for optimizing index creation for an empty collection. This feature complements the existing non-blocking method, providing an additional tool for scenarios requiring efficient index creation on empty collections.

Check out text indexing, which allows for efficient searching and querying of text-based data.

Next step