Skip to content

Perf: DB query optimization#29

Merged
cchwala merged 5 commits intomainfrom
perf/db-query-optimization
Mar 30, 2026
Merged

Perf: DB query optimization#29
cchwala merged 5 commits intomainfrom
perf/db-query-optimization

Conversation

@cchwala
Copy link
Copy Markdown
Member

@cchwala cchwala commented Mar 30, 2026

Database & Grafana query performance optimizations

With 130M data points (364 CMLs × 2 sublinks, 26 days at 10s resolution) on an 8 GB / 4 CPU VM, loading raw data in the real-time dashboard was slow. The zoomed-out 1h aggregate view was already fast. This PR addresses the raw data path through three changes:

1. PostgreSQL memory tuning (docker-compose.yml)
The database container previously ran with PostgreSQL defaults (128 MB shared_buffers). Parameters are now tuned for the VM size, keeping recently-used data chunks in RAM and steering the query planner toward index scans over sequential scans.

2. TimescaleDB compression (database/init.sql)
Chunks older than 7 days are compressed automatically via a background policy, using (cml_id, sublink_id) as the segment key so queries for a single CML decompress only ~1/728th of a chunk. The current week stays uncompressed for zero-overhead real-time ingestion. At ~21–51× compression ratio on existing data, the entire compressed history fits in shared_buffers. This scales well: as new streams are added the compressed footprint grows slowly while the hot uncompressed window stays bounded at one week.

3. Adaptive query bucketing in the real-time Grafana dashboard
Replaced the binary 1h-aggregate / raw-10s switch with a three-tier system based on the selected time range. For the middle tier (≤ 3 days, Auto mode), a single CTE scan computes MIN/MAX/AVG via time_bucket('$__interval', time), matching the panel's pixel density — so the min/max fill band and avg line are generated at no extra query cost. The Raw mode is now explicit-only, preventing accidental slow queries on wide time ranges. Minor: disabled point rendering on the RSL panel to suppress dots that appeared on the band boundary at low data density.

@codecov
Copy link
Copy Markdown

codecov bot commented Mar 30, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 73.18%. Comparing base (4dcf26e) to head (0e9af2c).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main      #29   +/-   ##
=======================================
  Coverage   73.18%   73.18%           
=======================================
  Files          22       22           
  Lines        1965     1965           
=======================================
  Hits         1438     1438           
  Misses        527      527           
Flag Coverage Δ
mno_simulator 84.18% <ø> (ø)
parser 77.91% <ø> (ø)
webserver 49.08% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@cchwala
Copy link
Copy Markdown
Member Author

cchwala commented Mar 30, 2026

Some additional details

Compression results

Period Before After Ratio
Mar 05–12 4063 MB 79 MB 51×
Mar 12–19 4226 MB 178 MB 24×
Mar 19–26 4875 MB 231 MB 21×
Total DB 16 GB 3.4 GB

Adaptive query tiers (RSL panel, Auto mode)

Time range Query Typical rows/sublink
> 3 days cml_data_1h materialized view ~72
≤ 3 days, interval=Auto time_bucket('$__interval') CTE (MIN/MAX/AVG in one scan) ~800
interval=Raw Raw cml_data up to 25 000

@cchwala
Copy link
Copy Markdown
Member Author

cchwala commented Mar 30, 2026

I verified that things work as expected with a full rebuild of all containers. Can be merged now.

@cchwala cchwala merged commit fb0d1f0 into main Mar 30, 2026
12 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