即使列是聚类键,Cassandra也会要求允许过滤

时间:2016-07-13 11:37:10

标签: cassandra

对于Cassandra来说很新,如果问题很简单,请道歉。

我创建了一个表:

create table ApiLog (
LogId uuid,     
DateCreated timestamp,
ClientIpAddress varchar,
primary key (LogId, DateCreated));

这项工作很好:

select * from apilog

如果我尝试使用DateCreated添加where子句,如下所示:

select * from apilog where datecreated <= '2016-07-14'

我明白了:

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

从SO上的其他问题以及关于datastax的教程我的理解是,由于datecreated列是一个聚类键,它可以用来过滤数据。

我也尝试创建一个索引但是我收到了相同的消息。我试图从主键中删除DateCreated并将其仅作为索引,我仍然得到相同的回复:

create index ApiLog_DateCreated on dotnetdemo.apilog (datecreated);

1 个答案:

答案 0 :(得分:8)

分区键LogId确定每个分区将存储在哪个节点上。因此,如果您未指定分区键,则Cassandra必须在所有节点上过滤此表的所有分区以查找匹配数据。这就是为什么你不得不说允许过滤,因为这种操作非常低效并且不鼓励。

如果您指定了特定的LogId,那么Cassandra可以在单个节点上找到该分区,并通过群集密钥有效地进行范围查询。

因此,您需要规划您的架构,以便您可以在单个分区中执行范围查询,而不必像您尝试那样进行全表扫描。