perf: indexes + remove slug roundtrip, encode doc URLs as short IDs - #98
Merged
Conversation
Add missing indexes that caused seq scans in getDocs: - Document.authorId (owned docs lookup) - Collaborator.userId (collaborated docs via Collaborator.some) - CollaborationRequest.userId and documentId (request lookups)
- Delete slugIDtoFullID helper that doubled WS DB load: it did findUnique where id=documentName and returned it unchanged (documentName is already the full UUID after #45). dbPersistence now uses documentName directly in fetch/store. - Add reversible shortId utils (UUID <-> 22-char base64url): server/src/utils/short-id.ts and client/src/utils/short-id.ts. No DB lookup, opaque IDs like Google/Notion style. - Encode links via paths.getHref (uuidToShortId) and decode in document route with try/catch that redirects to 404 on malformed short ID instead of 500.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes two performance/bloat issues and improves URL UX.
1. Missing DB indexes → seq scans
getDocsfilters byDocument.authorIdandCollaborator.some { userId }. With no index on those columns, Postgres does full table scans.EXPLAINon the dev DB (49 docs) confirmed:Seq Scan on documentsfiltered byauthorIdSeq Scan on collaboratorsfiltered byuserIdUnder k6 with 1k docs this dominates, not Yjs/CPU. Added 4 indexes:
Document @@index([authorId])Collaborator @@index([userId])CollaborationRequest @@index([userId])and@@index([documentId])After:
Index Scan using documents_authorId_idx. Migration20260829200000_add_performance_indexes— 0-risk, additive.2.
slugIDtoFullIDredundant roundtrip → ~2× WS DB loadAfter #45 the helper does
findUnique where id=slugIdand returns it unchanged — the WSdocumentNameis already the full UUID (client sendsdoc.idviauseCollab/paths.getHref, validated bygetDocumentPermission). EverydbPersistence.fetch/storepaid an extra DB query for zero value.server/src/utils/slugIDtoFullID.tsand its test (only used indbPersistence).dbPersistencenow usesdocumentNamedirectly asid.3. Bare UUIDs in URLs → opaque short IDs
/app/doc/<uuid>exposes internal IDs. Switched to 22-char base64url short IDs (Google/Notion/Linear style) without DB lookup:uuidToShortId/shortIdToUuidinserver/src/utils/short-id.ts(Node Buffer) andclient/src/utils/short-id.ts(browserbtoa/ fallback, no extra dep).paths.app.document.getHrefencodes UUIDs;DocumentPagedecodes the:idparam viaresolveDocumentIdwith atry/catchthat redirects to 404 on malformed input (avoids unhandled 500).