Files
zulip/zerver/migrations/0693_add_conditional_indexes_for_topic.py
Alex Vandiver 33e1d583bf messages: Add new conditional versions of subject indexes.
Indexes on topic ("subject") are polluted by the existence of DMs,
which all have empty topics, and as such skew the statistics greatly.
This is particularly important given the new use of the empty topic
for the "general chat" function -- left as-is, the database makes bad
query plans because it believes the topic is vastly more common than
it actually is.

We move the old indexes to a new name with `_all`, and
recreate (concurrently) the same indexes but with a condition on
is_channel_message.  These new indexes are unused at current, until
the query-building logic adds limits on is_channel_message; see the
following commit.
2025-03-18 09:34:11 -07:00

62 lines
2.3 KiB
Python

import django.db.models.functions.text
from django.contrib.postgres.operations import AddIndexConcurrently
from django.db import migrations, models
class Migration(migrations.Migration):
atomic = False
dependencies = [
("zerver", "0692_alter_message_is_channel_message"),
]
operations = [
migrations.RenameIndex(
model_name="message",
new_name="zerver_message_realm_upper_subject_all",
old_name="zerver_message_realm_upper_subject",
),
migrations.RenameIndex(
model_name="message",
new_name="zerver_message_realm_recipient_upper_subject_all",
old_name="zerver_message_realm_recipient_upper_subject",
),
migrations.RenameIndex(
model_name="message",
new_name="zerver_message_realm_recipient_subject_all",
old_name="zerver_message_realm_recipient_subject",
),
AddIndexConcurrently(
model_name="message",
index=models.Index(
models.F("realm_id"),
django.db.models.functions.text.Upper("subject"),
models.OrderBy(models.F("id"), descending=True, nulls_last=True),
condition=models.Q(("is_channel_message", True)),
name="zerver_message_realm_upper_subject",
),
),
AddIndexConcurrently(
model_name="message",
index=models.Index(
models.F("realm_id"),
models.F("recipient_id"),
django.db.models.functions.text.Upper("subject"),
models.OrderBy(models.F("id"), descending=True, nulls_last=True),
condition=models.Q(("is_channel_message", True)),
name="zerver_message_realm_recipient_upper_subject",
),
),
AddIndexConcurrently(
model_name="message",
index=models.Index(
models.F("realm_id"),
models.F("recipient_id"),
models.F("subject"),
models.OrderBy(models.F("id"), descending=True, nulls_last=True),
condition=models.Q(("is_channel_message", True)),
name="zerver_message_realm_recipient_subject",
),
),
]