Query-builder views record full table scans, so any write re-runs them for every subscriber
Problem
Query-builder views (views that return a query, e.g. ctx.from.a.where(...).rightSemijoin(ctx.from.b, ...)) record a full table scan in their read set for every table the query touches:
// Track read sets for all tables involved in this plan.
// TODO(jsdt): This means we will rerun the view and query for any change to these tables, so we should optimize this asap.
for table_id in plan.table_ids() {
tx.record_table_scan(&op, table_id);
}So any write to any row of any table the view reads makes that view stale for every subscribed sender, and call_views_with_tx_at re-runs all of them inside the writing transaction, charging the writer.
For the common per-sender pattern this turns an O(1) write into O(subscribers):
export const my_items = spacetimedb.view(
{ name: "my_items", public: true },
t.array(item.rowType),
(ctx) =>
ctx.from.membership
.where((m) => m.owner.eq(ctx.sender))
.rightSemijoin(ctx.from.item, (m, i) => m.group.eq(i.group)),
);Every insert/update of one item row re-runs my_items for every subscriber, although only the owners of that row's group can be affected. Total cost grows as writes × subscribers, i.e. ~N² in users.
Measured on Maincloud
TypeScript module, 50 subscribed clients, each subscribed to a view shaped like the one above; 1,670 writes to the joined table over ~10 min (plus ~1,150 procedure runs and ~600 setup/lifecycle calls):
- 21.8B billed CPU instructions (~10.9 s of V8 time, ~11 TeV), 94% of the database's energy.
- Comparing against a 2-subscriber run: roughly 1.4 ms base per call, and ~75 µs per view refresh. With 50 subscribers, each write paid ~50 refreshes (~4 ms) on top of its own work.
- Extrapolated: at S concurrent subscribers each write costs S × ~75 µs of transaction time, so a single database saturates around a few thousand concurrent subscribers at a modest write rate (~10–20 writes/s), well before cost matters.
The same module's procedural views that use point index lookups (ctx.db.t.idx.filter(key)) get precise read sets and don't show this.
Proposed fix
Derive precise read sets from the compiled plan instead of whole tables, the way procedural views record them through record_index_scan_point:
- For index-driven plans, record the point keys the plan actually probed (e.g.
membership.owner = sender, anditem.group = gfor each joinedg). - Fall back to a full table scan only for operators that really scan (and range scans, until #3762 lands).
Alternatively, maintain query-builder views incrementally in the subscription engine (delta evaluation) rather than re-running them.
Workaround
Rewrite query-builder views as procedural views that use point index lookups:
(ctx) => {
const out = [];
for (const m of ctx.db.membership.owner.filter(ctx.sender))
for (const i of ctx.db.item.group.filter(m.group)) out.push(i);
return out;
}Related: #3762 (range scans in view read sets are also recorded as full table scans).
Source: clockworklabs/SpacetimeDB