perf(providers): optimize gateway error rate query to avoid unnecessary view joins#1865
Merged
chrarnoldus merged 1 commit intomainfrom Apr 1, 2026
Merged
Conversation
…ry view joins The query was using microdollar_usage_view which joins 13+ tables via LEFT JOINs, but only needs columns from microdollar_usage and microdollar_usage_metadata. Query now directly joins only these two tables.
Contributor
Author
Code Review SummaryStatus: No Issues Found | Recommendation: Merge Files Reviewed (1 files)
Reviewed by gpt-5.4-20260305 · 116,925 tokens |
imanolmzd-svg
approved these changes
Apr 1, 2026
jeanduplessis
pushed a commit
that referenced
this pull request
Apr 2, 2026
…ry view joins (#1865) The query was using microdollar_usage_view which joins 13+ tables via LEFT JOINs, but only needs columns from microdollar_usage and microdollar_usage_metadata. Query now directly joins only these two tables. Co-authored-by: kiloconnect[bot] <240665456+kiloconnect[bot]@users.noreply.github.com>
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.
Summary
The
getGatewayErrorRatequery was usingmicrodollar_usage_view, which joins 13+ tables via LEFT JOINs (IP lookups, user agent, editor name, finish reason, etc.). The query only needs 4 columns:provider,has_error,created_at(frommicrodollar_usage), andis_user_byok(frommicrodollar_usage_metadata).Replaced the view query with a direct INNER JOIN between just
microdollar_usageandmicrodollar_usage_metadata, eliminating 12 unnecessary LEFT JOINs. This should significantly reduce query execution time, which is important since this query has a 500ms timeout and is called on every request.Verification
packages/db/src/schema.tsto confirm column sourcescreated_atindex exists onmicrodollar_usage(idx_created_at)Visual Changes
N/A
Reviewer Notes
microdollar_usage_metadata. This means rows without metadata will be excluded. This is intentional sinceis_user_byoklives on the metadata table and rows without metadata can't be filtered on it. If a metadata row is missing,is_user_byokwould be NULL and the= falsefilter would exclude it anyway.{ openrouter: 0, vercel: 0 }, so this optimization helps avoid unnecessary timeouts.