Time Series Processing with Streaming SQL

Time Series Processing with Streaming SQL

What Is Backpressure in Stream Processing?

Time series data — sensor readings, metrics, stock prices, logs — is naturally streaming. Processing time series with streaming SQL provides real-time downsampling, gap filling, trend detection, and anomaly alerting without batch jobs.

Time Series Patterns with SQL

Downsampling (1-second → 1-minute)

CREATE MATERIALIZED VIEW metrics_1min AS
SELECT metric_name, window_start,
  AVG(value) as avg_value, MIN(value) as min_value, MAX(value) as max_value,
  COUNT(*) as samples
FROM TUMBLE(raw_metrics, ts, INTERVAL '1 MINUTE')
GROUP BY metric_name, window_start;

Rate of Change

CREATE MATERIALIZED VIEW rate_of_change AS
SELECT metric_name,
  last_value(value ORDER BY ts) as current,
  first_value(value ORDER BY ts) as previous,
  last_value(value ORDER BY ts) - first_value(value ORDER BY ts) as delta
FROM raw_metrics WHERE ts > NOW() - INTERVAL '5 minutes'
GROUP BY metric_name;

Anomaly Detection (Z-Score)

CREATE MATERIALIZED VIEW anomalies AS
SELECT metric_name, value, ts,
  (value - AVG(value) OVER w) / NULLIF(STDDEV(value) OVER w, 0) as z_score
FROM raw_metrics
WINDOW w AS (PARTITION BY metric_name ORDER BY ts ROWS BETWEEN 100 PRECEDING AND CURRENT ROW)
HAVING ABS(z_score) > 3;

Streaming SQL vs Time Series DBs

AspectStreaming SQL (RisingWave)TSDB (InfluxDB, TimescaleDB)
Real-time aggregation✅ Continuous MVsQueries on read
Multi-source joins✅ SQL joinsLimited
ServingPG protocolCustom API
StorageS3Local disk

Frequently Asked Questions

Should I use a time series database or a streaming database?

Use a time series database (TimescaleDB, InfluxDB) for long-term time series storage and ad-hoc queries over historical data. Use a streaming database (RisingWave) for real-time aggregation, alerting, and joining time series with other data sources.

Best-in-Class Event Streaming
for Agents, Apps, and Analytics
GitHubXLinkedInSlackYouTube
Sign up for our to stay updated.