soft_reactivation: Add a partial index to speed up event lookups.

The full auditlog table is moderately large, and the previously-chosen
index (on `modified_user_id`) is not terribly specific.
This commit is contained in:
Alex Vandiver 2023-04-28 19:08:21 +00:00 committed by Tim Abbott
parent a56da4be76
commit 4f2417cfc4
3 changed files with 45 additions and 0 deletions

View File

@ -163,6 +163,10 @@ def add_missing_messages(user_profile: UserProfile) -> None:
# For stream messages we need to check messages against data from
# RealmAuditLog for visibility to user. So we fetch the subscription logs.
stream_ids = [sub["recipient__type_id"] for sub in all_stream_subs]
# We have a partial index on RealmAuditLog for these rows -- if
# this set changes, the partial index must be updated as well, to
# keep this query performant
events = [
RealmAuditLog.SUBSCRIPTION_CREATED,
RealmAuditLog.SUBSCRIPTION_DEACTIVATED,

View File

@ -0,0 +1,26 @@
# Generated by Django 4.2 on 2023-04-28 19:06
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("zerver", "0445_drop_userpresenceold"),
]
operations = [
migrations.AddIndex(
model_name="realmauditlog",
index=models.Index(
name="zerver_realmauditlog_user_subscriptions_idx",
fields=["modified_user", "modified_stream"],
condition=models.Q(
event_type__in=[
301, # RealmAuditLog.SUBSCRIPTION_CREATED
302, # RealmAuditLog.SUBSCRIPTION_ACTIVATED
303, # RealmAuditLog.SUBSCRIPTION_DEACTIVATED
]
),
),
),
]

View File

@ -4496,6 +4496,21 @@ class RealmAuditLog(AbstractRealmAuditLog):
return f"{self.modified_stream!r} {self.event_type} {self.event_time} {self.id}"
return f"{self.realm!r} {self.event_type} {self.event_time} {self.id}"
class Meta:
indexes = [
models.Index(
name="zerver_realmauditlog_user_subscriptions_idx",
fields=["modified_user", "modified_stream"],
condition=Q(
event_type__in=[
AbstractRealmAuditLog.SUBSCRIPTION_CREATED,
AbstractRealmAuditLog.SUBSCRIPTION_ACTIVATED,
AbstractRealmAuditLog.SUBSCRIPTION_DEACTIVATED,
]
),
)
]
class UserHotspot(models.Model):
user = models.ForeignKey(UserProfile, on_delete=CASCADE)