Skip to content

perf(db): R.4 — desc nulls last, swept and measured site by site - #318

Merged
Willi363363 merged 1 commit into
stagingfrom
perf/desc-nulls-last-sweep
Sep 18, 2026
Merged

Willi363363 merged 1 commit into
stagingfrom
perf/desc-nulls-last-sweep

Conversation

@Willi363363

@Willi363363 Willi363363 commented Sep 18, 2026 •

Copy link
Copy Markdown
Owner

Fourth and last step of track R, rebased on staging and carrying its own
R.4 ⬜ → ✅ tick. Independent of #315, #316 and #317 in code — but it edits
the same register file
, see Before merging.

09-query-debt.md asked for the sweep H.2 could not do in passing: "every
.desc() in an index, against every query that orders on it"
. Four indexes are
written DESC NULLS LAST; two of their queries already matched, found by H.2
and by K. The other two were probed one at a time.

Measured, site by site

Fifty thousand leaderboard entries over two thousand players, and twenty-five
thousand coin movements over two accounts. Median of seven explain (analyze)
runs each.

Site Before After
coins.ts the ledger read 3.3 ms, Bitmap Heap Scan + Sort 1.5 ms, Index Scan + Incremental Sort
account.ts the export 1.2 ms, Bitmap Heap Scan + Sort 0.5 ms, Index Scan, no sort at all
leaderboard.ts all-time board 26.5 ms 26.5 ms — left alone
daily-board.ts the day's board 6.3 ms 6.3 ms — left alone

Only the two that moved are changed. That is the discipline the register
asks for: a query fixed on the strength of another query's number is exactly the
guess it exists to refuse. daily-board.ts filters on game.daily_day and never
on the entry's mode, so the partial (mode, score desc, …) index cannot serve
it whatever the ordering says.

The ledger's sort becomes incremental rather than disappearing because it also
orders on seq, which no index carries beside created_at — the tie inside one
created_at still sorts.

The register's own number was wrong, and is corrected

09-query-debt.md claimed the all-time board went 45 ms → 26 ms by changing
one ordering. It does not reproduce. All four spellings — inner, outer, both,
neither — measured 26.5 to 27.1 ms on the same seed, with identical plans, and
identical again with enable_seqscan off.

The reason is G.7: the board is distinct on (user_id) now, so its ordering
starts with user_id, and no index starts there and continues with score.
The 19 ms was recovered by that deduplication, not by the ordering — and the row
was written before that shape existed. Corrected in place rather than deleted,
because a number that turned out to belong to something else is the thing this
register is for.

One spelling, not two

newestClockFirst is exported from coins.ts and used by exportAccount. An
order that has to match an index in two files is one source of truth or it is
the same defect coming back in whichever file is edited second.

Tests

Two cases in coins-volume.test.ts, both failing with the bare desc put
back — the plan one because the index is no longer reachable with seqscan and
bitmapscan off, the SQL one because the text is no longer written.

Checks

  • turbo run test --force --concurrency=1: 10 of 10 tasks, 3 866 passed, 0 skipped.
  • turbo run typecheck lint --force: 22 of 22.
  • pnpm format:check: green.

Before merging

#316 and #317 also edit 09-query-debt.md, which is at its 200-line cap. On this
branch the file is 195; on top of those two it will be over. Whichever merges
last gets rebased and re-trimmed, and the register is due a split either way —
recorded rather than done here, since it is not this step.

🤖 Generated with Claude Code

https://claude.ai/code/session_01DsMRxyowr2Ch5RwiAa3t9z

@vercel

vercel Bot commented Sep 18, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
wikifake Ready Ready Preview Sep 18, 2026 11:57am UTC

@qodo-code-review

Copy link
Copy Markdown

ⓘ Qodo reviews are paused because your trial has ended. Ask your workspace admin to add credits to resume reviews. Manage billing

`09-query-debt.md` asked for the sweep H.2 could not do in passing: every
`.desc()` in an index, against every query that orders on it. Four indexes are
written `DESC NULLS LAST`; two of their queries already matched. The other two
were probed one at a time, on fifty thousand leaderboard entries and twenty-five
thousand coin movements.

    coins.ts    the ledger read   3.3 ms → 1.5 ms   Bitmap Heap Scan + Sort
                                                    became Index Scan +
                                                    Incremental Sort
    account.ts  the export        1.2 ms → 0.5 ms   the sort disappears
    leaderboard.ts  all-time      26.5 ms → 26.5 ms  unchanged, left alone
    daily-board.ts  the day       6.3 ms → 6.3 ms    unchanged, left alone

Only the two that moved are changed, which is what the register asked for: a
query fixed on the strength of another query's number is the guess it exists to
refuse. `daily-board.ts` filters on `game.daily_day` and never on the entry's
`mode`, so the partial index cannot serve it whatever the ordering says.

**The register's own 45 ms → 26 ms does not reproduce, and the entry is
corrected.** All four spellings of the all-time board — inner, outer, both,
neither — measured 26.5 to 27.1 ms with identical plans, and identical again
with `enable_seqscan` off. The board is `distinct on (user_id)` since G.7, so
its ordering starts with `user_id` and no index starts there and continues with
`score`. The 19 ms belongs to that deduplication, not to an ordering.

The spelling is one constant, exported from `coins.ts` and used by
`exportAccount`: an order that has to match an index in two files is one source
of truth or it comes back in whichever file is edited second.

Both new cases fail with the bare `desc` put back — the plan one because the
index is no longer reachable, the SQL one because the text is no longer written.

`turbo run test --force --concurrency=1`: 10 of 10 tasks, 3 866 passed, 0
skipped. Typecheck and lint green over all 22 tasks.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DsMRxyowr2Ch5RwiAa3t9z
@Willi363363
Willi363363 force-pushed the perf/desc-nulls-last-sweep branch from 3b32680 to 5147417 Compare September 18, 2026 11:54
@Willi363363
Willi363363 merged commit 0bc14b3 into staging Sep 18, 2026
16 of 18 checks passed

This branch was successfully deployed

1 active deployment
Preview — 5147417c Deployed Sep 18, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant