Continuous Queries: Automating Time-Series Aggregation

A continuous query automatically aggregates real-time data on a schedule. Use it to create downsampled rollups, like hourly averages from raw sensor data, storing results in a new series.
Why it exists
Raw time-series data, especially from IoT devices or application metrics, can be incredibly high-volume. Querying this raw data directly for high-level trends, like daily averages for a dashboard, is inefficient and slow. Continuous queries solve this by pre-aggregating or "rolling up" the data into a new, smaller time series as it arrives, making subsequent analysis much faster.
The mental model
Think of a continuous query as a standing order you give your database. Instead of you manually running a GROUP BY query every hour to get the average temperature, you tell the database: "Every hour, run this aggregation on the last hour's data and save the result over there." It's a "set it and forget it" approach to data summarization that runs inside the database itself.
How it works
You define a continuous query using a CREATE CQ statement. This statement includes a unique ID, the query logic itself (SELECT, FROM, GROUP BY), and a destination series (INTO). The most critical part is the RESAMPLE clause, which controls the execution schedule and the time window of the data being queried. The EVERY parameter sets the execution frequency (e.g., every hour). The RANGE parameter defines the slice of time to query, relative to the execution time (e.g., now() minus 60 minutes to now()). The database scheduler then takes over, automatically executing this query at the specified interval and writing the result into the target time series. The system manages the time boundaries, which is why users cannot specify their own time filters in the WHERE clause.
When to use it
Use continuous queries for creating downsampled views of high-frequency data. This is essential for performance in monitoring dashboards, long-term trend analysis, and any scenario where you repeatedly query aggregated views of raw data. It's a core pattern for managing data lifecycle in time-series databases, moving from high-granularity raw data to lower-granularity summary data over time.
When not to use it
Avoid continuous queries for one-off data analysis or for queries that require complex, non-time-based filtering that changes frequently. It's not a replacement for ad-hoc querying. If you need real-time, per-event processing with complex logic, a more general stream processing engine like Flink or Spark Streaming might be a better fit than a database-centric CQ.
One canonical example
To create a new series temp_hourly_avg that stores the hourly average temperature from a raw series device1.temperature, you would create a continuous query. The query would be configured to run every hour (EVERY 1h). Each run would select the average temperature from the last hour (RANGE 60m) and insert that single value, timestamped, into the temp_hourly_avg series. This transforms millions of raw data points into just 24 summary points per day.
Interview question
What is the primary mechanism by which Continuous Queries improve performance for analyzing high-volume time-series data?
- a.They automatically pre-aggregate raw data into smaller, summarized time series on a recurring schedule.Correct
- b.They enable real-time, event-level processing, reducing latency for immediate insights.
- c.They optimize direct queries against raw, high-granularity data by indexing all data points.
- d.They allow dynamic, ad-hoc filtering on non-time-based dimensions, optimizing flexible queries.
Why? this is the answer
Continuous Queries enhance performance by automatically pre-aggregating high-volume raw data into smaller, downsampled time series on a defined schedule. This reduces the amount of data that subsequent analytical queries need to process, making them faster. Option C is incorrect because while indexing helps, the core mechanism of CQs is data reduction through aggregation, not just optimizing direct access to all raw data points.
Just read this? Test yourself on what you have been reading.
Read the original → iotdb.apache.org
- #time-series
- #stream processing
- #database
- #aggregation
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.
See open roles