具有多个集合值的 Cassandra CQL where 子句?

标签 cassandra cql cql3 cassandra-2.1

我的数据模型:-

tid                                  | codes        | raw          | type
-------------------------------------+--------------+--------------+------
a64fdd60-1bc4-11e5-9b30-3dca08b6a366 | {12, 34, 53} | {sdafb=safd} |  cmd

CREATE TABLE MyTable (
tid       TIMEUUID,
type      TEXT,
codes     SET<INT>,
raw       TEXT,
PRIMARY KEY (tid)
);
CREATE INDEX ON myTable (codes);

如何根据多个设置值查询表以返回行。

这个有效:-

select * from logData where codes contains 34;

但我想根据多个设置值获取行,但这些都不起作用:-

select * from logData where codes contains 34, 12; or 
select * from logData where codes contains 34 and 12; or
select * from logData where codes contains {34, 12};

请多多指教。

最佳答案

如果我创建您的表结构并插入与上面类似的行,我可以检查 codes 集合中的多个值,如下所示:

aploetz@cqlsh:stackoverflow2> SELECT * FROM mytable 
    WHERE codes CONTAINS 34 
      AND codes CONTAINS 12
      ALLOW FILTERING;

 tid                                  | codes        | raw          | type
--------------------------------------+--------------+--------------+------
 2569f270-1c06-11e5-92f0-21b264d4c94d | {12, 34, 53} | {sdafb=safd} |  cmd

(1 rows)

正如其他人所提到的,让我告诉您为什么这是一个糟糕的想法...

在集合上使用二级索引(并且基数似乎相当高)每个节点都必须针对每个查询进行检查。 Cassandra 的想法是尽可能频繁地按分区键进行查询,这样每次查询只需命中一个节点。 Apple 的 Richard Low 写了一篇很棒的文章,名为 The sweet spot for Cassandra secondary indexes .它应该让您重新思考您使用二级索引的方式。

其次,我能让 Cassandra 接受这个查询的唯一方法是使用 ALLOW FILTERING .这意味着,Cassandra 可以应用所有过滤条件(WHERE 子句)的唯一方法是拉回每一行并单独过滤掉不符合条件的行。效率极低。需要明确的是,ALLOW FILTERING 指令是您应该永远使用的东西。

在任何情况下,如果 codes 是您需要查询的内容,那么您应该设计一个额外的查询表,将 codes 作为 PRIMARY KEY 的一部分.

关于具有多个集合值的 Cassandra CQL where 子句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31066477/

相关文章:

database - 与 CQRS 结合时,哪对 DBMS 提供最快的读/写?

scala - 如何从spark将文件写入cassandra

cassandra - 获取 Cassandra 的日期范围 - 选择 timeuuid,IN 返回 0 行

python - Cassandra 十进制精度问题

cassandra - ScyllaDB - [无效查询] 消息 ="Collection filtering is not supported yet"

Cassandra : DELETE doesn't work

cassandra - 如果数据在 Cassandra 中没有像 MySQL RAND() 那样改变,有没有办法每次都获得随机行

Cassandra CQL3 条件插入/更新

cassandra - 如何从 Cassandra key 空间中删除所有表和 UDT?

python - 如何使用cql引擎将图像存储为cassandra数据库中的字节字段?