From ac03a3d6093922324dfff3a424d51e9d28ca7134 Mon Sep 17 00:00:00 2001 From: Trenton H <797416+stumpylog@users.noreply.github.com> Date: Mon, 30 Mar 2026 15:14:15 -0700 Subject: [PATCH] Fix: count notes during iteration instead of issuing extra COUNT(*) query document.notes.count() bypasses the prefetch cache and hits the DB on every document during rebuild. Counting in the existing loop eliminates the query entirely. Co-Authored-By: Claude Sonnet 4.6 --- src/documents/search/_backend.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/documents/search/_backend.py b/src/documents/search/_backend.py index f2d697a05..fc1c57262 100644 --- a/src/documents/search/_backend.py +++ b/src/documents/search/_backend.py @@ -269,7 +269,9 @@ class TantivyBackend: # Notes — JSON for structured queries (notes.user:alice, notes.note:text), # companion text field for default full-text search. + num_notes = 0 for note in document.notes.all(): + num_notes += 1 note_data: dict[str, str] = {"note": note.note} if note.user: note_data["user"] = note.user.username @@ -305,7 +307,7 @@ class TantivyBackend: if document.page_count is not None: doc.add_unsigned("page_count", document.page_count) - doc.add_unsigned("num_notes", document.notes.count()) + doc.add_unsigned("num_notes", num_notes) # Owner if document.owner_id: