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 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.

API operation

/**
 * Create a secondary index. 
 * @api
 *
 * @param [] $request
 *            The request parameter, which is the name of the data table. 
 * @return [] The response.
 * @throws OTSClientException The exception that is returned when a parameter error occurs or the Tablestore server returns a verification error. 
 * @throws OTSServerException The exception that is returned when the Tablestore server returns an error. 
 * @example "src/examples/CreateIndex.php"
 */
public function createIndex(array $request)

Parameters

Parameter

Description

table_name

The name of the data table.

index_meta

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

  • name: the name of the index table.

  • primary_key: the primary key of the index table. The primary key is a combination of all primary key columns and a random number of predefined columns of the data table.

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

  • index_update_mode: the update mode of the index table. Valid values: IndexUpdateModeConst::ASYNC_INDEX and IndexUpdateModeConst::SYNC_INDEX.

    • If you do not specify the index_update_mode parameter or you set the index_update_mode parameter to IndexUpdateModeConst::ASYNC_INDEX, the asynchronous mode is used to update the index.

      If you use the global secondary index feature, you must set the index_update_mode parameter to IndexUpdateModeConst::ASYNC_INDEX.

    • If you set the index_update_mode parameter to IndexUpdateModeConst::SYNC_INDEX, the synchronous mode is used to update the index.

      If you use the local secondary index feature, you must set the index_update_mode parameter to IndexUpdateModeConst::SYNC_INDEX.

  • index_type: the type of the index table. Valid values: IndexTypeConst::GLOBAL_INDEX and IndexTypeConst::LOCAL_INDEX.

    • If you not specify the index_type parameter or you set the index_type parameter to IndexTypeConst::GLOBAL_INDEX, a global secondary index is created.

      Tablestore automatically synchronizes data from the indexed columns and primary key columns of the data table to the columns of the index table that you want to create in asynchronous mode. The synchronization latency is within a few milliseconds.

    • If you set the index_type parameter to IndexTypeConst::LOCAL_INDEX, a local secondary index is created.

      Tablestore automatically synchronizes data from the indexed columns and primary key columns of the data table to the columns of the index table that you want to create in synchronous mode. After data is written to the data table, you can immediately query the data in the index table.

include_base_data

Specifies whether to include the existing data of the data table in the index table. Default value: false.

  • If you set the include_base_data parameter to true, the existing data of the data table is included in the index table.

  • If you do not specify the include_base_data parameter or you set the include_base_data parameter to false, the existing data of the data table is not included in the index table.

Examples

Create a global secondary index

If you do not specify the index_type and index_update_mode parameters when you create a secondary index, a global secondary index is created.

$request = array(
    'table_name' => '<TABLE_NAME>', // Specify the name of the data table. 
    //'include_base_data' => true,  // Set the include_base_data parameter to true to include the existing data of the data table to the index table. 
    'index_meta' => array(
        'name' => '<INDEX_NAME>',  // Specify the name of the index table. 
        'primary_key' => array('Col1'), // Specify the primary key column of the index table. 
        'defined_column' => array('Col2')  // Specify the attribute column of the index table. 
    )
);
$otsClient->createIndex($request);

You can also specify the index_type and index_update_mode parameters to create a global secondary index.

$request = array(
    'table_name' => '<TABLE_NAME>', // Specify the name of the data table. 
    //'include_base_data' => true,  // Set the include_base_data parameter to true to include the existing data of the data table to the index table. 
    'index_meta' => array(
        'name' => '<INDEX_NAME>',  // Specify the name of the index table. 
        'primary_key' => array('Col1'), // Specify the primary key column of the index table. 
        'defined_column' => array('Col2')  // Specify the attribute column of the index table. 
        'index_type' => IndexTypeConst::GLOBAL_INDEX,
        'index_update_mode' => IndexUpdateModeConst::ASYNC_INDEX
    )
);
$otsClient->createIndex($request);

Create a local secondary index

The following sample code provides an example on how to create a local secondary index:

$createLocalRequest = array (
   'table_name' => '<TABLE_NAME>', // Specify the name of the data table. 
    //'include_base_data' => true,  // Set the include_base_data parameter to true to include the existing data of the data table to the index table. 
   'index_meta' => array (
       'name' => '<INDEX_NAME>',  // Specify the name of the index table. 
       'primary_key' => array('PK0', 'Col1'), // Specify the primary key column of the index table. The first primary key column of the index table must be the same as the first primary key column of the data table. 
       'defined_column' => array('Col2')  // Specify the attribute column of the index table. 
       'index_type' => IndexTypeConst::LOCAL_INDEX,
       'index_update_mode' => IndexUpdateModeConst::SYNC_INDEX
   )
);
$otsClient->createIndex($createLocalRequest);

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.