【问题标题】:Retrieving the row with the greatest timestamp in questDB检索 questDB 中时间戳最大的行
【发布时间】:2021-12-09 17:37:36
【问题描述】:

我目前在 Linux 上运行 QuestDB 6.1.2。如何从表中获取具有最大值的行?我在大约 500 万行的测试表上尝试了以下操作:

  1. select * from table where cast(timestamp as symbol) in (select cast(max(timestamp) as symbol) from table );
  2. select * from table inner join (select max(timestamp) mm from table ) on timestamp >= mm
  3. select * from table where timestamp = max(timestamp)
  4. select * from table where timestamp = (select max(timestamp) from table )

其中 1 正确但运行时间约为 5 秒,2 正确且运行时间约为 500 毫秒,但查询看起来不必要地冗长,3 编译但返回一个空表,4 语法不正确,尽管 sql 通常是这样做的

【问题讨论】:

    标签: questdb


    【解决方案1】:

    select * from table limit -1 有效。 QuestDB 默认返回按时间戳排序的行,limit -1 取最后一行,恰好是时间戳最大的行。要明确按时间戳排序,可以使用 select * from table order by timestamp limit -1 代替。此查询在同一张表上运行大约 300-400 毫秒。

    附带说明一下,使用 timestamp=max(timestamp) 的第三个查询还不起作用,因为 QuestDB 尚不支持 where 中的子查询(questDB 6.1.2)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-04-08
      • 2022-07-06
      • 1970-01-01
      • 2018-02-18
      • 2015-07-27
      • 2013-06-20
      • 1970-01-01
      相关资源
      最近更新 更多