1
select host, time as currentTime, floor((1 - (
  select sum(measure_value::double)
    from "DendroTimestreamDB"."hostMetrics"
    where cpuMode = 'idle'
    and time <= h.time
    group by host
) / ((
  select sum(measure_value::double)
    from "DendroTimestreamDB"."hostMetrics"
    where cpuMode = 'idle'
    and time <= h.time
    group by host
) + (
  select sum(measure_value::double)
    from "DendroTimestreamDB"."hostMetrics"
    where cpuMode = 'system'
    and time <= h.time
    group by host
) + (
  select sum(measure_value::double)
    from "DendroTimestreamDB"."hostMetrics"
    where cpuMode = 'user'
    and time <= h.time
    group by host
))) * 100) as utilization
from "DendroTimestreamDB"."hostMetrics" h
group by host, time

返回错误line 20:3: Given correlated subquery is not supported但根据Timestream 子查询支持

Timestream 查询语言支持相关子查询和其他子查询。

列:

cpuMode, 主机, 设备, cpuCode, 收集器, measure_value::double, measure_name, time,

样本数据:

空闲,MacBook-Pro.local,-,0,cpu,115950.13,cpu_seconds_total,2021-04-29 13:46:11.000000000

期望的输出:

主机、时间、利用率

MacBook-Pro.local 2021-04-29 13:47:56.000000000 15

MacBook-Pro.local 2021-04-29 13:47:41.000000000 16

MacBook-Pro.local 2021-04-29 13:47:26.000000000 19

我正在尝试使用公式 (1 - idleTime / totalTime) * 100 计算 CPU 利用率,但显然不支持这些相关的子查询。我只需要以不同的方式重写它吗?

在求和子查询中,我试图计算在主查询之前收到的度量值的总和,我正在使用and time <= h.time导致查询相关的行来执行此操作,从而导致问题。

万分感谢

4

1 回答 1

1

我很确定你想要条件聚合。我无法真正遵循逻辑,但组件似乎是这样的:

select host, time as currentTime,
       sum(sum(case when cpuMode = 'idle' then measure_value::double)) over (partition by host order by time)
       sum(sum(case when cpuMode = 'system' then measure_value::double)) over (partition by host order by time)
       sum(sum(case when cpuMode = 'user' then measure_value::double)) over (partition by host order by time)
from "DendroTimestreamDB"."hostMetrics" h
group by host, time
于 2021-05-01T02:12:02.740 回答