Prompt
How do I build a database for hundreds of millions of time-based rows?
Latest observation
Building a database for hundreds of millions of time-based rows is mostly an architecture and indexing problem, not just a “pick a database” problem. The right design depends on your access patterns, but there are some strong general principles.
First: define the workload
Before choosing tech, answer these:
- Write rate: how many rows per second/minute?
- Read patterns:
- latest N rows?
- range queries by time?
- queries by entity/device/account + time?
- aggregations over time windows?
- Retention: keep forever or roll off old data?
- Update pattern: append-only, or rows get updated/deleted?
- Concurrency: how many writers/readers?
- Latency target: sub-second? seconds?
- Data shape: fixed schema or flexible?
These determine whether you want:
- a traditional relational DB,
- a time-series DB,
- or a distributed columnar/analytical system.
Common good choices
1) PostgreSQL / MySQL if:
- you have hundreds of millions of rows but not extreme ingest,
- strong consistency matters,
- queries are straightforward,
- you can shard or partition carefully.
2) TimescaleDB (Postgres extension) if:
- your data is time-centric,
- you need SQL,
- you want partitioning, compression, retention policies, and time-series optimizations.
3) ClickHouse if:
- you mostly do analytics, scans, aggregates, dashboards,
- you ingest a lot,
- you can tolerate eventual consistency / batchy updates,
- you need very fast time-range aggregations.
4) Cassandra / ScyllaDB if:
- very high write throughput,
- predictable key-based access,
- you know your query patterns in advance,
- you need horizontal scale and can design around partition keys.
5) InfluxDB / VictoriaMetrics / Prometheus-like systems if:
- it’s truly metrics/time-series,
- mostly append-only,
- retention/downsampling are important.
Core design principles
1) Partition by time
For time-based data, split by time so queries only touch relevant chunks.
Examples:
- daily partitions for large volume
- monthly partitions if volume is lower
- hourly partitions for extremely high volume
In Postgres, this might mean native table partitioning.
In ClickHouse, this is typically built into table design.
Rule of thumb: partition size should be manageable, not tiny. Too many partitions hurts planning and maintenance.
2) Use the right primary access key
Usually the key pattern is:
entity_idtimestamp
Examples:
device_id + tsaccount_id + event_time
If you often query:
- “all events for device X between A and B”
- “latest events for account Y”
then indexing/partitioning should reflect that.
A common pattern:
- partition by time
- cluster/index by (entity_id, timestamp)
This gives efficient time-range reads for a specific entity.
3) Keep rows narrow
For very large row counts:
- avoid storing huge JSON blobs in the main hot table unless necessary
- normalize large infrequently used fields
- use compact types
- avoid wide indexes on many columns
Every extra indexed column increases write cost and storage.
4) Prefer append-only
Append-only workloads are much easier to scale than update-heavy ones.
If possible:
- write events immutably
- avoid in-place updates
- if state changes, write a new event row instead
This works especially well for time-series/event data.
5) Separate hot and cold data
Use tiers:
- hot: recent data, fast storage, frequent queries
- warm: older but still queryable
- cold/archive: cheap storage, slower access
Common approach:
- keep last 30–90 days in primary DB
- archive older data to object storage / warehouse
- optionally downsample old data
6) Index for your queries, not for everything
Too many indexes will kill ingest performance.
Typical useful indexes:
(entity_id, ts DESC)(ts)- maybe
(ts, entity_id)depending on query direction
If most queries are “by time range,” a time partition plus a clustered order may be enough.
Example schema pattern
For PostgreSQL / TimescaleDB
CREATE TABLE events (
id BIGSERIAL PRIMARY KEY,
entity_id BIGINT NOT NULL,
ts TIMESTAMPTZ NOT NULL,
event_type TEXT NOT NULL,
value DOUBLE PRECISION,
payload JSONB
);
Then:
- partition by time
- add index on
(entity_id, ts DESC) - maybe index on
(ts DESC)
If using TimescaleDB:
- make it a hypertable
- compress older chunks
- apply retention policies
For ClickHouse
Typical design:
- partition by month/day
- order by
(entity_id, ts)or(ts, entity_id)depending on queries
Example:
CREATE TABLE events
(
entity_id UInt64,
ts DateTime64(3),
event_type LowCardinality(String),
value Float64,
payload String
)
ENGINE = MergeTree
PARTITION BY toYYYYMM(ts)
ORDER BY (entity_id, ts);
For Cassandra/ScyllaDB
You must design around partition keys carefully.
Example:
- partition key =
(entity_id, bucket_date) - clustering key =
ts
This avoids giant partitions.
PRIMARY KEY ((entity_id, bucket_date), ts)
Where bucket_date might be day or hour.
Practical scaling tips
1) Bucket time if needed
If one entity can generate huge volume, use a bucket:
(entity_id, yyyy_mm_dd)or(entity_id, hour_bucket)
This prevents a single partition from becoming huge.
2) Avoid “hot partition” problems
If all writes land in the same current-day partition, that partition can become hot.
Mitigations:
- choose a DB that handles this well
- add sharding/bucketing by entity hash
- use higher-resolution partitions only if necessary
- spread writes across multiple nodes if distributed
3) Precompute summaries
For dashboards and analytics, don’t repeatedly scan raw rows.
Store:
- per-minute aggregates
- per-hour aggregates
- per-day aggregates
Then raw data is for drill-down, not every query.
4) Use compression
Time-series data compresses well.
Options:
- Timescale compression
- ClickHouse columnar compression
- Postgres TOAST helps somewhat, but not ideal for huge analytic loads
Compression reduces:
- storage
- IO
- cache pressure
5) Plan retention early
Hundreds of millions of rows can become billions fast.
Decide:
- how long to keep raw data
- what to roll up
- when to delete
- whether deletion is automatic by partition drop
Dropping whole partitions is much cheaper than deleting row-by-row.
What I’d recommend in practice
If you need SQL + operational simplicity
Use PostgreSQL + partitioning, or better, TimescaleDB if the data is truly time-series.
Good for:
- moderate to high ingest
- operational queries
- strong consistency
- straightforward app integration
If you need fast analytics over huge volumes
Use ClickHouse.
Good for:
- aggregations
- dashboards
- time-range scans
- billions of rows at low cost
If you need massive write scale and predictable lookups
Use Cassandra/ScyllaDB.
Good for:
- very high throughput
- known query patterns
- horizontal scale
But you must design the model around queries, not normalization.
A simple decision guide
- Need transactions + relational integrity? → PostgreSQL / TimescaleDB
- Mostly analytical queries? → ClickHouse
- Very high write throughput with known access patterns? → Cassandra/ScyllaDB
- Standard metrics monitoring? → Prometheus-like / VictoriaMetrics / InfluxDB
Common mistakes
- using one giant unpartitioned table
- indexing too many columns
- using timestamp alone as the key for everything
- storing huge JSON blobs in every row
- deleting old data row-by-row instead of dropping partitions
- choosing a DB before knowing query patterns
A good default architecture
If I had to recommend a generic pattern:
- Ingest into a write-optimized store
- Partition by time
- Index/cluster by entity + time
- Aggregate into summary tables
- Archive old raw data
- Compress and drop old partitions
- Scale out only when needed
If you want, I can help you design this for your exact case. If you tell me:
- approximate row size
- write rate
- query patterns
- retention period
- whether you need SQL/transactions
…I can recommend a specific database and schema.