far*_*awa 5 database cql cassandra cql3
我有这张桌子:
CREATE TABLE custumer_events_service.events_by_websiteId_time(
"event_id" text,
"currentTime" timestamp,
"websiteId" varchar,
OTHER COLUMNS ...
PRIMARY KEY(event_id, websiteId, currentTime)
)
Run Code Online (Sandbox Code Playgroud)
在这种情况下,currentime当我执行这个查询时,我会得到 10000 行的排序:
SELECT * FROM events_by_websiteid_time WHERE websiteid='xxxx' LIMIT 10000 ALLOW FILTERING;
Run Code Online (Sandbox Code Playgroud)
还是我必须WITH CLUSTERING ORDER BY (currentTime DESC);在最后添加?
Cassandra 只能在分区内强制执行排序顺序。当您使用ALLOW FILTERING以避免必须提供分区键 ( event_id) 时,您的结果集将按每个的散列标记值排序event_id,然后按websiteid和排序currentTime。
为了让您的结果按 排序currentTime,您需要创建一个新的查询表或更改现有表的 PRIMARY KEY 定义(可能还有 CLUSTERING ORDER)。如果您决定创建一个新的查询表,它必须如下所示:
CREATE TABLE custumer_events_service.events_by_websiteId_time_eventid(
event_id text,
currentTime timestamp,
websiteId varchar,
OTHER COLUMNS ...
PRIMARY KEY (websiteid,currentTime,event_id))
WITH CLUSTERING ORDER BY (currentTime DESC, event_id ASC);
Run Code Online (Sandbox Code Playgroud)
这将允许此查询:
SELECT * FROM events_by_websiteid_time_eventid WHERE websiteid='xxxx' LIMIT 10000;
Run Code Online (Sandbox Code Playgroud)
...按您的预期工作。