Azure Cosmos DB Cassandra API 中的辅助索引编制Secondary indexing in Azure Cosmos DB Cassandra API

适用于: Cassandra API

Azure Cosmos DB 中的 Cassandra API 利用底层索引基础结构来公开平台中固有的索引编制强度。The Cassandra API in Azure Cosmos DB leverages the underlying indexing infrastructure to expose the indexing strength that is inherent in the platform. 但是,与核心 SQL API 不同,Azure Cosmos DB 中的 Cassandra API 默认情况下不会为所有特性编制索引。However, unlike the core SQL API, Cassandra API in Azure Cosmos DB does not index all attributes by default. 它支持使用辅助索引编制来为特定的特性创建索引,其行为方式与 Apache Cassandra 相同。Instead, it supports secondary indexing to create an index on certain attributes, which behaves the same way as Apache Cassandra.

一般情况下,不建议对未分区的列执行筛选查询。In general, it's not advised to execute filter queries on the columns that aren't partitioned. 必须显式使用 ALLOW FILTERING 语法,这可能会导致某个操作无法正常执行。You must use ALLOW FILTERING syntax explicitly, which results in an operation that may not perform well. 在 Azure Cosmos DB 中,可对低基数特征运行此类查询,因为这些查询会在不同的分区之间扇出以检索结果。In Azure Cosmos DB you can run such queries on low cardinality attributes because they fan out across partitions to retrieve the results.

不建议为经常更新的列创建索引。It's not advised to create an index on a frequently updated column. 在定义表时创建索引是明智之举。It is prudent to create an index when you define the table. 这可以确保数据和索引处于一致状态。This ensures that data and indexes are in a consistent state. 如果为现有数据创建新索引,目前,无法跟踪表的索引进度变化。In case you create a new index on the existing data, currently, you can't track the index progress change for the table. 如果需要跟踪此操作的进度,必须通过支持票证来请求进度变化。If you need to track the progress for this operation, you have to request the progress change via a support ticket.

备注

以下对象不支持辅助索引:Secondary index is not supported on the following objects:

  • 冻结的集合类型、小数和变体类型等数据类型。data types such as frozen collection types, decimal, and variant types.
  • 静态列Static columns
  • 聚集键Clustering keys

索引编制示例Indexing example

首先,在 CQL shell 提示符下运行以下命令以创建一个示例键空间和表:First, create a sample keyspace and table by running the following commands on the CQL shell prompt:

CREATE KEYSPACE sampleks WITH REPLICATION = {'class' : 'SimpleStrategy'};
CREATE TABLE sampleks.t1(user_id int PRIMARY KEY, lastname text) WITH cosmosdb_provisioned_throughput=400;

然后,使用以下命令插入示例用户数据:Then, insert sample user data with the following commands:

insert into sampleks.t1(user_id,lastname) values (1, 'nishu');
insert into sampleks.t1(user_id,lastname) values (2, 'vinod');
insert into sampleks.t1(user_id,lastname) values (3, 'bat');
insert into sampleks.t1(user_id,lastname) values (5, 'vivek');
insert into sampleks.t1(user_id,lastname) values (6, 'siddhesh');
insert into sampleks.t1(user_id,lastname) values (7, 'akash');
insert into sampleks.t1(user_id,lastname) values (8, 'Theo');
insert into sampleks.t1(user_id,lastname) values (9, 'jagan');

如果尝试执行以下语句,将会遇到错误,要求你使用 ALLOW FILTERINGIf you try executing the following statement, you will run into an error that asks you to use ALLOW FILTERING:

select user_id, lastname from sampleks.t1 where lastname='nishu';

尽管 Cassandra API 支持上一部分中所述的 ALLOW FILTERING,但不建议使用此语法。Although the Cassandra API supports ALLOW FILTERING, as mentioned in the previous section, it's not recommended. 应按以下示例中所示创建索引:You should instead create an index in the as shown in the following example:

CREATE INDEX ON sampleks.t1 (lastname);

对“lastname”字段创建索引后,接下来可以成功运行以前的查询。After creating an index on the "lastname" field, you can now run the previous query successfully. 使用 Azure Cosmos DB 中的 Cassandra API 时,无需提供索引名称。With Cassandra API in Azure Cosmos DB, you do not have to provide an index name. 将使用格式为 tablename_columnname_idx 的默认索引。A default index with format tablename_columnname_idx is used. 例如, t1_lastname_idx 是上述表的索引名称。For example, t1_lastname_idx is the index name for the previous table.

删除索引Dropping the index

要删除某个索引,需要知道其名称。You need to know what the index name is to drop the index. 运行 desc schema 命令来获取表的说明。Run the desc schema command to get the description of your table. 此命令的输出包含 CREATE INDEX tablename_columnname_idx ON keyspacename.tablename(columnname) 格式的索引名称。The output of this command includes the index name in the format CREATE INDEX tablename_columnname_idx ON keyspacename.tablename(columnname). 然后,可以使用索引名称删除该索引,如以下示例中所示:You can then use the index name to drop the index as shown in the following example:

drop index sampleks.t1_lastname_idx;

后续步骤Next steps