perf: batch DB operations in ingest and plan list to cut CPU load - #5
Draft
dmnlk wants to merge 1 commit into
Draft
perf: batch DB operations in ingest and plan list to cut CPU load#5dmnlk wants to merge 1 commit into
dmnlk wants to merge 1 commit into
Conversation
The ingest endpoint previously issued one INSERT per transcript line, and the plan list endpoint issued a nested sequence of User.FindByID, Project.FindByID, and a full FindByPlanDocumentID scan per plan. Both compounded into O(N) round-trips on hot paths, pegging DB CPU. Repository additions: - EventRepository.CreateBatch: single transaction / multi-row INSERT - UserRepository.FindByIDs and ProjectRepository.FindByIDs - PlanCommentThreadRepository.CountActiveByPlanDocumentID (COUNT only) Handler changes: - /api/ingest now builds all events once and persists them in one call. Duplicate UUIDs are still silently skipped. - Plan list prefetches users and projects in two batch queries and counts comment threads via the new COUNT query instead of fetching full thread rows. Implemented for memory, sqlite, postgres, turso, and dynamodb backends, with matching testsuite coverage that runs across all implementations.
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.
背景
CPU 高騰の原因調査で、以下 2 箇所のホットパスに深刻な N+1 / 直列 DB アクセスがあることが判明したため修正します。
1.
/api/ingestの直列 INSERTPostToolUsehook が頻繁に発火するため、ingest は最も叩かれるエンドポイントです。従来は transcript の行数ぶん
Event.Createを直列で呼んでおり、1 リクエストで N 回の DB ラウンドトリップが発生していました。2. Plan 一覧の多段 N+1
1 Plan につき以下を個別クエリで発行していました:
GetCollaboratorUserIDs(1 回)User.FindByID← ネストループProject.FindByIDPlanCommentThread.FindByPlanDocumentID← 件数しか使わないのに全スレッドを取得100 件 × 3 共同作業者の前提だと、1 リクエストで 400+ クエリ 発行していました。
変更点
Repository 層 (interface + 5 バックエンド + testsuite)
新規メソッド:
テスト方法
go build ./.../go vet ./...成功go test ./...(memory / sqlite / api) 全パスgo build -tags=integration ./...成功TURSO_URL=file:...で turso の integration テスト全パス影響範囲