scheduled_messages: Add focused scheduled message indexes.

This commit is contained in:
Tim Abbott 2023-05-04 17:38:55 -07:00
parent 835f62617e
commit 602e4c2aa3
2 changed files with 54 additions and 0 deletions

View File

@ -0,0 +1,28 @@
# Generated by Django 4.2 on 2023-05-09 00:58
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("zerver", "0448_scheduledmessage_new_fields"),
]
operations = [
migrations.AddIndex(
model_name="scheduledmessage",
index=models.Index(
condition=models.Q(("delivered", False), ("failed", False)),
fields=["scheduled_timestamp"],
name="zerver_unsent_scheduled_messages_by_time",
),
),
migrations.AddIndex(
model_name="scheduledmessage",
index=models.Index(
condition=models.Q(("delivered", False)),
fields=["sender", "delivery_type", "scheduled_timestamp"],
name="zerver_unsent_scheduled_messages_by_user",
),
),
]

View File

@ -4346,6 +4346,32 @@ class ScheduledMessage(models.Model):
default=SEND_LATER,
)
class Meta:
indexes = [
# We expect a large number of delivered scheduled messages
# to accumulate over time. This first index is for the
# deliver_scheduled_messages worker.
models.Index(
name="zerver_unsent_scheduled_messages_by_time",
fields=["scheduled_timestamp"],
condition=Q(
delivered=False,
failed=False,
),
),
# This index is for displaying scheduled messages to the
# user themself via the API; we don't filter failed
# messages since we will want to display those so that
# failures don't just disappear into a black hole.
models.Index(
name="zerver_unsent_scheduled_messages_by_user",
fields=["sender", "delivery_type", "scheduled_timestamp"],
condition=Q(
delivered=False,
),
),
]
def __str__(self) -> str:
display_recipient = get_display_recipient(self.recipient)
return f"{display_recipient} {self.subject} {self.sender!r} {self.scheduled_timestamp}"