获取时间序列 table 中的最后一行?

Get last row in table of time series?

我已经能够得到时间序列的最后一行 table 作为:

SELECT * from myapp.locations WHERE organization_id=1 and user_id=15 and date='2017-2-22'  ORDER BY unix_time DESC LIMIT 1;

效果很好,但是,我想知道执行 ORDER BY 的性能和开销,因为行已经排序,我只是用它来获取最后一行,这对我来说是开销吗?

如果我不使用 ORDER BY,我将始终获得 table 中的第一行,所以,我虽然可以用另一种方式使用 INSERT ,例如:总是在 table?

的开头而不是结尾插入

有什么建议吗?我应该使用 ORDER BY 而不必担心性能吗?

只需将集群键顺序定义为 DESC

喜欢下面的架构:

CREATE TABLE locations (
    organization_id int,
    user_id int,
    date text,
    unix_time bigint,
    lat double,
    long double,
    PRIMARY KEY ((organization_id, user_id, date), unix_time)
) WITH CLUSTERING ORDER BY (unix_time DESC); 

因此默认情况下您的数据将按 unix_time desc 排序,您无需在查询中指定
现在您可以使用下面的查询来获取最后一行:

SELECT * from myapp.locations WHERE organization_id = 1 and user_id = 15 and date = '2017-2-22' LIMIT 1;

如果您的 table 查询模式始终是 ORDER BY unix_time DESC 那么您处于 逆序时间序列 场景中,我可以这样说您的模型 不准确 (没有错)。

没有理由不通过在 table 定义中添加 WITH CLUSTERING ORDER BY unix_time DESC 来对记录进行倒序排序,在我看来 ORDER BY unix_time DESC 最多与明确针对这些用例的东西(好吧,我认为它会表现更差)。