Repository navigation
fix(usage): avoid SQLite history scans for latest additional quotas - #14
Merged
Merged
Conversation
Replace history-wide ranking with indexed per-account alias seeks while preserving latest-row semantics. Add SQLite covering indexes and large-pool regression coverage. Seat: Codex Check: 75 relevant repository/integration/migration tests; Ruff; import; strict OpenSpec; historical result equality and query-plan benchmark.
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.
SQLite additional-quota routing and cache warming ranked the full matching usage history on each latest lookup. With roughly 1.1 million historical rows, representative reads took 0.7–4.2 seconds and repeatedly consumed several CPU cores.
Replace that SQLite path with covering-index seeks per account and alias, then choose the latest candidate using the existing timestamp, usage and ID precedence. Keep canonical and historical aliases, account/window/since filters, orphan history and large account pools intact. The additive SQLite-only migration supplies matching indexes; PostgreSQL routing is unchanged.
Validation: 75 relevant repository, integration and migration tests; imports; Ruff; strict OpenSpec. A database-snapshot comparison returned identical result IDs for four quota keys, with representative reads reduced to 3–19 ms; EXPLAIN QUERY PLAN confirms covering-index seeks. Migration downgrade/upgrade was exercised on the snapshot. The deployed fix reduced observed backend CPU from approximately 465% to 0.6–12.5% under active traffic after a graceful restart.