All Products
Search
Document Center

Tablestore:Create a secondary index

Last Updated:Mar 15, 2024

The secondary index feature allows you to query data based on the primary key of a data table and the index columns of the secondary index that is created for the data table. If you need to use the attribute columns of a data table to query data, you can create a secondary index for the data table to accelerate data queries. When you create a secondary index for a data table, you can set the index columns or attribute columns of the secondary index to the predefined columns that you specified when you created the data table. After you create a secondary index, you can use the secondary index to query data.

Note
  • Secondary indexes are classified into global secondary indexes and local secondary indexes. For more information about the secondary index feature, see Overview.

  • You can create one or more index tables when you create a data table by calling the CreateTable operation. For more information, see Create data tables.

Prerequisites

  • An OTSClient instance is initialized. For more information, see Initialize an OTSClient instance.

  • A data table for which the maxVersions parameter is set to 1 is created. One of the following conditions must be met by the timeToLive parameter of the data table:

    • The timeToLive parameter of the data table is set to -1, which means that data in the data table never expires.

    • The timeToLive parameter of the data table is set to a value other than -1, and update operations on the data table are prohibited.

  • Predefined columns are specified for the data table.

Usage notes

  • You can use Tablestore SDK for .NET to create only global secondary indexes. If you want to use the local secondary index feature, you can use Tablestore SDK for other programming languages, the Tablestore console, or the Tablestore CLI to create a local secondary index.

  • The name of an index table must be different from the name of an existing time series table or data table.

  • When you create a secondary index, Tablestore automatically adds the primary key columns of the data table that are not specified as index columns to the secondary index as the primary key columns of the secondary index.

Parameters

Parameter

Description

mainTableName

The name of the data table.

indexMeta

The schema information about the index table. The schema information contains the following items:

  • indexName: the name of the index table.

  • primaryKey: the primary key of the index table, which is a combination of all primary key columns and a random number of predefined columns of the data table.

  • definedColumns: the attribute columns of the index table. The attribute columns are a combination of predefined columns of the data table.

  • indexUpdateMode: the update mode of the index table. Valid value: IUM_ASYNC_INDEX.

  • indexType: the type of the index table. Valid value: IT_GLOBAL_INDEX.

Examples

The following sample code provides an example on how to create a global secondary index that does not include the existing data of the data table for which the global secondary index is created. In the example, the primary key columns of the data table are pk1 and pk2. The primary key column and the attribute column that are specified for the global secondary index are col1 and col2, respectively. The primary key columns of the index table consist of col1, pk1, and pk2. The attribute column of the index table is col2.

public static void CreateGlobalIndex()
{
    OTSClient otsClient = Config.GetClient();

    Console.WriteLine("Start create globalIndex...");

    IndexMeta indexMeta = new IndexMeta(IndexName);
    // Specify a primary key column for the index table. 
    indexMeta.PrimaryKey = new List<string>() { "col1" };
    // Specify an attribute column for the index table. 
    indexMeta.DefinedColumns = new List<string>() { "col2" };
    //indexMeta.IndexType = IndexType.IT_GLOBAL_INDEX;
    //indexMeta.IndexUpdateModel = IndexUpdateMode.IUM_ASYNC_INDEX;

    CapacityUnit reservedThroughput = new CapacityUnit(0, 0);
    CreateGlobalIndexRequest request = new CreateGlobalIndexRequest(TableName, indexMeta);
    otsClient.CreateGlobalIndex(request);

    Console.WriteLine("Global Index is created,tableName: " + TableName + ",IndexName:" + IndexName);

 }

References

  • After you create a secondary index, you can use the secondary index to read a single row of data or data whose primary key values are within a specific range. For more information, see Use a secondary index to read data.

  • You can delete a secondary index that you no longer use. For more information, see Delete a secondary index.