Cassandra命令和聚类键

时间:2016-03-01 17:54:21

标签: database cassandra cql 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)
)

在这种情况下,当我执行此查询时,我会获得currentime排序的10000行:

SELECT * FROM events_by_websiteid_time WHERE websiteid='xxxx' LIMIT 10000 ALLOW FILTERING;

或者我最后必须添加WITH CLUSTERING ORDER BY (currentTime DESC);吗?

1 个答案:

答案 0 :(得分:4)

Cassandra只能在分区内强制执行排序。当您使用ALLOW FILTERING来避免提供分区键(event_id)时,您的结果集将按每个event_id的哈希标记值排序,然后按{{1}排序}和websiteid

要使结果按currentTime排序,您需要创建新的查询表或更改现有表的PRIMARY KEY定义(可能还有CLUSTERING ORDER)。如果您决定创建一个新的查询表,则必须看起来像这样:

currentTime

这将允许此查询:

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);

......按照你的期望工作。