Skip to main content

durcno@1.0.0-alpha.31

Patch Changes​

  • 054b9a4 - Thanks to @almahdi404 !

    perf(rq): narrow relation subquery projections and fix nested limit/offset

    Two changes to the SQL generated by db.query(Table).findMany/findFirst: a projection fix that avoids reading unused columns, and corrections to nested limit/offset handling.

    Narrowed relation subquery projections​

    Each related row set is built with a LEFT JOIN LATERAL over a derived table. That derived table used to select "alias".*, so PostgreSQL read every column of every matched related row off the heap even when the parent projection only referenced a few of them.

    The projection is now built from the same columns filter that drives json_build_object, so only the columns the result actually needs are read:

    -- before
    FROM (SELECT "posts".* FROM "public"."posts" "posts" WHERE ...) "posts"

    -- after
    FROM (SELECT "posts"."id", "posts"."title" FROM "public"."posts" "posts" WHERE ...) "posts"

    The saving scales with (parent rows x related rows) and is largest for tables with wide text/jsonb columns, since the unrequested ones no longer have to be fetched and detoasted.

    Alias-based select mode keeps the * projection, because the columns referenced by a SqlFn expression are not introspectable. Results are unchanged.

    Nested limit and offset corrections​

    Three fixes to the limit/offset options accepted inside a with block for many relations.

    offset was never emitted. It was accepted by the type definitions and documented, but buildRelationSubquery only handled orderBy and limit, so offset was silently dropped. It now renders after LIMIT, as PostgreSQL requires when both are present.

    limit: 0 was dropped. The guard was a truthiness check rather than an undefined check, so a zero limit never reached the generated SQL. It now correctly produces LIMIT 0 and returns an empty array.

    limit/offset no longer inline prepared Arg placeholders. Nested values were interpolated directly, which rendered an Arg object as LIMIT [object Object] and failed with a syntax error. They now bind as proper $N parameters, matching the top-level options:

    const statement = prepare(
    { lim: Arg.number(), off: Arg.number() },
    (args) =>
    db
    .prepare()
    .query(schema.Posts)
    .findMany({
    columns: { id: true },
    with: {
    comments: {
    columns: { body: true },
    orderBy: asc(schema.Comments.body),
    limit: args.lim,
    offset: args.off,
    },
    },
    }),
    );

    The shared top-level and nested limit/offset rendering is now a single appendLimitOffset helper, so both paths stay in sync.

  • c4a5ae4 - Thanks to @almahdi404 !

    perf(rows): precompute the row-conversion plan of select and relational queries

    Every result row was re-deriving what each of its values should be converted with, even though the answer only depends on the query, not on the row. The conversion is now resolved once per query and replayed for each row.

    What changed​

    handleRows is the last step of a query: it maps driver rows to the JS shapes you get back. It used to do that work per value:

    • an explicit .select({...}) looked up the projection item again for every cell and re-ran the same null / column / SqlFn / literal type dispatch on it, walking the prototype chain each time the item was not a column;
    • select("*") resolved columnsBySql[key] ?? columns[key] — plus a scan of the joined tables — for every cell of every row, from inside nested callbacks;
    • relational queries allocated an Object.keys array per row (per nested child object too) and re-resolved the alias map, the table columns, and the relation map on every key.

    All three now build a plan from the keys of the first row and loop over it:

    // before — per cell
    const item = this.#$select[key];
    if (item === null) { /* ... */ }
    else if (isTCol(item)) row[key] = item.fromDriver(row[key]);
    else if (item instanceof SqlFn) { /* ... */ }
    else if (typeof item === "bigint") { /* ... */ }

    // after — per key, once
    const convert = buildSelectItemConverter(this.#$select[key]);
    // then, per cell
    row[key] = convert(row[key]);

    Relational queries do the same per level of the relation tree, so the plan for users -> posts -> comments is built once and reused by every parent row and every nested child.

    Two smaller wins on the same path: Column caches its array dimensions instead of reading them back through the config on every conversion, and the converter of a CTE/virtual column is resolved from its source expression once, at construction, rather than re-dispatching on the source kind per value.

    The five copies of the SELECT * row mapper (select, insert, update, delete, first) are now one mapStarRows helper in src/query-builders/helpers.ts, with select caching its plan across rows.

    Results​

    Measured with the new pnpm bench suite (perf/), which feeds synthetic driver rows to handleRows so the numbers isolate conversion cost from SQL generation and network time. Both variants ran interleaved in one process, medians over 15 rounds:

    BenchmarkBeforeAfterSpeedup
    select("*") — 10k rows × 12 col13.1 ms7.8 ms1.7×
    select({...}) — 10k rows × 8 col2.2 ms0.77 ms2.9×
    relational nested — 2k × 5 × 211.9 ms7.0 ms1.7×

    Results are unchanged. tests/qb/rows.unit.test.ts covers the conversion of star, aliased, joined, and relational results, including NULL handling, the Column ... not found in any table error, and reusing a query's plan across executions.