Skip to content

fix search term performance - #271

Merged
chiatt merged 1 commit into
mainfrom
adg/term-search-performance
Oct 22, 2026
Merged

chiatt merged 1 commit into
mainfrom
adg/term-search-performance

Conversation

@aarongundel

Copy link
Copy Markdown
Contributor

improves search term performance

@chiatt

chiatt commented Oct 22, 2026

Copy link
Copy Markdown
Member

This changes in this PR improve the performance of term searches in basic search which are performed by default with multiple relationship hops. Before this PR, two things made that query slow:

  1. The term match subquery was copied into the SQL many times. The seed set (rows in TermSearch that match the term) was a nested Django subquery. Each hop then nested it again for both the "from" and "to" directions, and the results were combined with OR across many IN (...) clauses. With 2 hops, the term lookup appeared in the SQL several times. Postgres often turns OR'd IN subqueries like these into per-row SubPlans, which scale very badly.
  2. The substring match used icontains, which Django compiles to UPPER(value) LIKE UPPER('%term%').

This PR addresses both of these issues.

It's worth noting that there may be cases where relationship hoping will be too expensive even with this change. Being able to reduce the number of hops (currently hard coded to a max of 2) would be really important. That change would be out of scope for this PR, but instead should be addressed in a new PR with this feature: #272

@chiatt chiatt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm! This does remind me of the need to allow admins to control the hop cap: #272

@chiatt
chiatt merged commit 203f844 into main Oct 22, 2026
6 checks passed
@chiatt
chiatt deleted the adg/term-search-performance branch October 22, 2026 22:17
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.

2 participants