Cassandra允许过滤

标签 cassandra datastax cql datastax-enterprise

我有一张 table ,如下

CREATE TABLE test (
 day int,
 id varchar,  
 start int,
 action varchar,  
 PRIMARY KEY((day),start,id)
);

我想运行此查询
Select * from test where day=1 and start > 1475485412 and start < 1485785654 
and action='accept' ALLOW FILTERING

这个可以过滤高效吗?

我希望cassandra会按此顺序过滤
1. By Partitioning column(day)
2. By the range column(start) on the 1's result
3. By action column on 2's result. 

因此,在此查询上,允许过滤将不是一个坏选择。

如果where子句上有多个过滤参数,而最后一个未索引的列是过滤参数,那么该过滤器将如何工作?
请解释。

最佳答案

Is this ALLOW FILTERING efficient?



当您写“this”时,是指在查询和模型的上下文中,但是ALLOW FILTERING查询的效率主要取决于它必须过滤的数据。除非您显示一些真实数据,否则这是一个很难回答的问题。

I am expecting that cassandra will filter in this order...



是的,这将会发生。但是,在查询中包含ALLOW FILTERING子句通常意味着表设计不佳,也就是说,您没有遵循有关Cassandra建模的一些准则(特别是“一个查询<->一张表”)。

作为一种解决方案,我可以提示您在action字段之前的聚类键中包括start字段,从而修改表定义:
CREATE TABLE test (
 day int,
 id varchar,  
 start int,
 action varchar,  
 PRIMARY KEY((day),action,start,id)
);

然后,您将在没有任何ALLOW FILTERING子句的情况下重写查询:
SELECT * FROM test WHERE day=1 AND action='accept' AND start > 1475485412 AND start < 1485785654

只有一个小问题:如果一条记录“切换”了action值,您将无法在单个action字段上执行更新(因为它现在是集群键的一部分),因此您需要使用旧的action值执行删除操作,并执行插入操作具有正确的新值。但是,如果您拥有Cassandra 3.0+,则可以在新的Materialized View实现的帮助下完成所有这些操作。有一个look at the documentation以获取更多信息。

关于Cassandra允许过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42576201/

相关文章:

cassandra - 在压缩历史记录中,rows_merged是什么意思?

cassandra - Cassandra 中分层实体的建模

database - 在我的场景中,Cassandra (CQL) 架构/表看起来与 RDBMS 相同

Cassandra,改变replication_factor后的奇怪行为

cassandra - 即使使用 Quorum 配置,Cassandra 中的数据也不一致

cassandra - 如何在 Cassandra 中插入带有 TimeUUIDType 列的行?

cassandra - 比较 Cassandra 结构与关系数据库

cassandra - 类型错误 : not all arguments converted during string formatting in CQL

java - 使用 Datastax Java 驱动程序返回 JSON

java - 无法连接到远程 cassandra