Repository navigation
Reduce dots() N+1 queries to a single batch fetch - #146
OpenGithubs wants to merge 1 commit into
Conversation
Previously dots() issued one dot_spaces SELECT per dot (N+1 pattern). Now fetches all dot_spaces rows once and groups them in memory via a Map. With 10 dots this cuts 11 DB round-trips down to 2.
|
Verified the win against main: dots() goes from N+1 round-trips to 2, and grouping with a Map preserves the old ORDER BY spaceId since .sort() on these TEXT ids is the same lexicographic order. One behavior change hiding in the fallback: |
|
Checked this locally before weighing in. Ran the old and new dots() against the same fixture (5 dots across 3 spaces, one dot granted to a second space): outputs are byte-identical and prepare calls drop from 6 to 2, so the N+1 to batch claim holds up. One thing I looked at while reviewing: the Ordering is also equivalent: SQLite's default BINARY collation on these ASCII ids matches JS .sort(). |
Summary
Performance optimization:
WorkspaceStore.dots()used an N+1 query pattern — oneSELECT * FROM dotsfollowed by oneSELECT spaceId FROM dot_spaces WHERE dotId=?per dot. With N dots that is N+1 database round-trips just to list the workspace. Now fetches alldot_spacesrows once and groups them in memory with a Map.Changes
src/server/workspace.ts: batch fetch dot_spaces, group in-memory, cut N+1 round-trips down to 2.Commits
perf-dots-joinperf: reduce dots() N+1 queries to a single batch fetch