← Back to Glossary

Time-series database

A time-series database is a database optimized for storing and querying data points indexed by time, such as metrics, sensor readings, or logs, with efficient time-range scans, downsampling, and retention.

Overview

Time-series databases (TSDBs) are purpose-built for data that arrives continuously and is almost always queried by time range: application metrics, IoT sensor readings, financial tick data, and infrastructure monitoring data. Purpose-built examples include InfluxDB, TimescaleDB (a Postgres extension), and Prometheus. They typically offer storage layouts optimized for append-heavy, time-ordered writes; efficient compression for repetitive numeric metric data; automatic downsampling or rollups (storing coarser aggregates for older data); and built-in retention policies that expire data after a configured window.

Common query patterns

Time-series workloads usually revolve around a handful of patterns: filtering to a time window, grouping into time buckets (e.g., hourly or daily averages), computing rolling/moving statistics, and finding the most recent value before a given time.

Time-series analysis in DuckDB

DuckDB isn't a dedicated time-series database — it has no built-in retention policies or purpose-built time-series storage engine — but its SQL supports the query patterns that time-series analysis needs well. Bucketing by time interval is straightforward with date_trunc or time_bucket-style expressions, and DuckDB supports ASOF JOIN, which matches each row in one table to the most recent (or nearest) row in another table by timestamp — a common and otherwise awkward time-series pattern.

Copy code

-- hourly average from raw readings SELECT date_trunc('hour', ts) AS hour, avg(value) AS avg_value FROM sensor_readings GROUP BY ALL ORDER BY ALL; -- match each trade to the most recent quote at or before it SELECT t.trade_id, t.ts, q.price FROM trades t ASOF JOIN quotes q ON t.ts >= q.ts;

For ad hoc analysis of historical time-series data already exported to Parquet or CSV, DuckDB is often a fast, zero-infrastructure alternative to spinning up a dedicated TSDB, though for high-frequency ingestion with automated retention and rollups, a purpose-built time-series database is generally the better long-term fit.

Related terms

FAQS

No. DuckDB has no automatic expiration or downsampling of old data — those need to be implemented manually (e.g., scheduled DELETE statements or writing rollup tables) or handled by a dedicated time-series database.

An ASOF JOIN matches each row in one table to the closest row in another table by time (typically the most recent one at or before it), which is a common need in time-series analysis, such as matching a trade to the prevailing quote at that moment.