自动选择 time_bucket() 的比例时间,取决于选择的查询时间 window 和预期的点数

Automatically choose proportional time for time_bucket(), depends on selected time window of query and expected quantity of points

我在这类查询中使用 timescaleDB 特殊的 time_bucket() 方法。

SELECT time_bucket('**1 day**', time) AS datetime, temp.inside_t FROM temp where serial in ('77777777777', '77777777778')   and time > '2020-**02-03** 13:54:46.768000'  and time < '2020-**03-04** 13:53:46.768000'  GROUP BY datetime   ORDER BY datetime DESC;

SELECT time_bucket('**1 hour**', time) AS datetime, temp.inside_t FROM temp where serial in ('77777777777', '77777777778')   and time > '2020-**03-03** 13:54:46.768000'  and time < '2020-**03-04** 13:53:46.768000' GROUP BY datetime   ORDER BY datetime DESC;

取决于查询的时间window和预期点数,我应该手动计算合适的时间段(1天[=​​38 =],或 6 小时,共 15 分钟)

在我的例子中,不可能只测量查询时间 window 并发送预期的点数,例如我想收到 100 点

SELECT time_bucket(**100**) AS datetime, temp.inside_t FROM temp where serial in ('77777777777', '77777777778')   and time > '2020-02-03 13:54:46.768000'  and time < '2020-03-04 13:53:46.768000' GROUP BY datetime   ORDER BY datetime DESC;

或 time_bucket() 的一些合适的替代方案

我读了这个 article 但没有发现任何有趣的东西。

更新 什么 API 处理这个请求

简单的解决方案来自于了解如何使用 Grafana。必须减去除以想要的点数,所以确定你可以得到想要的点数的秒数。

    List get(long from, long until, long maxDataPoints) {
    final String query = String.format("SELECT time_bucket('%s seconds', time) AS dateTime, avg(temp.inside), avg(temp.outside) FROM temp where serial in ('777777777') and time > '2020-03-03 13:54:46.768000' and time < '2020-03-04 13:54:46.768000' GROUP BY dateTime ORDER BY dateTime DESC;",
        round((until - from) / maxDataPoints));
    return jdbcTemplate.query(query, new RowMapper());

顺便说一句,这是另一个有用的 method