Skip to content

Commit 80b0f90

Browse files
authored
Merge pull request #110359 from TheovanKraay/secondary-indexing-cassandra
cassandra secondary indexes initial commit
2 parents 1d67916 + 8d802ea commit 80b0f90

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

articles/cosmos-db/TOC.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,8 @@
597597
items:
598598
- name: Elastic scale
599599
href: manage-scale-cassandra.md
600+
- name: Secondary Indexes
601+
href: cassandra-secondary-index.md
600602
- name: How-to guides
601603
items:
602604
- name: Change feed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
---
2+
title: Indexing in Azure Cosmos DB Cassandra API account
3+
description: Learn how secondary indexing works in Azure Azure Cosmos DB Cassandra API account.
4+
author: TheovanKraay
5+
ms.service: cosmos-db
6+
ms.topic: conceptual
7+
ms.date: 04/04/2020
8+
ms.author: thvankra
9+
ms.reviewer: sngun
10+
---
11+
12+
# Secondary indexing in Azure Cosmos DB Cassandra API
13+
14+
The Cassandra API in Azure Cosmos DB leverages the underlying indexing infrastructure to expose the indexing strength that is inherent in the platform. However, unlike the core SQL API, Cassandra API in Azure Cosmos DB does not index all attributes by default. Instead, it supports secondary indexing to create an index on certain attributes, which behaves the same way as Apache Cassandra.
15+
16+
In general, it's not advised to execute filter queries on the columns that aren't partitioned. You must use ALLOW FILTERING syntax explicitly, which results in an operation that may not perform well. In Azure Cosmos DB you can run such queries on low cardinality attributes because they fan out across partitions to retrieve the results.
17+
18+
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]( https://docs.microsoft.com/azure/azure-portal/supportability/how-to-create-azure-support-request).
19+
20+
21+
> [!NOTE]
22+
> Secondary index is not supported on the following objects:
23+
> - data types such as frozen collection types, decimal, and variant types.
24+
> - Static columns
25+
> - Clustering keys
26+
27+
## Indexing example
28+
29+
First, create a sample keyspace and table by running the following commands on the CQL shell prompt:
30+
31+
```shell
32+
CREATE KEYSPACE sampleks WITH REPLICATION = {'class' : 'SimpleStrategy'};
33+
CREATE TABLE sampleks.t1(user_id int PRIMARY KEY, lastname text) WITH cosmosdb_provisioned_throughput=400;
34+
```
35+
36+
Then, insert sample user data with the following commands:
37+
38+
```shell
39+
insert into sampleks.t1(user_id,lastname) values (1, 'nishu');
40+
insert into sampleks.t1(user_id,lastname) values (2, 'vinod');
41+
insert into sampleks.t1(user_id,lastname) values (3, 'bat');
42+
insert into sampleks.t1(user_id,lastname) values (5, 'vivek');
43+
insert into sampleks.t1(user_id,lastname) values (6, 'siddhesh');
44+
insert into sampleks.t1(user_id,lastname) values (7, 'akash');
45+
insert into sampleks.t1(user_id,lastname) values (8, 'Theo');
46+
insert into sampleks.t1(user_id,lastname) values (9, 'jagan');
47+
```
48+
49+
If you try executing the following statement, you will run into an error that asks you to use `ALLOW FILTERING`:
50+
51+
```shell
52+
select user_id, lastname from sampleks.t1 where lastname='nishu';
53+
```
54+
55+
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:
56+
57+
```shell
58+
CREATE INDEX ON sampleks.t1 (lastname);
59+
```
60+
After creating an index on the "lastname" field, you can now run the previous query successfully. With Cassandra API in Azure Cosmos DB, you do not have to provide an index name. A default index with format `tablename_columnname_idx` is used. For example, ` t1_lastname_idx` is the index name for the previous table.
61+
62+
## Dropping the index
63+
You need to know what the index name is to drop the index. Run the `desc schema` command to get the description of your table. 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:
64+
65+
```shell
66+
drop index sampleks.t1_lastname_idx;
67+
```
68+
69+
## Next steps
70+
* Learn how [automatic indexing](index-overview.md) works in Azure Cosmos DB
71+
* [Apache Cassandra features supported by Azure Cosmos DB Cassandra API](cassandra-support.md)

0 commit comments

Comments
 (0)