cassandra在索引列上选择并且不支持PRIMARY KEY的IN子句

时间:2015-09-14 06:57:35

标签: cassandra cql cql3

在Cassandra,我正在使用cql:

select msg from log where id in ('A', 'B') and filter1 = 'filter' 

(其中id是分区键,filter1是辅助索引,filter1不能用作群集列。

这给出了回复:

Select on indexed columns and with IN clause for the PRIMARY KEY are not supported

如何更改CQL以防止这种情况?

2 个答案:

答案 0 :(得分:3)

您需要将其拆分为以下单独的查询:

select msg from log where id = 'A' and filter1 = 'filter';

select msg from log where id = 'B' and filter1 = 'filter';

由于数据在Cassandra中的分区方式,CQL有许多看似随意的限制(阻止低效查询,也因为它们实现起来很复杂)。

随着时间的推移,我认为这些限制将慢慢消除,但现在我们必须解决它们。有关限制的更多详细信息,请参阅enter image description here

答案 1 :(得分:2)

Another option, is that you could build a table specifically for this query (a query table) with filter1 as a partition key and id as a clustering key. That way, your query works and you avoid having a secondary index all-together.

aploetz@cqlsh:stackoverflow> CREATE TABLE log 
    (filter1 text, 
          id text, 
         msg text, 
     PRIMARY KEY (filter1, id));
aploetz@cqlsh:stackoverflow> INSERT INTO log (filter1, id, msg) 
                             VALUES ('filter','A','message A');
aploetz@cqlsh:stackoverflow> INSERT INTO log (filter1, id, msg)
                             VALUES ('filter','B','message B');
aploetz@cqlsh:stackoverflow> INSERT INTO log (filter1, id, msg) 
                             VALUES ('filter','C','message C');
aploetz@cqlsh:stackoverflow> SELECT msg FROM log 
                             WHERE filter1='filter' AND id IN ('A','B');

 msg
-----------
 message A
 message B

(2 rows)

You would still be using an "IN" which isn't known to perform well either. But you would also be specifying a partition key, so it might perform better than expected.