Edit

Share via


How to configure Azure Cosmos DB for NoSQL global secondary indexes (preview)

APPLIES TO: NoSQL

Important

Azure Cosmos DB for NoSQL global secondary indexes are currently in preview. This preview is provided without a service-level agreement. At this time, we don't recommend that you use global secondary indexes for production workloads. Certain features of this preview might not be supported or might have constrained capabilities. For more information, see the supplemental terms of use for Microsoft Azure previews.

Global secondary indexes provide a powerful way to optimize query performance and simplify application logic by storing your data with a different partition key and/ or data model. This article describes how to create global secondary indexes and how to use them to avoid cross-partition queries.

Prerequisites

Enable global secondary indexes

Enable the global secondary index feature for your Azure Cosmos DB account. Continuous backups must be turned on for the account before enabling global secondary indexes.

  1. Sign in to the Azure portal.

  2. Go to your Azure Cosmos DB for NoSQL account.

  3. In the resource menu, select Settings.

  4. Navigate to the Features page. Then select Global Secondary Index for NoSQL API (preview) and Enable.

    Screenshot of how to enable the Global Secondary Index feature in the Azure portal.

Create a global secondary index

After the global secondary index feature is enabled, you can create global secondary indexes.

Create a source container

Global secondary indexes store a copy of data from the source container. Before creating a global secondary index, create the source container that your index container will be built from. If you already have a container in your Azure Cosmos DB account that you would like to use as the source, you can skip these steps.

  1. Use the Azure portal, the Azure SDKs, the Azure CLI, or the REST API to create a source container named gsi-src with /customerId as the partition key path.

    Note

    The /customerId field is used only as an example in this article. For your own containers, select a partition key that works for your solution.

  2. Insert a few items in the source container. To follow the examples that are shown in this article, make sure that the items have customerId and emailAddress fields. A sample item might look like this:

    {
      "id": "eaf0338e-2b61-4163-822f-7bef75bf51de",
      "customerId": "36c7cc3d-1709-45c6-819f-10e5586a6cb7",
      "emailAddress": "justine@contoso.com",
      "name": "Justine"
    }
    

    Tip

    In this example, you populate the source container with sample data before adding an index container. You can also create a global secondary index from an empty source container.

Create a global secondary index container

Once the source container is created, you can create a global secondary index container using the Azure portal or Azure CLI.

  1. Navigate to Data Explorer in your Azure Cosmos DB account. Select your source container, gsi-src in this example, and select New Global Secondary Index from the drop-down.

    Screenshot of how to create a Global Secondary Index in the Data Explorer page of the Azure portal.

  2. The source container ID will be populated for you. In the Index container id field, enter gsi-target.

  3. In the Global secondary index definition field, enter SELECT c.customerId, c.emailAddress FROM c.

  4. In the Partition key field, enter /emailAddress.

    Screenshot of how to configure a Global Secondary Index in the Data Explorer page of the Azure portal.

  5. Global secondary index containers must use autoscale throughput. Configure any other container settings you'd like and select OK to create the global secondary index container.

  6. After the global secondary index container is created, data is automatically synced from the source container. Try executing create, update, and delete operations in the source container. You'll see the same changes propagated to items in the global secondary index.

Query data from global secondary indexes

In this example, we have a source container partitioned on customerId and a global secondary index container partitioned on emailAddress. Without the index container, queries that only include the emailAddress would be cross-partition, but now they can be run against the global secondary index instead to increase efficiency.

Querying data from global secondary indexes is similar to querying data from any other container. You can use the Azure portal, Azure SDKs, or REST API to query data in global secondary indexes.

Container container = client.GetDatabase("gsi-db").GetContainer("gsi-target");

FeedIterator<MyClass> myQuery = container.GetItemQueryIterator<MyClass>(new QueryDefinition("SELECT * FROM c WHERE c.emailAddress = 'justine@contoso.com'"));

Next steps