Redigera

Dela 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. You can enable this feature by using the Azure portal and the feature can't be disabled. 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 handle cross-partition queries efficiently.

Prerequisites

Enable global secondary indexes

The global secondary index feature needs to be enabled for your Azure Cosmos DB account before provisioning a builder or creating index containers.

  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.

Warning

The global secondary index feature can't be disabled on an account once enabled, however the global secondary builder and index containers themselves can be deprovisioned.

Create a global secondary index builder

After the global secondary index feature is enabled for your account, you'll see a new page in the Settings section of the Azure portal for Global Secondary Index Builder. You must provision a builder before creating global secondary index containers in your account. The builder is responsible for automatically hydrating data in the index containers and keeping them in sync with source containers. Learn more about options for provisioning the global secondary index builder.

  1. Sign in to the Azure portal.

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

  3. In the resource menu, select Global Secondary Index Builder.

  4. On the Global Secondary Index Builder page, configure the SKU and the number of instances for the builder.

    Note

    This resource menu option and page appear only when the global secondary index feature is enabled for the account.

  5. Select Save.

Create a global secondary index

After the global secondary index feature is enabled and the builder is provisioned, you can create index containers.

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 source container in your Azure Cosmos DB account, 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 that has /customerId as the partition key path. Name this source container gsi-src.

    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"
    }
    

    Note

    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. 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, the builder automatically syncs data 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 index container.

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 use be executed 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