Prompt
How do I design time-based partitioning for sensor data?
Latest observation
A good time-based partitioning design for sensor data usually comes down to balancing:
- write throughput,
- query patterns,
- retention/archival needs,
- partition management overhead.
Here’s a practical way to design it.
1) Start with the query patterns
Ask:
- Do you mostly query recent data?
- Do you query by device/sensor ID + time range?
- Do you run aggregations over days/weeks/months?
- Do you delete or archive data after a fixed retention period?
This determines whether you should partition by:
- time only,
- time + device hash,
- time + region/site,
- or a hybrid approach.
2) Choose a time granularity
Common choices:
- Daily partitions: best for high-volume sensor streams and easy retention.
- Monthly partitions: fewer partitions, good for moderate volume.
- Hourly partitions: only if ingestion volume is very high and queries are very time-localized.
Rule of thumb
- If you ingest millions of rows per day or need fast deletes by age, use daily.
- If data volume is smaller, use monthly.
- Avoid overly fine partitions unless you really need them; too many partitions hurts planning and maintenance.
3) Keep partition size reasonable
A partition should be:
- not too small to avoid excessive metadata overhead,
- not too large to avoid slow scans and painful maintenance.
A common target is something like:
- 100 MB to a few GB per partition, depending on the database and workload.
4) Use a composite partitioning strategy when needed
For sensor data, time alone can create hot spots if many writes land in the current partition. To reduce contention and improve parallelism:
- Range partition by time
- then subpartition or hash partition by sensor/device ID
Example design:
PARTITION BY RANGE (event_date)SUBPARTITION BY HASH(sensor_id)
This helps:
- prune old data by time,
- spread writes across multiple partitions,
- speed up queries filtering by sensor/device.
5) Index for the access path
Partitioning is not a substitute for indexing.
Typical indexes:
(sensor_id, event_time)for point lookups and time ranges,(event_time)if you often scan time windows,- maybe
(site_id, event_time)if location-based.
If your database supports it, prefer indexes that match your most common filters exactly.
6) Design for retention and archiving
Time partitions make retention easy:
- drop old partitions,
- move cold partitions to cheaper storage,
- compress older partitions.
Example:
- keep daily partitions for 90 days on fast storage,
- archive monthly partitions older than 90 days,
- delete after 1 year.
7) Handle late-arriving data
Sensor systems often receive delayed events. Decide how long you’ll accept late data into old partitions.
Options:
- keep partitions writable for a short grace period,
- allow inserts into older partitions,
- buffer late data in a staging table and backfill later.
8) Avoid too many tiny partitions
Common mistake: partitioning by minute or second when not necessary.
Problems:
- too many objects,
- slow query planning,
- difficult maintenance,
- large metadata overhead.
Use the coarsest time unit that still meets performance and retention goals.
9) Example pattern
For a high-volume sensor table:
- partition by day
- subpartition by hash(sensor_id) into 8 or 16 buckets
- index on
(sensor_id, event_time) - retain 90 days hot, archive older data monthly
Example conceptual schema:
CREATE TABLE sensor_readings (
sensor_id BIGINT NOT NULL,
event_time TIMESTAMP NOT NULL,
value DOUBLE PRECISION,
quality SMALLINT,
region_id INT
)
PARTITION BY RANGE (event_time);
Then create daily partitions like:
2026-10-252026-10-26- etc.
If supported, add hash subpartitioning by sensor_id.
10) Practical recommendation
If you’re unsure, start with:
- daily range partitions on event time
- index on (sensor_id, event_time)
- retention by dropping old partitions
- add hash subpartitioning only if write load or query concurrency requires it
That’s usually the safest starting point.
If you want, I can also give you:
- a PostgreSQL, MySQL, or SQL Server partitioning example, or
- a design tailored to your exact sensor volume and query patterns.