Create a secondary index

更新时间:
复制 MD 格式

The secondary index feature lets you query data by the primary key columns of a data table and the index columns of a secondary index. To accelerate queries on attribute columns, create a secondary index for the data table. When creating a secondary index, set its index columns or attribute columns to predefined columns defined at table creation time.

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

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

Prerequisites

Before you begin, ensure that you have:

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

  • A data table with maxVersions set to 1. The timeToLive parameter of the data table must meet one of the following conditions:

    • timeToLive is set to -1, which means data never expires.

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

  • Predefined columns specified for the data table.

Usage notes

  • Tablestore SDK for .NET supports only global secondary indexes. To create a local secondary index, use Tablestore SDK for another programming language, the Tablestore console, or the Tablestore CLI.

  • The index table name must be different from any existing time series table or data table name.

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

Parameters

Parameter

Description

mainTableName

The name of the data table.

indexMeta

The schema of the index table. This parameter contains the following fields:

  • indexName: the name of the index table.

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

  • definedColumns: the attribute columns of the index table, which are a subset of the 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 example creates a global secondary index that excludes existing data from the data table. The data table has two primary key columns: pk1 and pk2. The index column is col1 and the attribute column is col2. The resulting index table has three primary key columns (col1, pk1, and pk2) and one attribute column (col2).

public static void CreateGlobalIndex(OTSClient otsClient, String TableName, String IndexName)
{
    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);

 }

What's next