cassandra Key ordering and allowed queries


Example

The partition key is the minimum specifier needed to perform a query using a where clause.

If you declare a composite clustering key, the order matters.

Say you have the following primary key:

PRIMARY KEY((part_key1, part_key_2), (clust_key_1, clust_key_2, clust_key_3))

Then, the only valid queries use the following fields in the where clause:

  • part_key_1, part_key_2
  • part_key_1, part_key_2, clust_key_1
  • part_key_1, part_key_2, clust_key_1, clust_key_2
  • part_key_1, part_key_2, clust_key_1, clust_key_2, clust_key_3

Example of invalid queries are:

  • part_key_1, part_key_2, clust_key_2
  • Anything that does not contain both part_key_1, part_key_2
  • ...

If you want to use clust_key_2, you have to also specify clust_key_1, and so on.

So the order in which you declare your clustering keys will have an impact on the type of queries you can do. In the opposite, the order of the partition key fields is not important, since you always have to specify all of them in a query.