Repository navigation
降低 D1 写入:用量更新不重写索引、额度历史去重、汇总增量重算 - #293
Merged
Merged
Conversation
线上 7 天明细显示 D1 写入是 Cloudflare Free 唯一偏紧的资源(日常 36%~42%)。D1 把每个 被 SET 触及的索引都计入写入行数,三处写法因此成倍放大: - usage_hourly_facts upsert:不再 SET 冲突键列(冲突时必然相等)。token 变化的一次更新 10 → 4 行。 - limit_window_history:只在读数变化或距上一条满 1 小时时记录(保留校准采样)。不变时 4 → 0 行。 - 单日 rollup 重算:由整天 DELETE+INSERT 改为「删无明细支撑的格子 + 只更新变化的格子」, 日的起止时刻单独、仅在不同时更新。10 小时改 1 小时:55 → 2 行;无变化:15 → 0 行。 新增 write-cost.test.ts 以本地 D1 meta.rows_written(与线上同口径、含索引)钉住预算, 并覆盖身份变化/明细删除时清掉陈旧格子;变异验证全部变红。 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This was referenced Nov 7, 2026
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.
为什么
2026-11-07 回源(等级 7):D1 写入日常 3.6~4.2 万行/天(Free 上限 10 万,36%~42%),是唯一偏紧的资源;读取 15~23%、请求 1%、存储 6%。7 天逐 SQL 明细显示 D1 把每个被 SET 触及的索引都计入写入行数。
改动与实测(本地 D1 meta.rows_written,与线上同口径)
预计日写入从约 4 万行降到约 1.5~1.8 万行。
验证(等级 3)
write-cost.test.ts新增 8 条:写入预算 + 结果与全量一致 + 明细删除/身份变化时清掉陈旧格子 + 分批常量一致。test_signed_release本机缺 cryptography,uv run --with cryptography补跑 OK)。部署后
回源复测 D1 每日写入(
scripts/check_cf_usage.py+ 逐 SQL 明细)。另:本机额度采集间隔已由 5 分钟改为 30 分钟(运维配置,不在本 PR)。
🤖 Generated with Claude Code