Skip to content

perf(providers): optimize gateway error rate query to avoid unnecessary view joins#1865

Merged
chrarnoldus merged 1 commit intomainfrom
perf/optimize-gateway-error-rate-query
Apr 1, 2026
Merged

perf(providers): optimize gateway error rate query to avoid unnecessary view joins#1865
chrarnoldus merged 1 commit intomainfrom
perf/optimize-gateway-error-rate-query

Conversation

@kilo-code-bot
Copy link
Copy Markdown
Contributor

@kilo-code-bot kilo-code-bot Bot commented Apr 1, 2026

Summary

The getGatewayErrorRate query was using microdollar_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 (from microdollar_usage), and is_user_byok (from microdollar_usage_metadata).

Replaced the view query with a direct INNER JOIN between just microdollar_usage and microdollar_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

  • Reviewed the view definition in packages/db/src/schema.ts to confirm column sources
  • Verified the optimized query uses the correct table aliases and column references
  • Confirmed created_at index exists on microdollar_usage (idx_created_at)

Visual Changes

N/A

Reviewer Notes

  • Changed from LEFT JOIN (in the view) to INNER JOIN for microdollar_usage_metadata. This means rows without metadata will be excluded. This is intentional since is_user_byok lives on the metadata table and rows without metadata can't be filtered on it. If a metadata row is missing, is_user_byok would be NULL and the = false filter would exclude it anyway.
  • The query has a 500ms timeout with a fallback to { openrouter: 0, vercel: 0 }, so this optimization helps avoid unnecessary timeouts.

…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.
@kilo-code-bot
Copy link
Copy Markdown
Contributor Author

kilo-code-bot Bot commented Apr 1, 2026

Code Review Summary

Status: No Issues Found | Recommendation: Merge

Files Reviewed (1 files)
  • src/lib/providers/gateway-error-rate.ts

Reviewed by gpt-5.4-20260305 · 116,925 tokens

@chrarnoldus chrarnoldus merged commit bd87483 into main Apr 1, 2026
15 checks passed
@chrarnoldus chrarnoldus deleted the perf/optimize-gateway-error-rate-query branch April 1, 2026 19:58
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>
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.

2 participants