Batch rule/tag lookups and index FilterRule for the ingest query (C9)

FilterRule had no indexes beyond the default primary/foreign-key ones,
and apply_filter_rules_for_feed issued 2 queries per subscriber (its
enabled rules, its subscription tags) inside a loop over every
subscriber of the feed — both cost grew with subscriber/rule count on
every single feed poll.

Adds a composite index on (user, is_enabled, priority), matching the
exact filter+order shape every caller already uses (this task,
apps.rules.tasks' backlog re-apply). apply_filter_rules_for_feed now
fetches rules and subscription tags once for the whole feed's subscriber
set, grouped by user/subscription in Python, instead of once per
subscriber.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
ksmithandClaude Sonnet 5 committed 2026-10-31 16:09:36 -06:00
1 parent abf4cef55b
commit cb0ce5a336
4 files changed
+91 -11

No files matched your search

+37
View File
@@ -150,3 +150,40 @@ def test_disabled_rule_does_not_apply(feed, alice):
@pytest.mark.django_db
def test_dispatch_with_empty_article_ids_is_a_noop(feed):
assert apply_filter_rules_for_feed(feed.id, []) == 0
@pytest.mark.django_db
def test_dispatch_fetches_rules_and_tags_once_per_feed_not_once_per_subscriber(feed):
"""Rules/subscription tags are batched across every subscriber of the
feed in one query each (C9), not re-queried per subscriber — assert
this by checking the query count stays flat as subscribers grow."""
from django.db import connection
from django.test.utils import CaptureQueriesContext
users = [User.objects.create_user(f"user{i}", password="pw-123456") for i in range(5)]
for user in users:
sub = Subscription.objects.create(user=user, feed=feed)
tag = Tag.objects.create(user=user, name="Tech")
SubscriptionTag.objects.create(user=user, subscription=sub, tag=tag)
FilterRule.objects.create(
user=user,
tag=tag,
match_field=FilterRule.FIELD_TITLE,
match_type=FilterRule.MATCH_CONTAINS,
match_value="Python",
action=FilterRule.ACTION_STAR,
)
article = Article.objects.create(feed=feed, guid="guid-1", title="Python news")
with CaptureQueriesContext(connection) as ctx:
apply_filter_rules_for_feed(feed.id, [article.pk])
# Exactly one SELECT against each table regardless of subscriber
# count (5 here) — a regression back to per-subscription querying
# would show up as 5 of each instead of 1.
rule_selects = [q for q in ctx.captured_queries if "rules_filterrule" in q["sql"]]
tag_selects = [q for q in ctx.captured_queries if "articles_subscriptiontag" in q["sql"]]
assert len(rule_selects) == 1
assert len(tag_selects) == 1
for user in users:
assert is_starred(user, article) is True