Prompt
How do I build a sensor data warehouse with fast range filters?
Latest observation
A good pattern for a sensor data warehouse with fast range filters is:
- Store raw time-series data append-only
- Partition primarily by time
- Cluster/sort by the dimensions you filter on most
- Use a columnar engine or warehouse-friendly storage
- Pre-aggregate for common queries
- Keep recent “hot” data and historical “cold” data in the right tier
1) Start with your query patterns
Before designing anything, identify the main filters:
- Time ranges:
WHERE ts BETWEEN ... - Sensor/device IDs:
WHERE sensor_id = ... - Geographic/site filters:
WHERE site_id = ... - Value thresholds:
WHERE temperature > 80 - Common dashboards: last 1h, 24h, 7d, etc.
Fast range filters come from making the storage layout match those patterns.
2) Use a time-partitioned layout
For sensor data, partition by a coarse time unit:
- Daily partitions are common
- Hourly if ingest/query volume is very high
- Avoid too many tiny partitions
Example partitioning:
event_date = 2026-10-25- optionally
event_hour = 13
This lets the engine skip irrelevant data quickly.
3) Cluster or sort by frequently filtered dimensions
Within each time partition, sort/cluster by:
sensor_idsite_id- maybe
device_type
This improves pruning and compression, especially for range scans over a subset of sensors.
Examples:
- BigQuery: clustering
- Snowflake: clustering keys
- Redshift: sort keys/dist keys
- Delta/Iceberg/Hudi: partition + file clustering/compaction
- Postgres/Timescale: time partitioning + hypertables + indexes
4) Choose the right storage format
For analytical warehouses:
- Parquet or ORC is ideal
- Columnar storage speeds up reading only needed columns
- Compresses well
Typical schema:
sensor_readings (
ts TIMESTAMP,
event_date DATE,
sensor_id STRING,
site_id STRING,
metric_name STRING,
metric_value DOUBLE,
quality_flag INT
)
5) Use indexes or metadata that support range pruning
If you’re in a relational system, add:
- B-tree index on
tsfor range scans - Composite index on
(sensor_id, ts)if you often query sensor + time - BRIN index in PostgreSQL for very large append-only time-series tables
For warehouse/lakehouse systems, the equivalent is usually:
- partition pruning
- file stats
- clustering/sort order
6) Precompute the common rollups
If users often query:
- 1-minute averages
- hourly min/max
- daily summaries
then create aggregate tables/materialized views:
sensor_minute_aggsensor_hour_aggsensor_daily_agg
This avoids scanning raw data for dashboards.
7) Separate hot and cold data
A common architecture:
- Hot recent data: fast store for the last 7–30 days
- Cold historical data: cheaper warehouse/object storage
Examples:
- Hot: TimescaleDB, ClickHouse, InfluxDB, PostgreSQL + partitions
- Cold: Snowflake, BigQuery, Databricks, S3 + Iceberg/Delta
8) Consider a time-series optimized engine if range scans dominate
If most queries are:
- time bounded
- filter by sensor/device
- aggregate over large ranges
then consider:
- ClickHouse: excellent for analytical range scans
- TimescaleDB: strong SQL + time-series features
- Apache Druid: good for real-time analytics
- BigQuery/Snowflake: good if you want managed warehousing
- Postgres + BRIN: good for moderate scale
9) Example design
If you want a warehouse-like setup on cloud object storage:
- Raw ingest lands in
s3://bucket/sensor_data/dt=YYYY-MM-DD/ - Files are Parquet
- Table is in Iceberg/Delta/Hudi
- Partition by
event_date - Cluster/sort by
sensor_id - Build daily/hourly aggregate tables
- Query via Trino/Athena/Spark/Databricks
This gives good range-filter performance and scalability.
10) Practical rules of thumb
- Partition on a column with low-to-moderate cardinality and strong pruning value: usually time
- Don’t partition by
sensor_idunless you have very few sensors - Keep partitions large enough to avoid metadata overhead
- Use compaction to prevent many small files
- Store timestamps in UTC
- Add generated columns like
event_datefor partitioning - Maintain summaries for dashboards
11) If you want a simple schema/index recommendation
For PostgreSQL/TimescaleDB:
- hypertable on
ts - index on
(sensor_id, ts DESC) - BRIN on
tsfor large historical scans - continuous aggregates for rollups
For a cloud warehouse/lakehouse:
- partition by date
- cluster by
sensor_id - use Parquet
- build aggregate tables
12) Example query pattern
SELECT sensor_id, avg(metric_value)
FROM sensor_readings
WHERE ts >= '2026-10-01'
AND ts < '2026-10-08'
AND site_id = 'site_12'
GROUP BY sensor_id;
This is fast when:
- the engine prunes all partitions except that week
- files are clustered by
site_id/sensor_id - only needed columns are read
If you want, I can also give you:
- a concrete schema for PostgreSQL/TimescaleDB,
- a cloud warehouse design for BigQuery/Snowflake,
- or a lakehouse design using Parquet + Iceberg/Delta.