Prompt
how to store time series data
Latest observation
Storing time series data depends on your scale and query patterns, but the core ideas are usually the same:
1) Use a schema designed for time
A typical time series record looks like:
- timestamp: when the event happened
- metric name: what is being measured
- value: the numeric reading
- tags/dimensions: extra context like host, region, sensor_id, user_id
Example:
timestamp metric value host region
2026-10-25T10:00Z cpu_usage 72.1 app-1 us-east
2026-10-25T10:01Z cpu_usage 68.4 app-1 us-east
2) Choose the right storage system
Small/simple use cases
Use a regular relational database like PostgreSQL or MySQL if:
- data volume is modest
- queries are not too heavy
- you want simplicity
A common PostgreSQL table:
CREATE TABLE metrics (
time TIMESTAMPTZ NOT NULL,
metric_name TEXT NOT NULL,
value DOUBLE PRECISION NOT NULL,
host TEXT,
region TEXT
);
CREATE INDEX ON metrics (metric_name, time DESC);
CREATE INDEX ON metrics (host, time DESC);
Larger time series workloads
Use a time-series database if you need:
- high ingest rate
- fast range queries
- automatic retention/downsampling
Examples:
- TimescaleDB (Postgres extension)
- InfluxDB
- Prometheus (for monitoring metrics)
- ClickHouse (excellent for analytical time-based workloads)
3) Partition by time
For large datasets, partitioning helps a lot.
Common approaches:
- daily/weekly/monthly partitions
- store recent data separately from old data
- keep hot data on fast storage
Example idea:
metrics_2026_10metrics_2026_11
This improves:
- query speed for time ranges
- deletion of old data
- maintenance
4) Index for your query patterns
The most common query is:
- “give me data for this metric between time A and time B”
Useful index:
(metric_name, time)- or
(device_id, time)
Avoid indexing too many dimensions unless needed, because it can slow writes.
5) Control retention
Time series data grows quickly, so define:
- how long to keep raw data
- whether to aggregate older data
Example:
- keep raw data for 30 days
- keep hourly aggregates for 1 year
This is often done with:
- retention policies
- scheduled cleanup jobs
- rollups/downsampling
6) Compress if possible
Time series data often compresses well because:
- timestamps are ordered
- values change gradually
- many rows have repeated tags
Use:
- database compression features
- columnar storage systems
- binary formats if storing files
7) Consider append-only design
Time series data is usually:
- mostly inserted
- rarely updated
- sometimes deleted by retention rules
This makes append-only storage efficient.
8) If using files, format them well
If you’re storing in object storage or files:
- use Parquet or ORC for analytics
- use CSV only for simple/export use cases
- partition files by date, e.g.
year=2026/month=10/day=25/
Rule of thumb
- Under moderate scale: PostgreSQL + time indexing
- Metrics/monitoring: Prometheus or InfluxDB
- Analytics over huge data: ClickHouse or BigQuery/Snowflake
- General time-series in Postgres ecosystem: TimescaleDB
If you want, I can also give you:
- a database schema example,
- a best storage choice by use case, or
- a Python code example for writing time series data.