8

短版:是否可以查询与特定日期对应的所有 timeuuid 列?

更多细节:

我有一个定义如下的表:

CREATE TABLE timetest(
  key uuid,
  activation_time timeuuid,
  value text,
  PRIMARY KEY(key,activation_time)
);

我已经用单行填充了它,如下(f0532ef0-2a15-11e3-b292-51843b245f21是与日期对应的 timeuuid 2013-09-30 22:19:06+0100):

insert into timetest (key, activation_time, value) VALUES (7daecb80-29b0-11e3-92ec-e291eb9d325e, f0532ef0-2a15-11e3-b292-51843b245f21, 'some value'); 

我可以按如下方式查询该行:

select activation_time,dateof(activation_time) from timetest where key=7daecb80-29b0-11e3-92ec-e291eb9d325e

结果如下(使用cqlsh)

 activation_time                      | dateof(activation_time)
--------------------------------------+--------------------------
 f0532ef0-2a15-11e3-b292-51843b245f21 | 2013-09-30 22:19:06+0100

现在让我们假设我的表中有很多数据,我想检索activation_time与特定日期对应的所有行,比如2013-09-30 22:19:06+0100.

我本来希望能够查询 和 之间的所有 timeuuid 的范围,minTimeuuid('2013-09-30 22:19:06+0100')但这maxTimeuuid('2013-09-30 22:19:06+0100')似乎不可能(以下查询返回零行):

select * from timetest where key=7daecb80-29b0-11e3-92ec-e291eb9d325e and activation_time>minTimeuuid('2013-09-30 22:19:06+0100') and activation_time<=maxTimeuuid('2013-09-30 22:19:06+0100'); 

看来我需要使用 hack 来增加查询中的第二个日期(增加一秒)来捕获行,即

select * from timetest where key=7daecb80-29b0-11e3-92ec-e291eb9d325e and activation_time>minTimeuuid('2013-09-30 22:19:06+0100') and activation_time<=maxTimeuuid('2013-09-30 22:19:07+0100');

这感觉不对。我错过了什么吗?有没有更清洁的方法来做到这一点?

CQL 文档讨论了timeuuid 函数,但它对带有 timeuuids 的 gte/lte 表达式非常短,除此之外:

min/maxTimeuuid 示例选择 timeuuid 列 t 严格晚于 2013-01-01 00:05+0000 但严格早于 2013-02-02 10:00+0000 的所有行。t >= maxTimeuuid('2013-01-01 00:05+0000') 不会选择恰好在 2013-01-01 00:05+0000 生成的 timeuuid,本质上等同于 t > maxTimeuuid('2013-01 -01 00:05+0000')。

ps 以下查询也返回零行:

select * from timetest where key=7daecb80-29b0-11e3-92ec-e291eb9d325e and activation_time<=maxTimeuuid('2013-09-30 22:19:06+0100'); 

以下查询返回行:

select * from timetest where key=7daecb80-29b0-11e3-92ec-e291eb9d325e and activation_time>minTimeuuid('2013-09-30 22:19:06+0100');
4

2 回答 2

9

我确定问题在于 cqlsh 不会为您的时间戳显示毫秒所以真正的时间戳类似于 '2013-09-30 22:19:06.123+0100' 当您调用 maxTimeuuid('2013-09-30 22: 19:06+0100') 因为缺少毫秒,假定为零,因此它与调用 maxTimeuuid('2013-09-30 22:19:06.000+0100') 相同

由于 22:19:06.123 > 22:19:06.000 导致记录被过滤掉。

于 2013-11-21T18:04:57.583 回答
0

与答案没有直接关系,但作为@dimas 答案的附加插件。
cqlsh(版本 5.0.1)现在似乎显示毫秒

 system.dateof(id)  
---------------------------------
 2016-06-03 02:42:09.990000+0000
 2016-05-28 17:07:30.244000+0000
于 2016-06-07T15:58:57.897 回答