我刚看了this youtube video of Patrick McFadin关于 cassandra 数据建模。
有一张 table ,如下:
create table user_activity_history {
username varchar,
interaction_date varchar,
activity_code varchar,
detail varchar,
PRIMARY KEY((username,interaction_date),interaction_time)
);
为什么是主键
((username,interaction_date),interaction_time)
.这与
(username,interaction_date,interaction_time)
有何不同.
最佳答案
区别与表的partition_key
有关.通常,PRIMARY KEY 中的第一个元素也是分区键 - 这定义了数据在集群中的物理位置,例如,通过使用以下内容:
PRIMARY KEY(username,interaction_date,interaction_time)
插入表中的数据将根据
username
进行分区(物理定位) ,而通过使用以下内容:PRIMARY KEY((username,interaction_date),interaction_time)
它将根据
username,interaction_date
进行分区组合。后一种方案的优点是与单个 username
相关的数据。可以跨集群中的节点存储。在 CREATE TABLE 上的 datastax 的 CQL 文档中有关于 partition_keys 的更多详细信息:
When you use a compound PRIMARY KEY Cassandra treats the first column declared in a definition as the partition key and stores all columns of the row on the same physical node. When you use a composite partition key, Cassandra treats the columns in nested parentheses as partition keys and stores columns of a row on more than one node. You declare a composite partition key using an extra set of parentheses to define which columns partition the data.
关于cassandra - 了解 Cassandra 复合键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19557188/