Lazy join demand is recomputed over all demanded keys on every batch (O(N) per batch)

未关闭
#2,321 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 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);
  • coveredKeys flattens every segment's keys;
  • added filters every key, and removed scans 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):

  1. 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, skip setDemand entirely.
  2. Give SubsetDemandController an 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

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

TanStack/db 的其他 Issue

查看 TanStack/db 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。