Doc section hints look up only the sections of the page's docs (#1087)

_fetch_section_hints matched the search against every doc section in the
library, joined each match to doc_section and doc, and only then kept the
requested FileGroups.  A common word matches thousands of sections, and each
join is a random read of a large section row, so a cold files or docs search
spent most of its time here, even when no result on the page was a doc.

The match is now limited to the rowids of the requested docs' sections, read
from covering indexes.  The results are the same.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
lrnselfreliance 2026-12-03 18:01:38 -07:00 • committed by GitHub
parent 1d432483bf
commit c973d2f40b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 47 additions and 3 deletions

View file

@ -491,6 +491,8 @@ def _fetch_section_hints(session, file_group_ids, search_str):
# FTS5 auxiliary functions (snippet, rank) must be evaluated in the innermost SELECT that scans the
# FTS table; the window function must live in a separate outer layer.
# The match is limited to the rowids of the requested docs' sections (read from covering indexes), so only
# those sections are joined; a common word may match thousands of sections elsewhere in the library.
sql = text('''
SELECT fg_id, kind, ordinal, label, snippet FROM (
SELECT fg_id, kind, ordinal, label, snippet,
@ -506,7 +508,11 @@ def _fetch_section_hints(session, file_group_ids, search_str):
FROM doc_section_fts
JOIN doc_section ds ON ds.id = doc_section_fts.rowid
JOIN doc d ON d.id = ds.doc_id
WHERE doc_section_fts MATCH :q AND d.file_group_id IN :ids
WHERE doc_section_fts MATCH :q
AND doc_section_fts.rowid IN (
SELECT s.id FROM doc page_doc
JOIN doc_section s ON s.doc_id = page_doc.id
WHERE page_doc.file_group_id IN :ids)
)
) WHERE rn = 1
''').bindparams(bindparam('ids', expanding=True))