Bringing together the Apache Cassandra experts from the community and DataStax.

Want to learn? Have a question? Want to share your expertise? You are in the right place!

Not sure where to begin? Getting Started

 

question

harshv avatar image
harshv asked ·

Can I use a secondary index in a query without using the partition key?

I am confused after reading about index in DataStax documents at following 2 places:

Place1: How are indexes stored and updated?

https://docs.datastax.com/en/cassandra-oss/3.0/cassandra/dml/dmlIndexInternals.html
If a secondary index is used in a query that is not restricted to a particular partition key, the query will have prohibitive read latency because all nodes will be queried. A query with these parameters is only allowed if the query option ALLOW FILTERING is used.  This option is not appropriate for production environments 

Place2: Using a secondary index

https://docs.datastax.com/en/cql-oss/3.3/cql/cql_using/useSecondaryIndex.html

cqlsh> CREATE INDEX rrank ON cycling.rank_by_year_and_name (rank);
SELECT * FROM cycling.rank_by_year_and_name WHERE rank = 1;

Here on this page, there are of examples about executing a query with only secondary index attribute and doesn't include partition key.

Main question: Can i use secondary index in a query without using partition key fields ?

Side Question: Which explanation is correct (place1 or place2) ?

secondary index
10 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

1 Answer

Erick Ramirez avatar image
Erick Ramirez answered ·

Yes, you can run queries which only use the secondary index as a filter. But be aware that it is expensive since all nodes in the cluster need to be contacted.

If you limit the query to search within a partition, only the relevant replica(s) will be contacted by the coordinator. However, it is not necessary to specify the partition key when querying secondary indexes. Cheers!

Share
10 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.