Lazy join demand is recomputed over all demanded keys on every batch (O(N) per batch)
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 新手友好度
- 25/100
- Issue 类型
- 重构
- 描述清晰度
- 基本清楚
- 活跃度
- 活跃
- 技术栈
- typescript
- 领域
- database
调研方向
Start in packages/db/src/query/compiler/joins.ts at the demand tap (around line 502), where the whole demanded key set is rebuilt and passed to setDemand on every batch. Then read setDemand, canonicalizeKeys and equalKeySets in packages/db/src/query/live/subset-demand-controller.ts. Done means a batch's demand cost scales with the keys it changes, which you can check by timing writes of about 21k rows in 2,000-row batches.
由索引模型根据 Issue 内容生成。
描述
Validated against @tanstack/db 1.0.0-rc.1 and 1.0.0-rc.1.1.
The same code is on main (15d2d2d) in packages/db/src/query/compiler/joins.ts and packages/db/src/query/live/subset-demand-controller.ts.
This is a performance issue, not a correctness bug.
What happens
For a lazy join, the tap on the active pipeline (joins.ts, around line 502) runs on every batch the active side emits. After updating demandWeights for the batch, it rebuilds the whole demanded key set and passes it to setDemand:
const keys = new Set(
[...demandWeights.values()].filter(({ weight }) => weight > 0).map(({ key }) => key),
)
callbacks[target.sourceId]?.setDemand?.(demandPlans[index]!, keys)
SubsetDemandController.setDemand then makes several more passes over the whole set:
canonicalizeKeys(keys)serializes every key;equalKeySets(previous.keys, nextKeys);coveredKeysflattens every segment's keys;addedfilters every key, andremovedscans every previous key.
So each batch costs O(total demanded keys) per lazy join, even when the batch adds only a few keys.
Writing N rows into the active side in batches of size B costs about O(N² / B) per lazy join.
Measured
A local-first app writes ~21k rows into a collection that is the main source of a live query with several lazy left joins (all collections fully loaded locally, eager sync).
The rows go in batches of 2,000 (one begin/write/commit per batch) so the page stays responsive.
Chrome profile of the main thread while those batches are written (~2.9 s busy):
| Function | Self time |
|---|---|
canonicalizeKeys (subset-demand-controller.js:167 in dist) |
228 ms |
setDemand |
96 ms |
the demand tap in joins.js |
133 ms |
That is ~450 ms, about 16% of the work, spent recomputing demand that changed by one batch.
It also stops us from using smaller batches, which would keep input latency lower: with 500-row batches the same write takes ~4 s instead of ~1.6 s, because every batch pays the full O(N) demand cost again.
Possible fix
Make demand incremental, so a batch costs O(keys it changes):
- In the
tap, record which keys crossed from weight 0 to positive (added) and from positive to 0 (removed) in this batch. If neither set is empty, call the controller with them; if both are empty, skipsetDemandentirely. - Give
SubsetDemandControlleran incremental entry point, e.g.updateDemand(subscription, plan, added, removed), that keeps its canonical key map across calls, canonicalizes only the changed keys, and requests a segment only for added keys no live segment covers. Removals and failed segments can keep today's full path.
Happy to provide a reduced repro or a PR if that helps.
- 主要语言
- TypeScript
- 星标
- 4k
- 派生
- 277
- 平均合并
- 1 天 7 小时
- 30 天内合并 PR
- 172
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
TanStack/db 的其他 Issue
-
难度 1/5 1-3 小时 新手友好度 88/100
维护者通常 1 天内回复
-
OfflineExecutor.dispose() never settles pendingTransactionPromises可能已有人在做 @Claire-ovo 于 6 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 1 天内回复
-
Two @tanstack/db tests time out under CPU load because a cold module import counts against the 5 s default timeout可能已有人在做 @jp-knj 于 5 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 82/100
维护者通常 1 天内回复
-
setWindow publishes the new page before its include demand settles (on-demand materialize / toArray)未关闭
难度 4/5 3-5 天 新手友好度 30/100
维护者通常 1 天内回复
相似的 Issue
-
triage
难度 2/5 1-3 小时 新手友好度 82/100
维护者通常 1 天内回复
-
🎙️ task - drop the raw `./node_modules/.bin/rhachet roles boot` line from bhuild's feedback template未关闭
难度 2/5 1-3 小时 新手友好度 62/100
ehmpathy/rhachet-roles-bhuild#639 ·
维护者通常 1 天内回复
-
e2e-failure ready-to-code
难度 2/5 1-3 小时 新手友好度 70/100
redhat-developer/rhdh-plugin-export-overlays#4650 · 1 条评论 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 62/100
维护者通常 13 天内回复
-
难度 2/5 1-3 小时 新手友好度 75/100
omdsh-dev/DSH-better-sidebar#1098 ·
维护者通常 2 天内回复