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.
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
maxVersionsset to1. ThetimeToLiveparameter of the data table must meet one of the following conditions:timeToLiveis set to-1, which means data never expires.timeToLiveis 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:
|
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
After you create a secondary index, read a single row or a range of rows using the index. For more information, see Use a secondary index to read data.
To remove a secondary index you no longer need, see Delete a secondary index.