如何按两列过滤 Cassandra 行

How to filter Cassandra rows by two columns

我有以下table

CREATE TABLE detections.events (
    id UUID,
    event_type INT,
    created_time TIMESTAMP
    PRIMARY KEY ( id, event_type, created_time )
) WITH CLUSTERING ORDER BY ( created_time DESC );

和 2 个查询:

SELECT * FROM detections.events WHERE id=?

SELECT * FROM detections.events WHERE event_type=?

但是,显然,如果我这样做的话,我无法像那样设置 table PRIMARY KEY ( id, created_time ) 我可以通过 id 查询,但不能通过 event_type

如果我这样做 PRIMARY KEY ( event_type, created_time ) 我可以通过 event_type 查询,但是不能通过 id

有一种方法可以让我的 table 能够完成这两个查询而无需创建新的 table 并且仍然按 created_time?

订购

我刚刚使用

解决了这个问题

PRIMARY KEY ( id, created_time )

CREATE INDEX event_type_idx ON detections.events( event_type );

更多信息:https://docs.datastax.com/en/cql-oss/3.3/cql/cql_using/useMultIndexes.html