Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

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

Alex Vandiver4f2417cfc4be
diff --git a/zerver/lib/soft_deactivation.py b/zerver/lib/soft_deactivation.py
index e55566da9b3..8e1ef3ad58a 100644
--- a/zerver/lib/soft_deactivation.py
+++ b/zerver/lib/soft_deactivation.py
@@ -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,
diff --git a/zerver/migrations/0446_realmauditlog_zerver_realmauditlog_user_subscriptions_idx.py b/zerver/migrations/0446_realmauditlog_zerver_realmauditlog_user_subscriptions_idx.py
new file mode 100644
index 00000000000..54d7046f825
--- /dev/null
+++ b/zerver/migrations/0446_realmauditlog_zerver_realmauditlog_user_subscriptions_idx.py
@@ -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
+ ]
+ ),
+ ),
+ ),
+ ]
diff --git a/zerver/models.py b/zerver/models.py
index 25aa796f077..102221d1859 100644
--- a/zerver/models.py
+++ b/zerver/models.py
@@ -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)