Skip to content

perf: bound latest-signal queries to a lookback window#282

Merged
zer0stars merged 1 commit intomainfrom
feat/latest-signals-lookback
Apr 17, 2026
Merged

perf: bound latest-signal queries to a lookback window#282
zer0stars merged 1 commit intomainfrom
feat/latest-signals-lookback

Conversation

@zer0stars
Copy link
Copy Markdown
Member

Summary

  • getLatestQuery / getLastSeenQuery filtered only on subject. With signal ordered by (subject, timestamp, name) and partitioned by month, missing timestamp bounds meant every historical partition was scanned per call (10–20M avg read rows in prod).
  • Adds LATEST_SIGNALS_LOOKBACK_DAYS (default 45 in settings.sample.yaml, 0 disables) and plumbs the lower bound into both queries.
  • Removes the unused DEVICE_LAST_SEEN_BIN_HOURS / lastSeenBucketHrs field that was threaded into the Service but never read.

Why

Top-10 slowest prod queries were all latest-signal fetches, each reading 10–21M rows. With the timestamp bound, partition pruning kicks in and most active vehicles drop to three partitions — expect a multi-x reduction in Avg. Read rows and Avg. Mem Usage.

Test plan

  • make lint clean
  • make test clean (excluding docker-gated CH integration tests)
  • After deploy, compare Avg. Read rows on the same queries in the query stats page vs. the screenshot we started from.
  • Tune LATEST_SIGNALS_LOOKBACK_DAYS down if a tighter window is safe.

🤖 Generated with Claude Code

getLatestQuery and getLastSeenQuery filtered only on subject, forcing
ClickHouse to scan every historical partition for a vehicle (10-20M rows
per call on prod-scale data). The signal table is ORDER BY (subject,
timestamp, name) PARTITION BY toYYYYMM(timestamp), so an upper bound on
timestamp enables partition pruning and primary-key range reads.

Adds LATEST_SIGNALS_LOOKBACK_DAYS (default 45 in the sample config, 0
disables the filter). Drops the unused DEVICE_LAST_SEEN_BIN_HOURS field
which was previously threaded into the Service but never read.
@zer0stars zer0stars merged commit c042cad into main Apr 17, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant