Are all values in a Primary Key Indexed?

87 Views Asked by At

Following a Tutorial on Cassandra, it was mentioned that if I do the following:

PRIMARY KEY(id, name) that id is the partition key and hence it is indexed. The name is the clustering column and hence it is also indexed. This means I can do a query such as:

SELECT * FROM my_table WHERE id = 'id_abc'; //this works!

I can also do a query such as:

SELECT * FROM my_table WHERE id = 'id_abc' AND name = 'name_123'; // this works!

However, I cannot do the following query:

SELECT * FROM my_table WHERE name = 'name_123'; // this does not work 

Why does the last statement not work if the clustering column is indexed? Why does the first query work and not the second?

The error I get for the last query is the following:

InvalidRequest: code=2200 [Invalid query] message="Cannot execute this query as it might involve data filtering and thus may have unpredictable performance. If you want to execute this query despite the performance unpredictability, use ALLOW FILTERING"

Thanks in advance!

1

There are 1 best solutions below

0
On BEST ANSWER

Just because it is named primary key there is no index on it in cassandra. ìd is your partition key - it defines which node in cassandra is responsible for your id. The clustering column name defines the order inside the partition.

Therefore SELECT * FROM my_table WHERE name = 'name_123'; // this does not work whould require all partitions to be scanned, which cassandra by default refuses.