“仅当分区键受EQ或IN限制时才支持ORDER BY。”卡桑德拉

时间:2019-08-14 05:09:32

标签: database-design cassandra

我已经这样创建了我的表。

CREATE TABLE test1.mytest (
    id text,
    year text,
    time bigint,
    crtby text,
    data text,
    postid text,
    upby text,
    PRIMARY KEY ((id, year), time)
) WITH CLUSTERING ORDER BY (time DESC)


我想根据时间键对数据进行排序。


SELECT * FROM test1.mytest WHERE id ='b' ORDER BY time asc ALLOW FILTERING ;

当我尝试上述命令时,我会得到

InvalidRequest: Error from server: code=2200 [Invalid query] message="ORDER BY is only supported when the partition key is restricted by an EQ or an IN."

1 个答案:

答案 0 :(得分:1)

该错误清楚地描述了失败的原因。您的主键是 id year ,然后在 time 键上进行进一步的聚类。

Cassandra需要知道 id year ,然后才能在时间之前下订单。

由于您并非总是在查询中使用分区键(id,年份),因此要按时间进行排序,因此必须使用物化视图。更多详细信息,请参见here