Using InfluxDB Continuous Queries to Downsample Time‑Series Data Safely
Learn how InfluxDB continuous queries automate downsampling, keep raw data intact, and improve query performance—complete with a step‑by‑step example and practical verification tips.
18 May 2026, 12:09 UTC

The problem: raw high‑resolution data slows down queries
When you store metrics at native resolution (e.g., one‑second CPU samples), dashboards that look at weeks or months must scan millions of points. This increases query latency and puts unnecessary load on the storage engine.
Thesis: continuous queries automate downsampling while keeping raw data intact
InfluxDB continuous queries (CQs) run on a schedule, aggregate raw data according to a user‑defined function, and write the results into a target measurement (often in a different retention policy). The raw series stay untouched for detailed analysis, and the downsampled series serve fast‑query workloads.
How continuous queries work
A CQ consists of three parts:
- Source measurement – the raw data you want to aggregate.
- Aggregation function – e.g.,
MEAN(),SUM(),MAX()applied over a time bucket. - Target destination – a measurement (and optionally a retention policy) where the aggregated points are written.
The InfluxDB server evaluates the CQ at the interval you specify (EVERY clause). If new points have arrived since the last run, the query processes them and writes the aggregated results.
Worked example: downsampling CPU usage from 1‑second to 1‑minute averages
Assume a database metrics with a retention policy autogen (infinite) holding raw CPU usage in measurement cpu with fields value and tag host. We want a downsampled measurement cpu_1m in a retention policy downsampled that keeps data for 90 days.
1. Create the target retention policy (if it does not exist)
# Run as an admin user in the Influx CLI or via the API
CREATE RETENTION POLICY "downsampled" ON "metrics" DURATION 90d REPLICATION 1 DEFAULT;
This creates a policy that automatically drops points older than 90 days.
2. Define the continuous query
# Still running with admin privileges
CREATE CONTINUOUS QUERY "cq_cpu_1m" ON "metrics"
BEGIN
SELECT MEAN("value") AS "value"
INTO "downsampled"."cpu_1m"
FROM "cpu"
GROUP BY time(1m), "host"
END;
Explanation:
EVERY 1mis implied because theGROUP BY time(1m)bucket matches the interval; InfluxDB will evaluate the query each minute.- The query reads points from
cpuin the default policy, computes the mean per host per minute, and writes the result tocpu_1munder thedownsampledpolicy.
3. Verify the CQ is active
SHOW CONTINUOUS QUERIES;
You should see a row with the name cq_cpu_1m, the query text, and the status running.
4. Check that downsampled points appear
Wait at least two minutes after creating the CQ, then query the target:
SELECT * FROM "downsampled"."cpu_1m" WHERE time > now() - 10m LIMIT 5;
If the raw data stream is healthy, you will see one point per host per minute with the averaged value.
Limitations and practical checks
Continuous queries are powerful but have caveats:
- Data gaps – If no raw points arrive during a bucket interval, the CQ writes nothing for that bucket, leading to missing points in the downsampled series. You can mitigate this by using
fill(previous)orfill(null)in a subsequent query, or by ensuring your agents push data at a steady rate. - Granularity loss** – Downsampled data cannot be used for sub‑minute anomaly detection or high‑frequency forecasting. Keep the raw retention policy long enough for those use cases.
- Interaction with shard TTL** – If a shard’s expiration time (TTL) is shorter than the CQ interval, points may be purged before the CQ can process them, causing gaps. Align the shard group duration with your CQ schedule or increase the shard TTL.
To verify that gaps are not silently affecting your dashboards, run a periodic check:
SELECT COUNT(*) FROM "downsampled"."cpu_1m"
WHERE time > now() - 1h
GROUP BY time(1m), "host"
If any minute shows a count of zero for a host that normally sends data, investigate the raw ingestion pipeline or consider shortening the CQ interval.
Actionable closing
- Identify the metrics that are queried over long periods and suffer from latency.
- Design a target retention policy that matches your desired historical window for the downsampled data.
- Create the CQ with a clear aggregation function and bucket size that balances query speed against needed granularity.
- After deployment, use
SHOW CONTINUOUS QUERIESand spot‑check the target measurement to confirm data appears as expected. - Monitor for missing buckets and adjust ingestion reliability or CQ interval accordingly.
By following these steps, you can offload heavy analytical work to pre‑aggregated series while preserving the raw data for deep dives—all without manual cron jobs or external scripts.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.