When querying data with SQL, you can target a mapping table created for a data table or one created for a search index. The right choice depends on your consistency and accuracy requirements.
How Tablestore routes SQL queries
Tablestore stores large amounts of structured data and supports multiple index schemas for different query and analytics scenarios:
Primary keys on data tables, which support single-row reads (GetRow) and range reads (GetRange)
Secondary index tables with custom primary key columns
Search indexes built on inverted indexes and spatio-temporal indexes
The SQL query feature lets you query data through either a mapping table created for a data table or one created for a search index. When you use a data table mapping table, Tablestore can automatically select a search index for execution depending on your configuration. For details, see Index selection policy.
Sample scenario
The following examples use a data table named exampletable with these columns:
|
Column |
Type |
Role |
|
|
Integer |
Primary key |
|
|
String |
Attribute column |
|
|
String |
Attribute column |
A search index named exampletable_searchindex is created for this table. It covers two columns: id (Integer) and context (Text).
Key fact: name is not in the search index. This determines when Tablestore falls back to the data table for query execution.
Choose a method based on your requirements:
Use a mapping table for a data table
When you create a mapping table for a data table, two parameters control query behavior:
|
Parameter |
Values |
Effect |
|
|
|
Controls whether results reflect the latest writes |
|
|
|
Controls whether Tablestore may route aggregation queries through a search index, which can return approximate results |
The combination of these two parameters determines which of the three methods below applies.
For instructions on creating a mapping table for a data table, see Create a mapping table for a table.
Method 1: Eventual consistency, aggregation accuracy not required
Use this method when your queries don't require the latest data and you can accept approximate aggregation results. Tablestore automatically uses a search index when all queried columns are covered by the index, improving query performance.
How automatic index selection works: Tablestore checks whether all columns in the SELECT list are covered by a search index.
If all columns are covered, Tablestore routes the query through the search index.
-
If any column is missing from the index, Tablestore uses the data table.
-
Create a mapping table named
exampletablefor the data table. Use default settings for all parameters.CREATE TABLE `exampletable` (`id` BIGINT, `name` MEDIUMTEXT, `context` MEDIUMTEXT, PRIMARY KEY(id)) ENGINE='Tablestore'; -
Query data.
The following statement selects all columns (
*). Becausenameis not inexampletable_searchindex, the index cannot cover the full query. Tablestore routes the query to the data table.SELECT * FROM exampletable LIMIT 10;The following statement selects only
idandcontext, both of which are inexampletable_searchindex. Tablestore routes the query to the search index.SELECT id,context FROM exampletable LIMIT 10;
-
Method 2: Strong consistency
Use this method when your queries must reflect the latest writes. Tablestore never routes queries through a search index in this mode, because search indexes use eventual consistency and cannot guarantee that results include the most recent data.
-
Create a mapping table named
exampletablefor the data table and setdata_consistencytostrong.CREATE TABLE `exampletable` (`id` BIGINT, `name` MEDIUMTEXT, `context` MEDIUMTEXT, PRIMARY KEY(id)) ENGINE='Tablestore', ENGINE_ATTRIBUTE='{"data_consistency": "strong"}'; -
Query data. For instructions, see Query data.
Search indexes use eventual consistency and cannot guarantee strong consistency. Therefore, Tablestore does not use search indexes for data query.
Method 3: Eventual consistency, aggregation accuracy required
Use this method when you need eventual consistency but require accurate aggregation results. Because search indexes may return approximate aggregation results, Tablestore does not use them for query execution in this mode.
-
Create a mapping table named
exampletablefor the data table, setdata_consistencytoeventual, and setallow_inaccurate_aggregationtofalse.CREATE TABLE `exampletable` (`id` BIGINT, `name` MEDIUMTEXT, `context` MEDIUMTEXT, PRIMARY KEY(id)) ENGINE='Tablestore', ENGINE_ATTRIBUTE='{"data_consistency": "eventual", "allow_inaccurate_aggregation": false}'; -
Query data. For instructions, see Query data.
Search indexes may return approximate aggregation results. Therefore, Tablestore does not use search indexes for data query.
Use a mapping table for a search index
Create a mapping table directly for a search index when you want to query using full-text search or other capabilities specific to that index—for example, using the text_match function on a Text-type column. Queries always run against the specified search index, regardless of column coverage.
For instructions on creating a mapping table for a search index, see Create mapping tables for search indexes.
-
Create a mapping table named
exampletable_searchindex_testfor the search index.CREATE TABLE `exampletable_searchindex_test` (`id` BIGINT, `context` MEDIUMTEXT) ENGINE='searchindex' ENGINE_ATTRIBUTE='{"index_name": "exampletable_searchindex", "table_name": "exampletable"}' -
Query data. The following example uses
text_matchto run a full-text search on thecontextcolumn.SELECT id,context FROM exampletable_searchindex_test WHERE text_match(context, "tablestore cool") LIMIT 10;