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 data whose primary keys are only 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.

Note 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. The leftmost primary key columns are defined earlier than the right primary key column in the table schema.

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 about specific operations, see Read data in the console and Read data by using 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 Access Tablestore data.
  • 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 about specific operations, see Tablestore Reader.