Skip to content

ORM: relation where re-resolves a non-_id relation target once per drain #446

Closed

Description

@MikeyZhang75

Summary

PR #420 fixed the main case — this is a narrower follow-up, not a duplicate.

#420 added a per-execution _documentByNormalizedId memo inside _getById, so relation targets resolved by db.get are now read once per query execution instead of once per (parent, drain). Verified: the original shape issued 200 db.get calls at the audit commit and issues 2 on current main.

What survives: a relation where referencing a one relation joined on anything other than _id still re-resolves its target once per drain.

Root cause

packages/kitcn/src/orm/query.ts:8362 — _loadOneRelation sets useGetById only when the join is on the primary id (:8409):

targetFields.length === 1 && targetFields[0] === '_id'

The else branch at :8429-8435 issues:

this._queryByFields(this.db.query(edge.targetTable), targetFields, values, indexName).first()

with no cross-drain memo. The chunk drain at :9221 re-enters it for every 32-row batch, so the same target is re-read once per batch.

So r.one.x({ to: x.id }) — the common shape — is covered by #420's memo; r.one.x({ to: x.someOtherColumn }) is not.

Not affected by the other merges

#425, #426 and #427 do not touch this path. #427 explicitly excludes one relations from its existence-probe lowering — its own note: "A one relation is a single row either way, so there is nothing to stop early at."

Suggested fix

Give _loadOneRelation's non-id branch the same execution-scoped memo, keyed on (targetTable, targetFields, values) rather than on a normalized id.

The staleness argument #420 already wrote for _documentByNormalizedId applies verbatim — same execution scope, same invalidation reasoning.

Small.

Existing tests

packages/kitcn/src/orm/query.relation-where-reads.vitest.ts:110 and :132 (added by #420) assert counts.get <= 2 over a 50-row scan with 2 distinct targets — those pin the _getById memo. The non-_id branch has no equivalent; extend that file rather than starting a new one.

Environment

  • kitcn 0.32.1
  • convex 1.44.0
  • verified and measured against origin/main at 14bab503

Activity

  1. self-assigned this
    on Sep 5, 2026
  2. added a commit that references this issue on Sep 13, 2026
    75ceda6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions