Repository navigation
perf(keys): API 密钥列表不再等待 30 天用量统计,并为其加覆盖索引 - #8346
Merged
Wei-Shaw merged 1 commit intoNov 10, 2026
Merged
Conversation
Contributor
|
All contributors have signed the CLA. ✅ |
Contributor
Author
|
I have read the CLA Document and I hereby sign the CLA |
/keys 页面要等密钥列表和每个 key 的用量统计都返回,才撤掉表格骨架屏。 用量统计(POST /usage/dashboard/api-keys-usage)按 api_key_id 汇总近 30 天的 usage_logs.actual_cost。数据量大时,要逐行回表读取几十万到上百万行,接口耗时 十几到三十几秒,甚至超时,页面就一直停在骨架屏。 - 前端:列表返回就渲染表格,用量列单独显示加载占位;统计失败时回落为 $0.0000。 - 后端:新增覆盖索引 (api_key_id, created_at) INCLUDE (actual_cost),使该统计可以 index-only scan,不再回表。迁移采用 CONCURRENTLY 建索引;与其他 usage_logs 索引迁移一样,重试前会先清理中断构建留下的 INVALID 索引。 Refs Wei-Shaw#5858 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
hezhaoqian1
force-pushed
the
perf/upstream-keys-usage-index
branch
from
October 26, 2026 03:16
632f74d to
b8de5e1
Compare
hjkl950217
added a commit
to hjkl950217/sub2api
that referenced
this pull request
Nov 6, 2026
Co-Authored-By: Claude Code <noreply@anthropic.com>
hjkl950217
added a commit
to hjkl950217/sub2api
that referenced
this pull request
Nov 6, 2026
/profile 结论:POST /usage/dashboard/api-keys-usage 只有一条聚合查询, WHERE api_key_id = ANY(...) AND created_at >= ...,已被 migration 249 的 idx_usage_logs_api_key_created_cost (api_key_id, created_at) INCLUDE (actual_cost) 覆盖,可走 index-only scan。批次 6 合入 Wei-Shaw#8346 时该索引已落地;此处在查询上方 标注依赖,避免后续改动给 created_at 套函数导致索引失效。 预聚合方案(上游 PR Wei-Shaw#5859,29 文件 +3062)未合入:属于更大改造,且该 PR 已长期 未更新、mergeable 未知。 Co-Authored-By: Claude Code <noreply@anthropic.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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
Refs #5858
问题
数据量上来以后,
/keys(API 密钥)页面经常一直停在表格骨架屏,等十几到三十几秒,甚至直到请求超时。原因有两层:
loadApiKeys拿到密钥列表后,接着await用量统计POST /usage/dashboard/api-keys-usage,两个都返回才把loading置回 false。列表本身只要几十毫秒,但表格要陪着慢的统计一起等。GetBatchAPIKeyUsageStats按api_key_id汇总近 30 天的usage_logs.actual_cost。现有索引里没有actual_cost,近 30 天的每一行都要回表读取。在一个实际部署上的测量(
usage_logs约 650 万行、10 GB,shared_buffers2 GB):改动
前端(
KeysView.vue):列表返回后就渲染表格,用量列在统计返回前显示独立的加载占位,统计失败时回落为$0.0000。切页、刷新时会通过已有的AbortController取消旧的统计请求。迁移
249_add_usage_logs_api_key_cost_index_notx.sql:加上这个索引后,统计可以走 index-only scan。写法沿用 174、226 等
usage_logs索引迁移:在prepareNonTransactionalMigration里登记索引名,重试前先清理中断构建留下的 INVALID 索引。上线后,同一部署上的实测:
索引大小约为行数 × 40 B(650 万行约 300 MB)。在该表上 CONCURRENTLY 构建约 70 秒,不阻塞写入。
和 #5859 的关系
#5859 用
usage_apikey_daily_rollups预聚合解决的是同一个接口,范围更大,还包含 ops trend 和 system-logs。本 PR 是一个小而独立的修复:(api_key_id, created_at)上的其他查询依然有用,或者也可以直接移除。测试
KeysView.spec.ts新增 2 个用例:统计挂起时表格已经渲染(loading=false)且用量列显示占位;统计失败时占位消失。第一个用例在旧代码上会失败。migrations/usage_log_api_key_cost_index_test.go:校验迁移内容。migrations_runner_api_key_cost_index_test.go:校验存在 INVALID 索引时,重试会先 DROP 再 CREATE。go build ./...、相关go test(含-tags=unit)、KeysView.spec.ts28/28、ESLint、vue-tsc --noEmit。🤖 Generated with Claude Code