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

@@ -0,0 +1,23 @@
# Generated by Django 5.2.18 on 2026-10-31 22:05
from django.conf import settings
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("articles", "0004_starred_saved_as_system_tags"),
("feeds", "0006_backfill_feed_next_fetch_at"),
("rules", "0002_folder_scope_to_tag_scope"),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
operations = [
migrations.AddIndex(
model_name="filterrule",
index=models.Index(
fields=["user", "is_enabled", "priority"], name="rules_filte_user_id_dcb19b_idx"
),
),
]
+10
View File
@@ -75,6 +75,16 @@ class FilterRule(models.Model):
class Meta:
ordering = ["priority", "id"]
indexes = [
# Matches the ingest-time query shape exactly (C9): every
# caller — apps.articles.tasks.apply_filter_rules_for_feed,
# apps.rules.tasks.reapply_rules_for_user_backlog — filters by
# (user, is_enabled) and orders by priority. `user` alone
# already has an index from the ForeignKey, but it doesn't
# cover is_enabled or let the database satisfy the ORDER BY
# from the index itself.
models.Index(fields=["user", "is_enabled", "priority"]),
]
def __str__(self):
label = self.name or f"{self.get_match_field_display()} {self.get_match_type_display()}"