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

leo*_*ame 2 cql cassandra 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);
Run Code Online (Sandbox Code Playgroud)

如何查询表以基于多个设置值返回行.

这有效: -

select * from logData where codes contains 34;
Run Code Online (Sandbox Code Playgroud)

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

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};
Run Code Online (Sandbox Code Playgroud)

请帮助.

Aar*_*ron 5

如果我创建表结构并在上面插入类似的行,我可以检查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)
Run Code Online (Sandbox Code Playgroud)

现在正如其他人所说,让我告诉你为什么这是一个可怕的想法 ......

如果集合上有二级索引(并且基数看起来相当高),则必须为每个查询检查每个节点.Cassandra的想法是尽可能经常地通过分区键进行查询,这样你只需要为每个查询命中一个节点.Apple的Richard Low撰写了一篇名为The Cassandra二级索引的精彩文章.它应该让你重新思考使用二级索引的方式.

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

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