Commit fb0d1f0
authored
Perf: DB query optimization (#29)
## 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.
Individual commits:
* perf: tune PostgreSQL memory settings for 8 GB VM
* perf: adaptive $__interval bucketing, three-tier auto/raw query split
* perf: optimize SQL query for CML time series data with min, max, and avg calculations
* perf: change timeseries panel to never show points for improved clarity
* perf: enable TimescaleDB compression after 7 days, increase shared_buffers to 2GB1 parent 4dcf26e commit fb0d1f0
3 files changed
Lines changed: 62 additions & 8 deletions
File tree
- database
- grafana/provisioning/dashboards/definitions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
112 | 112 | | |
113 | 113 | | |
114 | 114 | | |
115 | | - | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
73 | 73 | | |
74 | 74 | | |
75 | 75 | | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
76 | 88 | | |
77 | 89 | | |
78 | 90 | | |
| |||
Lines changed: 27 additions & 7 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
105 | 105 | | |
106 | 106 | | |
107 | 107 | | |
108 | | - | |
| 108 | + | |
109 | 109 | | |
110 | 110 | | |
111 | 111 | | |
| |||
373 | 373 | | |
374 | 374 | | |
375 | 375 | | |
376 | | - | |
| 376 | + | |
377 | 377 | | |
378 | 378 | | |
379 | 379 | | |
| |||
383 | 383 | | |
384 | 384 | | |
385 | 385 | | |
386 | | - | |
| 386 | + | |
387 | 387 | | |
388 | 388 | | |
389 | 389 | | |
| |||
403 | 403 | | |
404 | 404 | | |
405 | 405 | | |
406 | | - | |
| 406 | + | |
407 | 407 | | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
408 | 418 | | |
409 | 419 | | |
410 | 420 | | |
| |||
708 | 718 | | |
709 | 719 | | |
710 | 720 | | |
711 | | - | |
| 721 | + | |
712 | 722 | | |
713 | 723 | | |
714 | 724 | | |
| |||
718 | 728 | | |
719 | 729 | | |
720 | 730 | | |
721 | | - | |
| 731 | + | |
722 | 732 | | |
723 | 733 | | |
724 | 734 | | |
| |||
738 | 748 | | |
739 | 749 | | |
740 | 750 | | |
741 | | - | |
| 751 | + | |
742 | 752 | | |
| 753 | + | |
| 754 | + | |
| 755 | + | |
| 756 | + | |
| 757 | + | |
| 758 | + | |
| 759 | + | |
| 760 | + | |
| 761 | + | |
| 762 | + | |
743 | 763 | | |
744 | 764 | | |
745 | 765 | | |
| |||
0 commit comments