In my database (Postgresql 12; timescaleDB 1.7.0) there are multiple metrics tables containing one row per minute and device. It contains a deviceId, the time, four doubles and an enum value.
There are different time based queries to analyze the data such as drawing a graph on 12h slices of it or selecting the aggregated status of the last 5m.
To improve query performance I set up timescale's continuous aggregate views for the 12h case which drastically improved query time because everything is being precalculated. I tried the same for the much smaller slices of 5m expecting an improvement as the data for each query will be much smaller, albeit not as drastic as in the 12h example. Surprisingly, the opposite was the case. Selecting the raw data is now much faster than selecting the aggregate view which I don't really understand.
Here's the definition of my view:
CREATE VIEW metric_5m
WITH ( timescaledb.continuous,
timescaledb.refresh_interval = '5 minutes' )
AS
SELECT device,
time_bucket('5 minutes', time) as "time_bucket",
max(metric.maximum) as "maximum",
min(metric.minimum) as "minimum",
avg(metric.average) as "average",
avg(metric.sd) as "sd"
FROM metric
GROUP BY time_bucket, device;
Selecting the raw data (in my test setup ~3.6 million rows) takes around 300ms while selecting the view takes around 3500ms. I suspect I'm somehow using it wrong or with too small intervals because it performs pretty well for the 12h example, but I couldn't find why.
So, thankful for every help on this!