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.
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 max_versions parameter is set to 1 is created. One of the following conditions must be met by the time_to_live parameter of the data table:
The time_to_live parameter of the data table is set to -1, which means that data in the data table never expires.
The time_to_live 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
If you use Tablestore SDK for Python to create a secondary index for a data table, the existing data of the data table cannot be included in the search index. If you want to include the existing data of a data table in the secondary index that is created for the data table, use Tablestore SDKs for other programming languages, the Tablestore console, or the Tablestore CLI to create a secondary index.
Parameter | Description |
main_table_name | The name of the data table. |
index_meta | The schema information about the index table. The schema information contains the following items:
|
Examples
Create a global secondary index
The following sample code provides an example on how to create a secondary index for a data table. In the example, the primary key columns of the data table are pk1 and pk2. The primary key columns that are specified for the secondary index are definedcol1 and pk1. The attribute columns that are specified for the secondary index are definedcol2 and definedcol3.
# Specify the metadata of the index table.
# Specify the name, primary key columns, and attribute columns of the index table in sequence.
index_meta = SecondaryIndexMeta('<INDEX_NAME>', ['definedcol1', 'pk1'], ['definedcol2', 'definedcol3'])
# Specify the name of the data table.
client.create_secondary_index('<TABLE_NAME>', index_meta)
Create a local secondary index
The following sample code provides an example on how to create a secondary index for a data table. In the example, the primary key columns of the data table are pk1 and pk2. The primary key columns that are specified for the secondary index are definedcol1 and pk1. The attribute columns that are specified for the secondary index are definedcol2 and definedcol3.
# Specify the metadata of the index table.
# Specify the name, primary key columns, attribute columns, and type of the index table in sequence.
# The first primary key column of a local secondary index must be the same as the first primary key column of the data table.
index_meta = SecondaryIndexMeta('<INDEX_NAME>', ['pk1', 'definedcol1'], ['definedcol2', 'definedcol3'],index_type= SecondaryIndexType.LOCAL_INDEX)
# Specify the name of the data table.
client.create_secondary_index('<TABLE_NAME>', index_meta)
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.