Repository navigation
computers.gateway_key_hash 加唯一索引,网关认 key 不再扫全表 - #83
Merged
Merged
Conversation
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Be3SZps2JjqvyrJ1vbLmni
joesmart
force-pushed
the
joesmart/perf-gateway-key-index
branch
from
October 23, 2026 14:09
5609e64 to
230af84
Compare
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.
改了什么
给
computers.gateway_key_hash加唯一索引computers_gateway_key_hash_idx,迁移0048_computers_gateway_key_hash_idx.sql由pnpm --filter @baize/control-plane generate生成,已有迁移没动。为什么
网关每次模型请求都走
findComputerByGatewayKey,只凭哈希反查电脑,原先这一列没有索引,每次扫全表;列也不唯一,万一重复limit(1)会默默取第一行。唯一索引把查找变成索引扫描,重复在写入时就会被拒绝。其他按秘密查身份的列逐个看过,不需要加:
computers.token_hash:authenticateComputer先按主键id取行,再在内存里timingSafeEqual比对,从不作为查询条件。sessions:token 本身就是主键id。oauth_states.state:主键。routines_triggers_idx(GIN),schema 注释写明了不做全局唯一约束的理由,本 PR 不改。数据核对:
gateway_key_hash是notNull,值为hashSecret(32 字节随机数),每次 hello 都会轮换;开发库computers2 行、2 个不同哈希、0 个 null,迁移不会撞上唯一约束。怎么验证的
在临时库上跑全部 49 个迁移,插入 2 万台电脑后 EXPLAIN ANALYZE:
同一个库在事务里 drop 掉这个索引对照:
没有加测试:这次改动没有用户能观察到的行为变化,按约定不写断言索引存在的测试。
Closes #22
🤖 Generated with Claude Code
https://claude.ai/code/session_01DKvSzuVcWTvHeLZgmkKVrh