On-demand ordered windows wait on a boundary tie request even when ordering by the collection key
Maintainer thường phản hồi trong vòng 1 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 18/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Khá rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Công nghệ
- typescript
- Lĩnh vực
- data-engineering
Hướng nghiên cứu
Start in packages/db/src/query/live/ordered-source-loader.ts, around the sequencing at L745-L752 and the boundary request at L880, then look at buildCursorCurrent in packages/db/src/utils/cursor.ts. The repro.mjs script in the issue shows the two extra loadSubset calls per window step. Done means one loadSubset per step when the leading order term is unique, without changing behaviour for tied keys.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Describe the bug
An on-demand live query ordered by the collection key (getKey: row => row.id and .orderBy(({ item }) => item.id)) sends an extra loadSubset after every ordered page. It has where: <query where> AND id = <last loaded id>, with no orderBy and no limit. id is unique, so this request can only return the row that was just loaded.
Since 0.9.0 (#1797), the ordered source loader waits for it: ordered → boundary → more. Each window step therefore costs two sequential round trips instead of one, and setWindow() resolves only after the boundary request returns. In 0.6.14 a window step was a single cursor request.
With the Electric adapter, the following cursor request also sends cursor.whereCurrent (id = <last loaded id>) alongside whereFrom. Each step therefore makes two requests whose only result is a row the collection already holds.
Reproduction
A plain on-demand collection with a logging loadSubset and 50 ms of simulated latency. No adapter is involved. The script is in the details below.
@tanstack/[email protected]:
4- 57 ms page limit=5 -> 5 rows
57- 109 ms unordered limit=null -> 1 rows (list = 'a' AND id = 'row-004')
413- 466 ms cursor limit=5 -> 6 rows
466- 518 ms unordered limit=null -> 1 rows (list = 'a' AND id = 'row-009')
setWindow({ limit: 10 }) took 107 ms
@tanstack/[email protected], same script:
3- 60 ms page limit=5 -> 5 rows
363- 416 ms cursor limit=5 -> 6 rows
setWindow({ limit: 10 }) took 55 ms
repro.mjs (run with node repro.mjs next to an installed @tanstack/db)
import { BTreeIndex, createCollection, createLiveQueryCollection, eq, parseWhereExpression } from '@tanstack/db'
const LATENCY_MS = 50
const serverRows = Array.from({ length: 20 }, (_, i) => ({ id: `row-${String(i).padStart(3, '0')}`, list: 'a' }))
const toPredicate = where =>
where === undefined
? () => true
: parseWhereExpression(where, {
handlers: {
eq: ([f], v) => r => r[f] === v,
gt: ([f], v) => r => r[f] > v,
isNull: ([f]) => r => r[f] === null,
isUndefined: ([f]) => r => r[f] === undefined,
and: (...ps) => r => ps.every(p => p(r)),
or: (...ps) => r => ps.some(p => p(r)),
},
})
const select = (where, limit) =>
serverRows.filter(toPredicate(where)).sort((a, b) => (a.id < b.id ? -1 : 1)).slice(0, limit)
const and2 = (a, b) => ({ type: 'func', name: 'and', args: [a, b] })
const t0 = Date.now()
const calls = []
const items = createCollection({
id: 'items',
getKey: row => row.id,
syncMode: 'on-demand',
defaultIndexType: BTreeIndex,
defaultStringCollation: { stringSort: 'lexical' },
sync: {
sync: ({ begin, write, commit, markReady }) => {
markReady()
return {
loadSubset: async options => {
const call = {
start: Date.now() - t0,
kind: options.cursor ? 'cursor' : options.orderBy ? 'page' : 'unordered',
limit: options.limit ?? null,
}
calls.push(call)
await new Promise(resolve => setTimeout(resolve, LATENCY_MS))
// Like the Electric adapter: a cursor request loads whereCurrent ties plus whereFrom rows.
const rows = options.cursor
? [
...select(and2(options.where, options.cursor.whereCurrent)),
...select(and2(options.where, options.cursor.whereFrom), options.limit),
]
: select(options.where, options.limit)
begin()
for (const row of rows) write({ type: 'insert', value: row })
commit()
call.end = Date.now() - t0
call.rows = rows.length
},
}
},
},
})
items.createIndex(row => row.id, {
indexType: BTreeIndex,
options: { compareOptions: { ...items.compareOptions, direction: 'asc', nulls: 'last' } },
})
const windowed = createLiveQueryCollection(q =>
q
.from({ item: items })
.where(({ item }) => eq(item.list, 'a'))
.orderBy(({ item }) => item.id, { direction: 'asc', nulls: 'last' })
.limit(5)
)
await windowed.preload()
await new Promise(resolve => setTimeout(resolve, 300))
const calledAt = Date.now() - t0
await windowed.utils.setWindow({ offset: 0, limit: 10 })
const resolvedAt = Date.now() - t0
await new Promise(resolve => setTimeout(resolve, 300))
for (const call of calls) {
console.log(`${String(call.start).padStart(4)}-${String(call.end).padStart(4)} ms ${call.kind.padEnd(9)} limit=${String(call.limit).padEnd(4)} -> ${call.rows} rows`)
}
console.log(`setWindow({ limit: 10 }) took ${resolvedAt - calledAt} ms`)
Impact
We backfill a large on-demand Electric collection in 1,000-row windows by calling setWindow repeatedly, ordered by id. In our E2E benchmark (280 ms request latency, medians of 3 runs), the only change between the two runs was the upgrade from 0.6.14 to 0.13.0:
| 0.6.14 | 0.13.0 | |
|---|---|---|
| 30k rows: sequential round trips | 31 | 63 |
| 30k rows: time to load all rows | 12.9 s | 23.6 s |
| 70k rows plus dependent output queries: time to load all rows | 73 s | 114 s |
In production, every 1,000-row page also showed both id = <last id> requests, each returning the one row already loaded.
Expected behavior
When the leading order term is unique, a window step needs one loadSubset. No row can tie with the boundary, so neither the boundary request nor cursor.whereCurrent can add anything.
Possible approaches
Core can't infer from a getKey function that id is unique, so this probably needs an explicit signal. Ideas:
- An opt-in that marks the leading order term as unique, for example a collection option naming the key path, or
unique: trueon the order-by options. With it:loadBoundarywould skip the tie request;- adapters would leave out
cursor.whereCurrent, perhaps via a flag onLoadSubsetOptions/CursorExpressions.
- Independent of uniqueness: don't hold the next window step or
setWindow()resolution on the boundary request, so it costs no added latency. That was the 0.6.14 behavior, where the tie travelled with the cursor request.
Code references
- Sequencing, 0.13.0:
ordered-source-loader.tsL745–L752, and the boundary request at L880. The same sequencing is still onmain(0.14.0) at L774–L779. - Equality boundary:
utils/cursor.tsbuildCursorCurrent. - Electric adapter, both cursor parts:
electric.tsL771–L797. - Related: #960 (separate
whereFrom/whereCurrent), #1797 (sequential boundary step), #1893 (leading-column continuation with tie expansion), and RFC #1657, which proposed appending the primary key as a total-order tie-breaker.
Versions
@tanstack/db0.13.0; the same code is onmain/ 0.14.0.@tanstack/electric-db-collection0.5.9.@tanstack/react-db0.5.8.- Node 24; also seen in Chrome in production.
- Ngôn ngữ chính
- TypeScript
- Star
- 4k
- Fork
- 277
- Merge trung bình
- 17 giờ 40 phút
- Pull request đã merge (30 ngày)
- 223
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của TanStack/db
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Garbage collection cleans up a collection while one of its transactions is still persistingCó thể đã có người làm @Nabil372 đã nhận 1 ngày trước. Đang mở
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 35/100
Maintainer thường phản hồi trong vòng 1 ngày
-
persistedCollectionOptions without a schema can't be passed to createCollection (follow-up to #1452)Đang mở
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 68/100
Maintainer thường phản hồi trong vòng 1 ngày
Issue tương tự
-
a10y a10y-audit ai-driven-qa claude
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 86/100
linagora/twake-directory-manager#219 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
Maintainer thường phản hồi trong vòng 1 ngày
-
type: bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 79/100
Maintainer thường phản hồi trong vòng 1 ngày
-
0️⃣ phase/new
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Pulse v7: buildPreferencesFromTelos() bypasses telosSectionOrFile(), renders empty on unified-TELOS installsCó thể đã có người làm @zephyr91 đã nhận hôm nay. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
danielmiessler/LifeOS#2393 ·
Maintainer thường phản hồi trong vòng 4 ngày