Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Querying Cassandra by a partial partition key

In Cassandra, I can create a composite partition key, separate from my clustering key:

CREATE TABLE footable (
    column1 text,
    column2 text,
    column3 text,
    column4 text,
    PRIMARY KEY ((column1, column2))
)

As I understand it, quering by partition key is an extremely efficient (the most efficient?) method for retrieving data. What I don't know, however, is whether it's also efficient to query by only part of a composite partition key.

In MSSQL, this would be efficient, as long as components are included starting with the first (column1 instead of column2, in this example). Is this also the case in Cassandra? Is it highly efficient to query for rows based only on column1, here?

like image 352
Mark Avatar asked Dec 03 '14 16:12

Mark


2 Answers

This is not the case in Cassandra, because it is not possible. Doing so will yield the following error:

Partition key part entity must be restricted since preceding part is

Check out this Cassandra 2014 SF Summit presentation from DataStax MVP Robbie Strickland titled "CQL Under the Hood." Slides 62-64 show that the complete partition key is used as the rowkey. With composite partitioning keys in Cassandra, you must query by all of the rowkey or none of it.

You can watch the complete presentation video here.

like image 147
Aaron Avatar answered Sep 22 '22 22:09

Aaron


This is impossible in Cassandra because it would require a full table scan to resolve such a query. The location of the partition is defined by a hash of all members of the composite key, this means giving only half of the key is as good as giving none of it. The only way to find the record is to search through all keys and check if they match.

like image 38
RussS Avatar answered Sep 22 '22 22:09

RussS