All Products
Search
Document Center

Tablestore:Use a secondary index to read data

Last Updated:Mar 15, 2024

Tablestore allows you to read a single row of data or data whose primary key values are within a specific range from an index table. If the index table contain the attribute columns that you want to return, you can read the index table to obtain the data. Otherwise, you need to query the data from the data table for which the index table is created.

Prerequisites

Usage notes

  • You can use an index table only to read data.

  • The first primary key column of a local secondary index must be the same as the first primary key column of the data table.

  • If the attribute columns that you want to return are not contained in the index table, you need to query data from the data table for which the index table is created to obtain the required attribute columns.

Read a single row of data

You can call the GetRow operation to read a single row of data. For more information, see Read a single row of data.

Parameters

When you call the GetRow operation to read data from an index table, take note of the following items:

  • You must set the table_name parameter to the name of the index table.

  • Tablestore automatically adds the primary key columns of the data table that are not specified as index columns to an index table as the primary key columns of the index table. Therefore, when you specify the primary key columns of a row in an index table, you must specify the index columns based on which you create the index table and the primary key columns of the data table.

Examples

The following sample code provides an example on how to read a row of data in an index table based on the primary key of the row:

$request = [
 'table_name' => '<INDEX_NAME>', // Specify the name of the index table. 
 'primary_key' => [ // Specify the primary key of the row based on which you want to read data. If you want to read data from 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. 
 ['PK0', 123],
 ['Col1', "abc"],
 ['PK1', 'abc']
 ],
 'max_versions' => 1, // Specify that the latest version of data is read. 
 'columns_to_get' => ['Col1'] // Specify the attribute columns that you want to return. 
];
$response = $otsClient->getRow($request); 

Read data whose primary key values are within a specific range

You can call the GetRange operation to read data whose primary key values are within a specific range. For more information, see Read data whose primary key values are within a specific range.

Parameters

When you call the GetRange operation to read data from an index table, take note of the following items:

  • You must set the table_name parameter to the name of the index table.

  • Tablestore automatically adds the primary key columns of the data table that are not specified as index columns to an index table as the primary key columns of the index table. Therefore, when you specify the start primary key and end primary key of the range that you want to query, you must specify the index columns based on which you create the index table and the primary key columns of the data table.

Examples

The following sample code provides an example on how to read data whose primary key values are within a specific range from an index table:

// Query the rows in which the value of the PK0 column is in the range of [1, 4). 
// You must specify values for all primary key columns when you specify a range. If the range does not involve the values of a column, set the value of the column to an infinitely great value (INF_MAX) or an infinitely small value (INF_MIN). 
// If you want to read data from 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. 
$startPK = [
 ['Col1', "abc"], 
 ['PK0', null, PrimaryKeyTypeConst::CONST_INF_MIN] // INF_MIN indicates the minimum value. 
 ['PK1', null, PrimaryKeyTypeConst::CONST_INF_MIN] // INF_MIN indicates the minimum value. 
];
// You must specify values for all primary key columns when you specify a range. If the range does not involve the values of a column, set the value of the column to an infinitely great value (INF_MAX) or an infinitely small value (INF_MIN). 
$endPK = [
 ['Col1', "abc"],
 ['PK0', null, PrimaryKeyTypeConst::CONST_INF_MAX] // INF_MAX indicates the maximum value. 
 ['PK1', null, PrimaryKeyTypeConst::CONST_INF_MAX] // INF_MAX indicates the maximum value. 
];
$request = [
 'table_name' => '<INDEX_NAME>', // Specify the name of the index table. 
 'max_versions' => 1, // Specify that the latest version of data is read. 
 'direction' => DirectionConst::CONST_FORWARD, // Query data in the forward direction. 
 'inclusive_start_primary_key' => $startPK, // Specify the start primary key. 
 'exclusive_end_primary_key' => $endPK, // Specify the end primary key. 
 'limit' => 10 // Specify that up to 10 rows of data can be returned. 
];
$response = $otsClient->getRange ($request);
print "Read CU Consumed: {$response['consumed']['capacity_unit']['read']}\n";

foreach ($response['rows'] as $rowData) {
 // Process each row of data. 
} 

FAQ

References

  • If your business requires multi-dimensional queries and data analysis, you can create a search index and specify the required attributes as the fields of the search index. Then, you can query and analyze data by using the search index. For example, you can use a search index to perform queries based on non-primary key columns, Boolean queries, and fuzzy queries. You can also use a search index to obtain the maximum and minimum values, collect statistics about the number of rows, and group query results. For more information, see Search index.

  • If you want to execute SQL statements to query and analyze data, you can use the SQL query feature. For more information, see SQL query.