PromQL label lookups with match[] read the whole TimeSeries tags table
Summary
PromQL label lookups with match[] (/label/<name>/values, /labels) on a TimeSeries table read every row of the tags table, even when each selector names a single metric. On a large table they take seconds; filtering the same lookup on the metric name takes a fraction of a second.
Details
Since #3581 the lookup is
SELECT DISTINCT tags['<label>'] AS val
FROM timeSeriesTags(db, table)
WHERE id IN (SELECT id FROM timeSeriesSelector(db, table, '<selector>', start, end))
ORDER BY val
timeSeriesSelector() itself is fast, but the tags table is ordered by metric_name first, so id IN (...) alone gives the primary key nothing to prune with and every tags row is read.
Measured
ClickHouse 26.10, TimeSeries schema version 8, about 140M series in the tags table, 1 hour window, a dropdown scoped to a small metric (k8s_namespace_phase, about 3k series):
| Query | Time |
|---|---|
timeSeriesSelector() alone |
0.3 s |
Lookup as today (id IN (...)) |
6–16 s |
Same plus metric_name IN ('k8s_namespace_phase') |
0.24 s |
Same result rows in both cases.
Proposed fix
When every match[] selector names its metric (name{...}, name, or {__name__="name", ...}), add metric_name IN (...) to the tags filter. Selectors that can match several metrics ({job="x"}, {__name__=~"a|b"}) keep today's behaviour. PR to follow.