cassandra - Using IN operator in CQL on partition key and (clustering key or indexed column) -
i have alerts table. want query on using in operator on 2 columns , using greater operator on 1 column. have tried below things no luck. can please tell me db design make query work? environment details: [cqlsh 5.0.1 | cassandra 2.1.2 | cql spec 3.2.0 | native protocol v3]
with 'type' in partition key:
create table alerts ( serialnumber text, time bigint, type text, time2 int, status text, parentid int, primary key ((serialnumber,type), time) ) clustering order (time desc); cqlsh:testdb> select * alerts serialnumber in ( '1','2') , type in ( '1','2','3' ) , time > 1; code=2200 [invalid query] message="partition key part serialnumber cannot restricted in relation (only last part of partition key can)" with 'type' in clustering key:
create table alerts ( serialnumber text, time bigint, type text, time2 int, status text, parentid int, primary key (serialnumber, type, time) ) clustering order (type asc,time desc); cqlsh:testdb> select * alerts serialnumber in ( '1','2') , type in ( 'a','b') , time > 1; code=2200 [invalid query] message="clustering column "type" cannot restricted in relation" with index on type:
create table alerts ( serialnumber text, time bigint, type text, time2 int, status text, parentid int, primary key (serialnumber, time) ) clustering order (time desc); create index alertstypeindex on alerts(type); select * alerts serialnumber in ( '1','2') , time > 1 , type in ( 'a','b'); code=2200 [invalid query] message="in predicates on non-primary-key columns (type) not yet supported" select * alerts serialnumber in ( '1','2') , time > 1 , type = 'a'; code=2200 [invalid query] message="select on indexed columns , in clause primary key not supported"
which version on? second example worked me in 2.2.1:
connected vaportrails @ 127.0.0.1:9042. [cqlsh 5.0.1 | cassandra 2.2.1 | cql spec 3.3.0 | native protocol v4] use 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) ... ) clustering order (type asc,time desc); aploetz@cqlsh:stackoverflow> insert alerts (serialnumber , time,type,time2, status, parentid) values ('1',0,'1',1,'1',1); aploetz@cqlsh:stackoverflow> insert alerts (serialnumber , time,type,time2, status, parentid) values ('2',2,'2',2,'2',2); aploetz@cqlsh:stackoverflow> insert alerts (serialnumber , time,type,time2, status, parentid) values ('3',3,'3',3,'3',3); aploetz@cqlsh:stackoverflow> select * alerts serialnumber in ( '1','2') , type in ( '1','2','3' ) , time > 1; serialnumber | type | time | parentid | status | time2 --------------+------+------+----------+--------+------- 2 | 2 | 2 | 2 | 2 | 2 (1 rows) even if can work, i'm going recommend against it. using in keyword on partition key known "multi-key" query anti-pattern. datastax has blurb in documentation discussing why bad. essentially, approach doesn't scale because many nodes must queried satisfy these types of queries. should find way model alerts not need use in relation.
Comments
Post a Comment