Tag search optimization#969
Merged
Merged
Conversation
… JOINs
The previous implementation added one JOIN through the M2M for every
filter tag:
for tag in tags:
qs = qs.filter(tags__name=tag)
With 6-8 tags this produces ~16 JOINs against thread_tags/tag and the
planner degrades superlinearly. On a small dataset (~3000 posts), 5-8
tag intersection queries took 4-12 seconds.
The relational idiom for set intersection is one subquery counting
matched tags per thread:
SELECT thread_id FROM thread_tags
WHERE tag_name IN (...)
GROUP BY thread_id
HAVING COUNT(DISTINCT tag_id) = N
Implemented via the auto-generated M2M through model (Thread.tags.through)
with Count(distinct=True). Semantics preserved: case-sensitive name
match, AND across all tags. Language-code filtering remains handled
upstream (via the per-tag Tag.objects.get(name__iexact=...,
language_code=...) when TAG_SEARCH_INPUT_ENABLED is true).
Tests: extend ThreadTagModelsTests.test_run_adv_search_ANDing_tags with
4-tag and 6-tag assertions to exercise the new code path; the existing
1/2/3-tag assertions continue to verify semantics. Production data on
ask.wingware.com (2026-05-19): avg response time on tag-filter URLs
dropped from 947ms to 112ms after this fix landed.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
The subquery + HAVING COUNT form compares Count(DISTINCT tag_id) to len(tags), but unified_tags() concatenates query_tags + tags so the same tag can appear twice (e.g. "#tag1" plus selector "tag1") and the inflated len() can never match. Likewise, when every requested tag is unknown the tag list becomes empty after the case-insensitive lookup, which under the new query returns zero threads instead of leaving the result set unfiltered. Deduplicate the names and skip the subquery when the list is empty.
scripts/perf_tag_search.py drives /questions/tags:.../ with an increasing tag count against a running dev server and prints min/median/max timings; useful for measuring the JOIN-vs-subquery gap before/after the optimization.
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.
No description provided.