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.
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 a data table.
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
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.
When you create a local secondary index, the first primary key column of the index table must be the same as the first primary key column of the data table.
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:
|
IncludeBaseData | Specifies whether to include the existing data of the data table in the index table. If you set the IncludeBaseData parameter to true, the index table contains the existing data of the data table. If you set the IncludeBaseData parameter to false, the index table does not contain the existing data of the data table. |
Examples
Create a global secondary index
The following sample code provides an example on how to create a global secondary index that includes 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 definedcol1 and definedcol2, respectively. The primary key columns of the index table consist of definedcol1, pk1, and pk2. The attribute column of the index table is definedcol2.
func CreateGlobalIndexSample(client *tablestore.TableStoreClient, tableName string) {
// Specify the metadata of the index table.
indexMeta := new(tablestore.IndexMeta)
// Specify the definedcol1 column of the data table as the primary key column of the index table.
indexMeta.AddPrimaryKeyColumn("definedcol1")
// Specify the definedcol2 column of the data table as the attribute column of the index table.
indexMeta.AddDefinedColumn("definedcol2")
// Specify the name of the index table.
indexMeta.IndexName = "<INDEX_NAME>"
indexReq := &tablestore.CreateIndexRequest{
// Specify the data table for which you want to create the index table and apply the settings that you specified for the index table.
MainTableName:tableName,
IndexMeta: indexMeta,
/**
You can set the IncludeBaseData parameter to true to synchronize the existing data of the data table to the index table. This way, you can use the index table to query all data in the data table.
The amount of time required to synchronize the existing data of the data table to the index table varies based on the amount of data in the data table.
*/
IncludeBaseData: true,
}
resp, err := client.CreateIndex(indexReq)
if err != nil {
fmt.Println("Failed to create table with error:", err)
} else {
fmt.Println("Create index finished", resp)
}
}
Create a local secondary index
The following sample code provides an example on how to create a local secondary index that includes 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 columns that are specified for the local secondary index are pk1 and definedcol1. The attribute column that is specified for the local secondary index is definedcol2. The primary key columns of the index table consist of pk1, definedcol1, and pk2. The attribute column of the index table is definedcol2.
func CreateGLocalIndexSample(client *tablestore.TableStoreClient, tableName string) {
// Specify the metadata of the index table.
indexMeta := new(tablestore.IndexMeta)
// Set the first primary key column of the index table to the first primary key column of the data table.
indexMeta.AddPrimaryKeyColumn("pk1")
// Specify the definedcol1 column of the data table as the primary key column of the index table.
indexMeta.AddPrimaryKeyColumn("definedcol1")
// Specify the definedcol2 column of the data table as the attribute column of the index table.
indexMeta.AddDefinedColumn("definedcol2")
// Set the index type to IT_LOCAL_INDEX.
indexMeta.IndexType = tablestore.IT_LOCAL_INDEX
// Specify the name of the index table.
indexMeta.IndexName = "<INDEX_NAME>"
indexReq := &tablestore.CreateIndexRequest{
// Specify the data table for which you want to create the index table and apply the settings that you specified for the index table.
MainTableName:tableName,
IndexMeta: indexMeta,
/**
You can set the IncludeBaseData parameter to true to synchronize the existing data of the data table to the index table. This way, you can use the index table to query all data in the data table.
The amount of time required to synchronize the existing data of the data table to the index table varies based on the amount of data in the data table.
*/
IncludeBaseData: true,
}
resp, err := client.CreateIndex(indexReq)
if err != nil {
fmt.Println("Failed to create table with error:", err)
} else {
fmt.Println("Create index finished", resp)
}
}
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.