fix(ohlcv): bound pool OHLC queries to recent candles for perf (#582) - #583
Merged
Conversation
Pool OHLC endpoints (svm, evm, tvm) aggregate AggregateFunction state rows with a blocking GROUP BY. With no lower time bound the query merges the pool's ENTIRE history before ORDER BY ... LIMIT can discard it, so high-activity pools read GBs and take multiple seconds — the cause of the >10s calls and 500 timeouts reported in #582. Add a lightweight `window_bound` CTE that finds the timestamp cutoff of the most-recent (limit+offset) candles by scanning only the cheap `timestamp` column of `state_ohlc_prices`, then constrain the heavy aggregation to `timestamp >= cutoff`. Results are byte-identical (verified on production), including for sparse pools whose candles span a much wider wall-clock range than interval*limit. Benchmarked on production against outlier pools from #582: - SVM 1h 1m/60: ~830-1240ms/210MiB/815MiB-RAM -> ~120-200ms/16MiB/42MiB - SVM 1d 5m/288: ~980-1450ms -> ~160-250ms - EVM 1m/100: 3270ms/1.56GiB/5.1GB-RAM -> 155ms/262MiB/314MiB evm.sql is shared by the TVM route, so this covers svm/evm/tvm. Coarse intervals with little history regress by tens of ms (extra timestamp pass) but stay well under budget. Also bump dbs-config.yaml.example dex versions to match production (svm-dex v0.5.2, evm-dex v0.5.0, tron evm-dex v0.5.0). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HGQ61P2deQE3fRCVLRbo1S
YaroShkvorets
temporarily deployed
to
fix/ohlcv-window-bound-perf - token-api PR #583
July 9, 2026 18:14 — with
Render
Destroyed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes the slow pool-OHLC queries and intermittent 500s reported in #582 (customer charting use case).
Problem
The pool OHLC endpoints aggregate
AggregateFunctionstate rows via a blockingGROUP BY. With no lower time bound, the query merges the pool's entire history beforeORDER BY … LIMITcan discard it. High-activity pools therefore read GBs and take multiple seconds — the cause of the >10s calls and500 internal_server_errortimeouts in the report (especially under the client's 5× concurrency).This affects SVM, EVM, and TVM (TVM reuses
evm.sql). EVM was actually the worst: a single hot-pool query read 1.56 GiB / 5.1 GB RAM.Fix
Add a lightweight
window_boundCTE that finds the timestamp cutoff of the most-recent(limit+offset)candles by scanning only the cheaptimestampcolumn ofstate_ohlc_prices, then constrain the heavy aggregation withtimestamp >= cutoff.interval×limit— so this is not the unsafe "default a time window" approach.window_bound(EVM) so every counted bucket has a surviving row.Benchmarks (production,
FORMAT Null,use_query_cache=0)~7–21× faster, ~6–16× less memory. The per-query memory collapse is what relieves the concurrency contention producing the 500s.
Trade-off: coarse intervals (1h/1w) with little history regress by tens of ms (extra timestamp pass) but stay far under budget.
SETTINGS optimize_aggregation_in_order=1was tried and rejected — it doesn't prune (bytes unchanged) and made the 1h case slower.Also
dbs-config.yaml.exampledex versions to match production:svm-dex@v0.5.2,mainnet:evm-dex@v0.5.0,tron:evm-dex@v0.5.0.Validation notes
normalizeSQL()clean; parameter sets unchanged.state_ohlc_prices(solana:svm-dex@v0.5.25.8B rows;mainnet:evm-dex@v0.5.0), result parity confirmed.DB_TESTS=trueassertion pinned to the SVM pools OHLC endpoint: slow queries (p95 ~9s, max ~11s) and intermittent 500s on high-activity pools #582 outlier pools as a follow-up.🤖 Generated with Claude Code