Prompt
How do I model tags and labels for fast time-series queries?
Latest observation
For fast time-series queries, the key is to separate the metric values from the dimensions you filter/group by, and to model tags/labels in a way that supports your most common query patterns.
Core idea
Use:
- A narrow time-series fact table for measurements
- A dimension table for tags/labels when values are reused and queried often
- A denormalized or indexed representation when you need very fast filtering on high-cardinality tags
Common modeling options
1) Denormalized columns for common tags
If you have a small, stable set of tags like:
hostregionserviceenv
store them as regular columns in the main table.
Example:
metrics (
ts timestamp,
metric_id bigint,
value double,
host text,
region text,
service text,
env text
)
Best for:
- Fast queries on known dimensions
- Simple filtering and grouping
Tradeoff:
- Less flexible if tag keys vary often
2) Key-value tag table
Store tags separately:
metric_points (
ts timestamp,
metric_id bigint,
value double,
point_id bigint
)
point_tags (
point_id bigint,
tag_key text,
tag_value text
)
Best for:
- Dynamic, arbitrary labels
- Lots of different tag keys
Tradeoff:
- Joins can get expensive for high-volume queries
3) Inverted index / label index
This is common in time-series systems and works well for label-based filtering.
Schema idea:
series (
series_id bigint,
metric_id bigint,
host text,
region text,
service text,
env text
)
samples (
series_id bigint,
ts timestamp,
value double
)
Then create a lookup structure from labels to series_id.
For example:
series_labels (
label_key text,
label_value text,
series_id bigint
)
Best for:
- Very fast query planning/filtering by labels
- Many samples per series
Tradeoff:
- Requires maintaining a mapping layer
Recommended pattern for fast queries
For most analytical time-series workloads:
- Identify the series by labels/tags
- Store samples separately
- Index label combinations or use a series lookup table
- Partition by time
- Cluster/sort by series and timestamp
This gives you:
- Fast label filtering
- Efficient range scans over time
- Compact storage for samples
Practical indexing tips
Partitioning
Partition by time, e.g. day/week/month depending on volume.
Clustering / sort order
Sort by:
series_id, tsormetric_id, ts, host, region
This makes time-range queries efficient.
Indexing tags
If tags are used in WHERE clauses, index them:
- B-tree for low/medium cardinality
- Bitmap indexes for analytical databases
- Inverted indexes for text-like or label-style filtering
Cardinality matters
High-cardinality labels like:
user_idrequest_idsession_id
can hurt performance if stored as primary filtering dimensions.
Use them carefully:
- Only index if needed
- Consider sampling or separate logs/events
- Avoid putting extremely high-cardinality values in the main query path unless necessary
Good rule of thumb
- Stable, common filters → regular columns
- Flexible labels → label table / inverted index
- High-volume samples → separate time-series fact table
- Very high-cardinality dimensions → avoid as primary tags
Example design
A common and efficient design:
series (
series_id bigint primary key,
metric_name text,
host text,
region text,
service text,
env text
)
samples (
series_id bigint,
ts timestamp,
value double,
primary key (series_id, ts)
)
Query flow:
- Find matching
series_ids using tag filters - Read samples for those series in the time range
If you tell me what database you’re using (Postgres, ClickHouse, Cassandra, TimescaleDB, InfluxDB, etc.) and what your queries look like, I can suggest a concrete schema and indexes.