Cassandra 在CQL中对分区键和(群集键或索引列)使用IN运算符
我有一张提醒表。我想在两列上使用IN运算符,在一列上使用大于运算符进行查询。我试过下面的东西,但运气不好。有人能告诉我数据库的设计使查询工作吗? 我的环境详细信息:[cqlsh 5.0.1 | Cassandra 2.1.2 | CQL规范3.2.0 |本机协议v3] 使用分区键中的“type”:Cassandra 在CQL中对分区键和(群集键或索引列)使用IN运算符,cassandra,cql,cassandra-2.0,cql3,Cassandra,Cql,Cassandra 2.0,Cql3,我有一张提醒表。我想在两列上使用IN运算符,在一列上使用大于运算符进行查询。我试过下面的东西,但运气不好。有人能告诉我数据库的设计使查询工作吗? 我的环境详细信息:[cqlsh 5.0.1 | Cassandra 2.1.2 | CQL规范3.2.0 |本机协议v3] 使用分区键中的“type”: CREATE TABLE alerts ( serialNumber text, time bigint, type text, time2 int, stat
CREATE TABLE alerts (
serialNumber text,
time bigint,
type text,
time2 int,
status text,
parentId int,
PRIMARY KEY ((serialNumber,type), time)
) WITH CLUSTERING ORDER BY (time DESC);
cqlsh:testdb> select * from alerts WHERE serialNumber IN ( '1','2') AND type IN ( '1','2','3' ) AND time > 1;
code=2200 [Invalid query] message="Partition KEY part serialNumber cannot be restricted by IN relation (only the last part of the partition key can)"
在群集键中使用“type”:
CREATE TABLE alerts (
serialNumber text,
time bigint,
type text,
time2 int,
status text,
parentId int,
PRIMARY KEY (serialNumber, type, time)
) WITH CLUSTERING ORDER BY (type ASC,time DESC);
cqlsh:testdb> select * from alerts WHERE serialnumber IN ( '1','2') AND type IN ( 'a','b') and time > 1;
code=2200 [Invalid query] message="Clustering column "type" cannot be restricted by an IN relation"
在类型上具有索引:
CREATE TABLE alerts (
serialNumber text,
time bigint,
type text,
time2 int,
status text,
parentId int,
PRIMARY KEY (serialNumber, time)
) WITH CLUSTERING ORDER BY (time DESC);
CREATE INDEX alertsTypeIndex ON alerts(type);
select * from alerts WHERE serialnumber IN ( '1','2') and time > 1 AND type IN ( 'a','b');
code=2200 [Invalid query] message="IN predicates on non-primary-key columns (type) is not yet supported"
select * from alerts WHERE serialnumber IN ( '1','2') and time > 1 AND type = 'a';
code=2200 [Invalid query] message="Select on indexed columns and with IN clause for the PRIMARY KEY are not supported"
你是哪个版本的?您的第二个示例在2.2.1中对我有效:
Connected to VaporTrails at 127.0.0.1:9042.
[cqlsh 5.0.1 | Cassandra 2.2.1 | CQL spec 3.3.0 | Native protocol v4]
Use HELP for help.
aploetz@cqlsh> use stackoverflow ;
aploetz@cqlsh:stackoverflow> CREATE TABLE alerts (
... serialNumber text,
... time bigint,
... type text,
... time2 int,
... status text,
... parentId int,
... PRIMARY KEY (serialNumber, type, time)
... ) WITH CLUSTERING ORDER BY (type ASC,time DESC);
aploetz@cqlsh:stackoverflow> INSERT INTO alerts (serialnumber , time,type,time2, status, parentid) VALUES ('1',0,'1',1,'1',1);
aploetz@cqlsh:stackoverflow> INSERT INTO alerts (serialnumber , time,type,time2, status, parentid) VALUES ('2',2,'2',2,'2',2);
aploetz@cqlsh:stackoverflow> INSERT INTO alerts (serialnumber , time,type,time2, status, parentid) VALUES ('3',3,'3',3,'3',3);
aploetz@cqlsh:stackoverflow> select * from alerts WHERE serialNumber IN ( '1','2') AND type IN ( '1','2','3' ) AND time > 1;
serialnumber | type | time | parentid | status | time2
--------------+------+------+----------+--------+-------
2 | 2 | 2 | 2 | 2 | 2
(1 rows)
即使你能让它工作,我也会反对它。在分区键上使用IN关键字称为“多键”查询反模式。数据税有其特殊性。本质上,这种方法不能扩展,因为必须查询许多节点才能满足这些类型的查询。您应该找到另一种方法对警报进行建模,这样您就不需要使用IN relation。我刚刚遇到了一个类似的问题,我想在Cassandra列上使用IN操作符,但希望确保它不会像@Aaron所描述的那样糟糕 查看文档,有很多关于它的解释: TLDR: 只有在查询键的前面所有列是否相等时,才建议在分区键的最后一列上使用IN条件
有关更多示例和问题,请访问上面的链接。谢谢@Aaron。我使用的是2.1.2。您能为我的提醒推荐一种新的型号吗?
CREATE TABLE parts (part_type text, part_name text, part_num int, part_year text, serial_num text, PRIMARY KEY ((part_type, part_name), part_num, part_year));
SELECT * FROM parts WHERE part_type='alloy' AND part_name='hubcap' AND part_num=1249 AND part_year IN ('2010', '2015');