在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以防止这种情况?
答案 0 :(得分:3)
您需要将其拆分为以下单独的查询:
select msg from log where id = 'A' and filter1 = 'filter';
和
select msg from log where id = 'B' and filter1 = 'filter';
由于数据在Cassandra中的分区方式,CQL有许多看似随意的限制(阻止低效查询,也因为它们实现起来很复杂)。
答案 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.