|
| 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: kanshiG |
| 5 | +ms.service: cosmos-db |
| 6 | +ms.topic: conceptual |
| 7 | +ms.date: 04/04/2020 |
| 8 | +ms.author: govindk |
| 9 | +ms.reviewer: sngun |
| 10 | + |
| 11 | +--- |
| 12 | + |
| 13 | +# Indexing in Azure Cosmos DB Cassandra API |
| 14 | + |
| 15 | +The Cassandra API in Azure Cosmos DB leverages the underlying Indexing infrastructure to expose the indexing strength that is inherent in the platform. Cassandra API in Azure Cosmos DB supports secondary indexing to create index on certain attributes. In general, it's not advised to use Apache Cassandra to execute filter queries on the columns that aren't partitioned. You must use ALLOW filter syntax explicitly which, results in a less performant operation. However, in Azure CosmosDB you can run such queries on low cardinality attributes because they fan out across partitions to retrieve the results. |
| 16 | + |
| 17 | +It's not advised to create index on a frequently updated column. It is prudent to create index when you define the table. This ensures that data and index 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 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). |
| 18 | + |
| 19 | + |
| 20 | +> [!NOTE] |
| 21 | +> Secondary index is not supported on the following objects: |
| 22 | +> * data types such as frozen collection types, decimal, and variant types. |
| 23 | +> * Static columns |
| 24 | +> * Clustering keys |
| 25 | +
|
| 26 | +## Create |
| 27 | + |
| 28 | +### Create a table |
| 29 | +Use the following commands to create a key space and a table: |
| 30 | + |
| 31 | +``` |
| 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 | +### Insert sample data |
| 37 | + |
| 38 | +Insert the following user data into the table: |
| 39 | + |
| 40 | +``` |
| 41 | +insert into sampleks.t1(user_id,lastname) values (1, 'Nishu'); |
| 42 | +insert into sampleks.t1(user_id,lastname) values (2, 'John'); |
| 43 | +insert into sampleks.t1(user_id,lastname) values (3, 'Bat'); |
| 44 | +insert into sampleks.t1(user_id,lastname) values (5, 'Vivek'); |
| 45 | +insert into sampleks.t1(user_id,lastname) values (6, 'Siddhesh'); |
| 46 | +insert into sampleks.t1(user_id,lastname) values (7, 'Akash'); |
| 47 | +insert into sampleks.t1(user_id,lastname) values (8, 'Theo'); |
| 48 | +insert into sampleks.t1(user_id,lastname) values (9, 'Jagan'); |
| 49 | +``` |
| 50 | + |
| 51 | +### Execute a query with filter on the non-partition key cluster |
| 52 | + |
| 53 | +If you try executing the following statement, you will run into an error that asks you to use allow filtering: |
| 54 | + |
| 55 | +``` |
| 56 | +select user_id, lastname from gks1 .t1 where last_name='Nishu'; |
| 57 | +``` |
| 58 | + |
| 59 | +### Create an index |
| 60 | + |
| 61 | +Create an index with the following command: |
| 62 | + |
| 63 | +``` |
| 64 | +CREATE INDEX ON sampleks.t1 (lastname) |
| 65 | +``` |
| 66 | + |
| 67 | +Next you can run queries with filter on the indexed field: |
| 68 | + |
| 69 | +``` |
| 70 | +select user_id, lastname from gks1.t1 where user_id=1; |
| 71 | +``` |
| 72 | + |
| 73 | +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. |
| 74 | + |
| 75 | +### Drop the index |
| 76 | + |
| 77 | +You need to know 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. You can then use the index name to drop the index as shown in the following example: |
| 78 | + |
| 79 | +``` |
| 80 | +desc schema; |
| 81 | +drop index sampleks.t1_lastname_idx; |
| 82 | +``` |
| 83 | + |
| 84 | +## Next steps |
0 commit comments