All Products
Search
Document Center

Tablestore:What do I do if data whose primary keys are out of the specified range is returned when the GetRange operation is called to read data?

Last Updated:Aug 22, 2024

If data whose primary keys are out of the specified range is returned when the GetRange operation is called to read data, you can create a secondary index to read only data whose primary keys are within the specified range.

Problem description

When the GetRange operation is called to perform range query in the Tablestore console or by using Tablestore SDKs, query the external tables of Tablestore, or synchronize data from Tablestore by using Tablestore Reader, data whose primary keys are out of the specified range is returned.

Possible cause

The specified query conditions do not comply with the leftmost matching principle. For example, if a data table contains two primary key columns, beginPrimaryKey = INF_MIN and endPrimaryKey = INF_MAX, are specified for the first primary key column, and beginPrimaryKey = 10 and endPrimaryKey = 10 are specified for the second primary key column, all data in the table, instead of only the rows in which the second primary key column value is 10, is returned.

Important

The leftmost matching principle indicates that the range specified by the start primary key and the end primary key in the right primary key column only takes effect when the start primary key and the end primary key are the same within each leftmost primary key column. If the start primary key and the end primary key in a leftmost primary key column are different, the range specified by the start primary key and the end primary key in the right primary key column does not take effect. Here, the leftmost and right primary keys refer to the order in which the primary keys are defined in the table structure.

Solution

Create a secondary index with the sequence of primary key columns specified, and choose a proper query method as required.

  • To query data within a specified range in the Tablestore console or by using specific SDKs, use the secondary index. For more information, see Query data and Get started with secondary indexes by using Tablestore SDKs.

  • To query data within a specified range in a Tablestore external table, set the table name to the secondary index name in the SQL statement that is executed to create the external table. For more information, see Create a Tablestore external table.

  • To synchronize data within a specified range from Tablestore by using Tablestore Reader, set the table name to the secondary index name in the data synchronization script. For more information, see Tablestore Reader.